向service里面注入dao。为什么注入不进?有空的帮个忙看看。
就是在下面service里面,发觉userDao为null.为什么不能注入呢?兄弟们帮帮忙看看。
public class LoginServiceImpl implements LoginService {
private UserDAO userDao;
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
public User findUserById(String userId) {
User user = userDao.findUserById(userId);
return user;
}
}
applicationContext.xml:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN " "http://www.springframework.org/dtd/spring-beans.dtd ">
<beans default-autowire= "byName ">
<bean id= "sessionFactory " class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean ">
<property name= "configLocation ">
<value> classpath:hibernate.cfg.xml </value>
</property>
</bean>
<bean id= "loginAction " class= "freeunite.action.LoginAction " />
<bean id= "logoutAction " class= "freeunite.action.LogoutAction " />
<bean id= "userDao " class= "freeunite.dao.UserDAOImpl " singleton= "false ">
<property name= "sessionFactory ">
<ref bean= "sessionFactory " />
</property>
</bean>
web.xml:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app xmlns= "http://java.sun.com/xml/ns/j2ee "
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " version= "2.4 "
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
<filter>
<filter-name> encodingFilter </filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> UTF-8 </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> encodingFilter </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
<filter>
<filter-name> hibernateFilter </filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name> hibernateFilter </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value>
applicationContext.xml
</param-value>
</context-param>
<filter>
<filter-name> struts2 </filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name> struts2 </filter-name>
<url-pattern> /* </url-pattern>
<dispatcher> REQUEST </dispatcher>
<dispatcher> FORWARD </dispatcher>
</filter-mapping>
<filter>
<filter-name> struts-cleanup </filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name> struts-cleanup </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file> index.jsp </welcome-file>
</welcome-file-list>
</web-app>
[解决办法]
你所有的代码中没有对LoginServiceImpl 的成员变量userDao进行注入。
可以applicationContext.xml中加入
<bean id= "loginServiceImpl " class= "LoginServiceImpl " >
<property name= "userDao ">
<ref bean= "userDao " />
</property>
</bean>
[解决办法]
楼主在srping配置文件中没有配置注入DAO。。。
楼上的方法正解!!!
不过你的spring配置文件中少好多事务方面的配置吧!
[解决办法]
事务配置不是必须的
[解决办法]
anhy(鎏) ( )
--------------
正解