spring+hibernate不能通过拿到DAO对象
applicationContext.xml代码为:
<bean id="userDAO" class="com.ggxy.model.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userService" class="com.ggxy.service.UserService">
<property name="userDAO" ref="userDAO"></property>
</bean>
我已经写了依赖
service的代码为:
public class UserService {
private UserDAO userDAO;
public void save(User u) {
userDAO.save(u);
}
public List<User> findByUser(String name) {
List<User> lists = userDAO.findByUsername(name);
return lists;
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
action的代码为:
public class LoadAction extends BaseAction{
private String userName;
private String passWord;
private String pic;
private UserService userService;
public String pic() {
Image image = new Image();
try {
image.getImage(0, 0, response.getOutputStream());
} catch (
IOException e) {
e.printStackTrace();
}
return "success";
}
public String add() {
List<User> lists = userService.findByUser(userName);
if(lists == null || lists.size() < 1) {
User u = new User();
u.setUsername(userName);
u.setPassword(passWord);
u.setPic(pic);
userService.save(u);
return "success";
} else {
return "error";
}
}
}
我已经写有UserService的set、get方法,在此处省略.....
异常为:
java.lang.NullPointerException
com.ggxy.action.LoadAction.add(LoadAction.java:27)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)下面太多了,就不弄上去了。
求大神解救去,弄了几天还是不行,
空指针的原因是拿不到DAO对象,但是上面的配置都写好了,为什么就是拿不到啊,拿不到
spring
hibernate
dao
bean
------解决方案--------------------要想自动装配需要有struts与spring的插件包,如:struts2-spring-plugin-2.2.1.1.jar