lxl_java 2017-04-22 10:50 采纳率: 0%
浏览 1119

Spring 注入service问题

struts2与spring整合,action 未注册为bean,service却能自动注入,哪位大神能讲一下是怎么注入的

代码如下:
action:

public class UserAction extends ActionSupport implements ModelDriven<User> {

    private User user = new User();

    private UserService userService;

    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    // 登录
    public String login() throws Exception {
        System.out.println("--login--");
        userService.getUsersCount();
        return "fail";
    }

    private String ids;

    /*
     * (non-Javadoc)
     * 
     * @see com.opensymphony.xwork2.ModelDriven#getModel()
     */
    @Override
    public User getModel() {
        return user;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user
     *            the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }



    /**
     * @return the ids
     */
    public String getIds() {
        return ids;
    }

    /**
     * @param ids the ids to set
     */
    public void setIds(String ids) {
        this.ids = ids;
    }
} 

service接口


public interface UserService {

    int getUsersCount();
}

service实现类
@Component("userService")
public class UserServiceImpl implements UserService {

@Override
public int getUsersCount() {
    System.out.println(1111);
    return 0;
}

}
applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 使用annotation的方式注入 -->
    <context:annotation-config />
    <context:component-scan base-package="com.bawei" />


</beans>

struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
<!--    <constant name="struts.objectFactory" value="spring"></constant> -->

    <package name="default" namespace="/user" extends="struts-default">

        <global-results>
            <result name="list" type="redirect">/user/User_users</result>
        </global-results>

        <action name="*_*" class="com.bawei.action.{1}Action" method="{2}">
            <result name="fail">/WEB-INF/view/fail.jsp</result>
            <result name="registerSuccess">/WEB-INF/view/success.jsp</result>
            <result>/WEB-INF/view/{1}_{2}.jsp</result>
        </action>
    </package>

</struts>

  • 写回答

2条回答 默认 最新

  • 御前提笔小书童 博客专家认证 2017-04-23 00:55
    关注

    private UserService userService;

    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题