Spring e Hibernate, Problema ao utilizar a anotação "@Autowired"

1 resposta
JavaThai

Bom dia a todos.
Sr's estou tendo um problema na minha aplicação criei um crud simples utilizando Spring 3.0, hibernate 3.5 ,MySql e quando tento gravar uma tarefa
ele roda, mas me devolve um NullPointEx.
Eu utilizei o debug,ele mostra que a SessionFactory dele esta indo null,e no meu DAO, eu não consigo utilizar a anotação"@Autowired" pq quando eu salvo o TomCat me mostra o seguinte Erro:

606 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tarefaController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.emerson.tarefas.service.TarefaService br.com.emerson.tarefas.controller.TarefaController.tarefaService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tarefaService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private br.com.emerson.tarefas.dao.TarefaDao br.com.emerson.tarefas.service.TarefaService.tarefaDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tarefaDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory br.com.emerson.tarefas.dao.TarefaDao.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)
Então eu sou obrigado a tirar a anotação para rodar a app,então ele entra no meu metodo "persistence" do hibernate mas logo em seguida ele vai pro Exception e me devolve aquele nullpoint. Sr's estou sem idea de como resolver isso,será alguem tem alguma ideia de como eu possa resolver isso? Segue abaixo o meu código.
package br.com.empresa.tarefas.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import br.com.empresa.tarefas.modelo.Tarefa;
import br.com.empresa.tarefas.service.TarefaService;

@Controller
public class TarefaController {
	private static final Logger logger = LoggerFactory.getLogger(TarefaController.class);
	
	@Autowired
	private TarefaService tarefaService;
	
	@RequestMapping("adiciona-tarefa")
	public String chamaFormAdd(){
		return "tarefa/adiciona-tarefa";
	}
	
	@RequestMapping("salvaTarefa")
	public String salvar(Tarefa t){
		tarefaService.salvar(t);
		return "tarefa/lista-tarefas";
	}
	
}
package br.com.empresa.tarefas.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.com.empresa.tarefas.dao.TarefaDao;
import br.com.empresa.tarefas.modelo.Tarefa;

@Service
public class TarefaService {
	@Autowired
	private TarefaDao tarefaDao;
	
	@Transactional
	public void salvar(Tarefa t){
		tarefaDao.salvar(t);
	}

}
package br.com.empresa.tarefas.dao;


import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import br.com.empresa.tarefas.modelo.Tarefa;

@Repository
public class TarefaDao {
	private SessionFactory sessionFactory;
	
	public void salvar(Tarefa t){
		try {
			sessionFactory.getCurrentSession().persist(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1 Resposta

JavaThai

Ninguem se arisca?
È eu acho que F :?: :?: :?: :?: :?: :?: :?: :?: :?: .

Criado 5 de novembro de 2012
Ultima resposta 5 de nov. de 2012
Respostas 1
Participantes 1