Session + ApplicationScoped

1 resposta
jingle

Tenho minha classe startUp que nela gostaria de fazer as configuraçoes iniciais do meus sistema, exemplo: verificar se existe um usuario admim e caso nao exista inserir no banco o usuario admim…

porém preciso da minha Session do hibernate que nas minhas controller sempre recebi via injection que o vraptor fazia.

tentei receber o session via injection nessa minha classe com applicationScoped e da erro.

código:

@Component
@ApplicationScoped
public class StartUp {
	private final Session session;

	public StartUp(Session session) {
		this.session = session;
	}
....

Erro:

Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.Session]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionCreator': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionCreator': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

1 Resposta

Lucas_Cavalcanti

a Session do hibernate é RequestScoped, ou seja, precisa de uma Request pra funcionar, para ser aberta e fechada no tempo certo…

se vc precisa de uma Session num applicationScoped, vc precisa controlar a Session manualmente, ou seja, receber uma SessionFactory no construtor e abrir e fechar a Session quando for conveniente

Criado 27 de julho de 2010
Ultima resposta 27 de jul. de 2010
Respostas 1
Participantes 2