Announcement Announcement Module
No announcement yet.
Autowiring with annotations exception Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:

    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; 
    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:

    public class PublicadorController {
    	private PublicadorService publicadorService;
    	public PublicadorController() {
    	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) {
    		return "";
    public class PublicadorService {
    	private PublicadorDao publicadorDao;
    	public PublicadorService() {
    	public PublicadorService(PublicadorDao publicadorDao) {
    		this.publicadorDao = publicadorDao;
    	public void savePublicador(Publicador publicador) {
    	public Publicador getPublicador(Publicador publicador) {
    		return  publicadorDao.find(publicador.getId());
    I got the exception after adding the @Inject annotation above publicadorDao.

    public class PublicadorDao implements BaseDao {
    	private EntityManager em;
    	public void persist(BaseEntity baseEntity) {
    	public void merge(BaseEntity baseEntity) {
    	public void delete(BaseEntity baseEntity) {
    	public Publicador find(Integer id) {
    		return em.find(Publicador.class, id);
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns=""
    	<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" />
    	<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 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 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 class=""/>
    	<!-- Exception translation bean post processor -->
      	<beans:bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    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:

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


    • #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")'.


      • #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.