这是我的applicationContext.xml
<!-- 把Dao注入给Service -->
<bean id="userService" class="com.allen.service.spring.UserServiceImp">
<property name="userDao">
<ref bean="userDao" />
</property>
</bean>
<!-- 把service注入给Action -->
<bean id="userAction" class="com.allen.action.UserAction">
<property name="userService">
<ref bean="userService"/>
</property>
</bean>
这是我的action
ublic class UserAction extends MappingDispatchAction{
    private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public ActionForward login(ActionMapping mapping,ActionForm form,
     HttpServletRequest request,HttpServletResponse response){
String name = request.getParameter("loginName");
String passwd = request.getParameter("loginPassword");
System.out.print(this.getUserService());//这里打印的是空
     if(this.getUserService().userLogin(name,passwd)){//空指针
     return mapping.findForward("succcess");
     }
     return mapping.findForward("failed");
    }
}不是在spring的配置文件中 把service注入到action 中 spring会自动创建 service的实力 为什么得到的会是null》??

解决方案 »

  1.   

    有啊 在userService的实现类中有 
      

  2.   

     Spring配置里userDao那个实现类的配置写进去了嘛?
      

  3.   

    spring是在web.xml里配置的吗?是不是没有初始化配置文件你可以直接写一个main函数getbean看看能不能获取service
      

  4.   

    <?xml version="1.0" encoding="GB2312"?>
    <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-2.0.xsd">    <!-- 注入数据源 -->
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.mysql.jdbc.Driver">
    </property>
    <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
    </bean>
    <!-- 注入sessionFactory -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    </props>
    </property>
    <property name="mappingResources">
    <list>
    <value>com/allen/model/User.hbm.xml</value></list>
    </property></bean>
    <!-- 把Dao注入给 sessionFactory -->
    <bean id="userDao" class="com.allen.dao.hib.UserDAOImp">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <!-- 把Dao注入给Service -->
    <bean id="userService" class="com.allen.service.spring.UserServiceImp">
    <property name="userDao">
    <ref bean="userDao" />
    </property>
    </bean>
    <!-- 把service注入给Action -->
    <bean id="userAction" class="com.allen.action.UserAction">
    <property name="userService">
    <ref bean="userService"/>
    </property>
    </bean>
    </beans>这是整个spring的配置文件  是不是 web.xml中需要配置什么啊?
      

  5.   

    啊 web.XML配置好了 又出新问题了 呵呵 我再看看 +分怎么加啊?
      

  6.   

    严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: entity class not found: User
    Caused by: org.hibernate.MappingException: entity class not found: User这是我的hibernate 配置文件<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="User" table="t_users" catalog="test">
            <id name="id" type="java.lang.Long">
                <column name="id" />
                <generator class="native" />
            </id>
            <property name="name" type="java.lang.String">
                <column name="name" length="32" />
            </property>
            <property name="loginName" type="java.lang.String">
                <column name="login_name" length="10" />
            </property>
            <property name="loginPassword" type="java.lang.String">
                <column name="login_password" length="32" not-null="true" />
            </property>
            <property name="status" type="java.lang.Integer">
                <column name="status" not-null="true" />
            </property>
            <property name="phone" type="java.lang.String">
                <column name="phone" length="32" />
            </property>
            <property name="email" type="java.lang.String">
                <column name="email" length="32" />
            </property>
            <property name="enrollDate" type="java.util.Date">
                <column name="enroll_date" length="10" />
            </property>
            <property name="closeDate" type="java.util.Date">
                <column name="close_date" length="10" />
            </property>
            <property name="paymentStyle" type="java.lang.Integer">
                <column name="payment_style" />
            </property>
            <property name="career" type="java.lang.String">
                <column name="career" length="32" />
            </property>
            <property name="nationality" type="java.lang.String">
                <column name="nationality" length="32" />
            </property>
            <property name="gender" type="java.lang.String">
                <column name="gender" length="8" />
            </property>
            <property name="company" type="java.lang.String">
                <column name="company" length="32" />
            </property>
            <property name="address" type="java.lang.String">
                <column name="address" length="32" />
            </property>
            <property name="postCode" type="java.lang.String">
                <column name="post_code" length="16" />
            </property>
        </class>
    </hibernate-mapping>
      

  7.   

    没有找到实体类USER.  
    SPRING中你没把ORMAPPING那个映射配置文件写进去
      

  8.   

     那个USER类有包路劲吗? 
      

  9.   

    Caused by: org.hibernate.MappingException: entity class not found: User        
      

  10.   

    property name="mappingResources"> 
    <list> 
    <value>com/allen/model/User.hbm.xml </value> </list> 
    </property>
    这一句不是配置进去了吗
      

  11.   

    在你的<hibernate-mapping package="包名">