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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With