Hello there!! I'm new with spring and i am experiencing a problem wiring components. I have wired a Controller class to a Service class. But i'm having problems wiring the service class to a DAO class. i get this exception:
Here is the code of the classes:Code:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'publicadorService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pt.msoftware.territorios.persistence.dao.PublicadorDao pt.msoftware.territorios.persistence.services.PublicadorService.publicadorDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [pt.msoftware.territorios.persistence.dao.PublicadorDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
Code:@Controller @RequestMapping("/publicadores") public class PublicadorController { @Inject private PublicadorService publicadorService; public PublicadorController() { } @RequestMapping("managePublicador") public String managePublicador(Model model) { model.addAttribute("publicador", new Publicador()); return "/publicadores/managePub"; } @RequestMapping(value="savePub", method=RequestMethod.POST) public String savePublicador(Publicador publicador, BindingResult bindingResult) { publicadorService.savePublicador(publicador); return ""; } }I got the exception after adding the @Inject annotation above publicadorDao.Code:@Service public class PublicadorService { @Inject private PublicadorDao publicadorDao; public PublicadorService() { } public PublicadorService(PublicadorDao publicadorDao) { this.publicadorDao = publicadorDao; } public void savePublicador(Publicador publicador) { publicadorDao.merge(publicador); } public Publicador getPublicador(Publicador publicador) { return publicadorDao.find(publicador.getId()); } }
Code:@Repository("pulbicadorDao") @Transactional public class PublicadorDao implements BaseDao { @PersistenceContext private EntityManager em; public void persist(BaseEntity baseEntity) { em.persist(baseEntity); } public void merge(BaseEntity baseEntity) { em.merge(baseEntity); } public void delete(BaseEntity baseEntity) { em.remove(baseEntity); } public Publicador find(Integer id) { return em.find(Publicador.class, id); } }Can anyone figure it out what am i doing wrong? Thanks a lot.Code:<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="pt.msoftware.territorios" /> <!-- JPA AND HIBERNATE --> <beans:bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <beans:property name="dataSource" ref="dataSource" /> <beans:property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> <beans:property name="persistenceUnitName" value="territoriosPU"/> </beans:bean> <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/> <beans:property name="url" value="jdbc:mysql://localhost:3306/territorios"/> <beans:property name="username" value="territuser"/> <beans:property name="password" value="territuser"/> </beans:bean> <beans:bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <beans:property name="database" value="MYSQL" /> <beans:property name="showSql" value="true" /> <beans:property name="generateDdl" value="true" /> <beans:property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> </beans:bean> <beans:bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <!-- Exception translation bean post processor --> <beans:bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> </beans:beans>


Reply With Quote