在做Struts2和Spring整合时遇到Spring实例化无效的情况,Action中代码如下:
public class UserAction extends ActionSupport {
@Resource
private UserService userService;
public String execute(){
//userService.saveUser(new Object());
System.out.println(userService);
System.out.println("struts2spring整合成功");
return "success";
}
}
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: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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 自动扫描与装配bean -->
<context:component-scan base-package="com.bjwl"></context:component-scan>
</beans>
通过注解实例化UserService时一直得到的是null。最后经过查找,发现没有导入Struts2-Spring-plugin.jar的原因。