Announcement Announcement Module
Collapse
No announcement yet.
Autowiring with annotations exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring with annotations exception

    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:

    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()}
    Here is the code of the classes:

    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 "";
    	}
    }
    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());
    	}
    }
    I got the exception after adding the @Inject annotation above publicadorDao.

    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);
    	}
    }
    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>
    Can anyone figure it out what am i doing wrong? Thanks a lot.

  • #2
    Hello there.

    I already fixed the problem, but i did not understand what i have done.

    On the service class, instead of used the PublicadorDao class on the @Inject annotation i have used the interface like this:

    Code:
    @Inject
    private IPublicadorDao  publicadorDao;
    Why using the interface instead the concrete class annotated with the @Repository it works?

    Comment


    • #3
      I don't know, but I would point out the following:
      * PublicadorDao implements BaseDao, not IPublicadorDao. You don't show that definition or reference anywhere.
      * Probably irrelevant, but perhaps '@Repository("pulbicadorDao")' should be '@Repository("publicadorDao")'.

      Comment


      • #4
        You are right on both cases.

        1) I have noticed too that the name of the repository was wrong. But using the wright name or even removing it the problem remains.

        2) I have mistaken on the interface. Meanwhile I changed the interface to IPublicadorDao wich extends from the interface BaseDao. AND IT IS WORKING. But i don't know why.

        Would Spring thinks that BaseDao is a Repository too? But it isn't annotated!!! Im not understanding the root of the problem.

        Comment

        Working...
        X