action中service提供了get set方法 在set方法里面打印service是可以打印出来service和this.service都可以打印出来applicationContext.xml部分配置<!-- 准备hibernateTemplate -->
<bean id="hibernateTemplate" 
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean id="userService" class="com.service.impl.UserServiceImpl">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
<bean id="loginregAction" class="com.action.LoginRegAction">
<property name="userService">
<ref bean="userService"/>
</property>
</bean>struts2.xml配置<package name="default" namespace="/" extends="struts-default">
<action name="loginregAction" class="com.action.LoginRegAction">
<!--<result type="chain" name="success">userWeiboList</result>
-->
<result name="success">/home.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
public class LoginRegAction extends ActionSupport {
private UserServiceIf userService;

private String name;
private String password;


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserServiceIf getUserService() {
return userService;
}
public void setUserService(UserServiceIf userService) {
System.out.println("set userService---"+userService);//可以打印出来(在tomcat启动的时候会打印)
this.userService = userService;
System.out.println("set this.userService---"+this.userService);//可以打印出来在tomcat启动的时候会打印)
}


@Override
public String execute() throws Exception {
try {
System.out.println("userService------->"+userService);//这里一直是null 求解
//userService=new UserServiceImpl();
//System.out.println("new userService ====>"+userService);
User user=userService.login(name, password);
Map session =ActionContext.getContext().getSession();
session.put("user",user);
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
我用junit4测试了下service,测试是可以通过
ApplicationContext ac = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml"); 
private UserServiceIf userService=(UserServiceIf) ac.getBean("userService");
@Test
public void testLogin(){
System.out.println(userService);
User user=userService.login("a", "b");
System.out.println(user.getSex());//可以打印user的sex
}

解决方案 »

  1.   

    action中也用JUnit的方法获得service
      

  2.   

     <bean id="loginregAction" class="com.action.LoginRegAction">
            <property name="userService">
                <ref bean="userService"/>
            </property>
        </bean>
    struts的action要交给spring管理在调用action的时候才会通过spring注入
      

  3.   


    <package name="default" namespace="/" extends="struts-default">
            <action name="loginregAction" class="LoginRegAction">
                <!--<result type="chain" name="success">userWeiboList</result>
                -->
                <result name="success">/home.jsp</result>
                <result name="error">/error.jsp</result>
            </action>
        </package>
    class="com.action.LoginRegAction" 这个地方填入的是被拖管对象
    而不是填入类这样  action才被spring管理
    <bean id="LoginRegAction" class="com.action.LoginRegAction">
    <property name="loginService" ref="LoginService" />
    </bean>
    看你是新手跟定很着急 好好看教程 跟着做别找个帖子或者 找了例子就上手
      

  4.   

    [Quote=引用 4 楼 ollim 的回复:]
    class="com.action.LoginRegAction" 这个地方填入的是被拖管对象
    而不是填入类这样 action才被spring管理
    <bean id="LoginRegAction" class="com.action.LoginRegAction">
    <property name="loginService" ref="LoginService" />
    </bean>我那样填写整个类名也是可以的,我知道是填写那个对象也就是bean里面那个id