Could not autowire. No beans of ... type found

33,663

Your @Repository is not being picked up (scanned) by <context:component-scan base-package="controller" /> going by what you are showing us. But we would need to see the packages for each class. Spring depend on you to tell it what packages to look in for your @Service, @Controller, @Repository and @Component.

Have a look at how component scanning works in spring.

Share:
33,663
Charlie Harper
Author by

Charlie Harper

Updated on July 05, 2022

Comments

  • Charlie Harper
    Charlie Harper almost 2 years

    can you help me solve why i can't autowire a class?? class UserDaoImpl:

    @Repository
    public class UserDaoImpl implements UserDao {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        @Override
        public void addUser(User user) {
            sessionFactory.getCurrentSession().save(user);
        }
    
        @Override
        public List<User> getUsers() {
            return sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM user").list();
        }
    
    }
    

    i want this class autowire into other class:

    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserDao userDao; //here is the error
    
        @Override
        public void addUser(User user) {
    
        }
    
        @Override
        public List<User> getUsers() {
            return null;
        }
    
    }
    

    Controller:

    @Controller
    public class UserController {
    
        @Autowired
        UserService userService;
    
        String message = "This should be a list of users";
    
        @RequestMapping("/user")
        public ModelAndView showMessage() {
            ModelAndView modelAndView = new ModelAndView("user");
            modelAndView.addObject("message", message);
            return modelAndView;
        }
    
    }
    

    where can be the problem? should i show you more files? thanks. EDIT: ok so this is my session-factory.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                    <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
                    <prop key="hibernate.connection.url">jdbc:postgresql://localhost:8080/****</prop>
                    <prop key="hibernate.connection.username">****</prop>
                    <prop key="hibernate.connection.password">****</prop>
                    <prop key="hibernate.current_session_context_class">thread</prop>
                    <prop key="show_sql">true</prop>
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                    <value>model.User</value>
                </list>
            </property>
        </bean>
    
     </beans>
    

    -class User is simple POJO which is mapped with hibernate. And maybe web.xml can help us :)

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
    
        <display-name>Archetype Created Web Application</display-name>
    
        <servlet>
            <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>mvc-dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    </web-app>
    

    mvc-servlet-dispatcher.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
            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:component-scan base-package="controller" />
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/pages/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans>
    

    and i don't have any applicationContext.xml file, maybe this can be the problem, can you help me with that? I'm adding a screenshot of my project and problem: http://screenshot.cz/75QVH/

  • ebell
    ebell almost 10 years
    Just saw your package structure, you should clean this up and have com.foo.web, com.foo.service etc and then have your <context:component-scan base-package="controller" /> set to <context:component-scan base-package="com.foo" /> and it should pick up your Repository classes.
  • Charlie Harper
    Charlie Harper almost 10 years
    Yes i found it too, thanks, error dissapeared, but i'm still getting exceptions which starts with: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is...
  • ebell
    ebell almost 10 years
    Need to see your controller as that is now where your problem is. Also the exception stack trace would help.
  • Charlie Harper
    Charlie Harper almost 10 years
    my controller is very simple, just to show a message, because actually my database is empty and i just want to set up base application, but i added it to my post, and here's full stack trace: pastebin.com/tjfR1vMJ
  • ebell
    ebell almost 10 years
    Looks like the org.hibernate.SessionFactory bean is missing. So it looks like the session-factory.xml is NOT being read. You can do this a couple of ways, one is to import session-factory.xml in the mvc-servlet-dispatcher.xml or just copy sessionFactory bean into the mvc-servlet-dispatcher.xml file
  • Charlie Harper
    Charlie Harper almost 10 years
    i imported content of session-factory.xml into that xml file, but still tones of errors, im desperated :/ stack trace here... pastebin.com/FmHEWfw9
  • ebell
    ebell almost 10 years
    Looks like you have a problem connecting to your Postgres database, in the log you are getting "PSQLException: The connection attempt failed" do you have Postgres running and can you connect to it using pgAdmin with the setting you have defined in the sessionFactory above?
  • Charlie Harper
    Charlie Harper almost 10 years
    Oh my god it seems the problem was on first error = i was starting 64 bit tomcat on 32 bit machine, its solved probably, thanks so much for help, but i still don't get it why autowiring wasn't working only for session factory..