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>