首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

基于Annotation的Struts2+Spring3, Spring无法向acition中注入service解决思路

2013-10-21 
基于Annotation的Struts2+Spring3, Spring无法向acition中注入service我在整合环境 Struts2.2.1 + Spring3

基于Annotation的Struts2+Spring3, Spring无法向acition中注入service


我在整合环境 Struts2.2.1 + Spring3.1.2 时,我的UserAction 的第16行user = userService.getUserByID("1");
userService始终为null,报NullPointerException ,不知如何回事,请大侠帮忙看看,谢谢!

所用lib:
   aopalliance-1.0.jar
   aspectjrt-1.7.0.jar
   commons-fileupload-1.2.1.jar
   commons-io-1.3.2.jar
   commons-logging-1.1.1.jar
   freemarker-2.3.16.jar
   javassist-3.11.0.GA.jar
   log4j-1.2.14.jar
   ognl-3.0.jar
   spring-aop-3.1.2.RELEASE.jar
   spring-asm-3.1.2.RELEASE.jar
   spring-beans-3.1.2.RELEASE.jar
   spring-context-3.1.2.RELEASE.jar
   spring-core-3.1.2.RELEASE.jar
   spring-expression-3.1.2.RELEASE.jar
   spring-jdbc-3.1.2.RELEASE.jar
   spring-orm-3.1.2.RELEASE.jar
   spring-tx-3.1.2.RELEASE.jar
   spring-web-3.1.2.RELEASE.jar
   struts2-convention-plugin-2.2.1.jar
   struts2-core-2.2.1.jar
   xwork-core-2.2.1.jar


---web.xml------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>myWebApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
    
 
         <!-- 配置spring的监听器 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:applicationContext*.xml</param-value>
 </context-param> 
 <!-- 开启监听 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

        <!-- 配置Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>



---Action-------------------------------------------------------------

1 package com.ibm.action;
2 import javax.annotation.Resource;
3 import org.apache.struts2.convention.annotation.Action;
4 import com.opensymphony.xwork2.ActionSupport;
5 import com.ibm.service.UserService;
6 import com.ibm.User;
7 public class UserAction extends ActionSupport {


8     
9     private User user;
10
11     @Resource(name="userService")
12     private UserService userService;
13
14     @Action("/user/detail")
15     public String detail() throws Exception {
16         user = userService.getUserByID("1");
17   return SUCCESS;
18     }
19 }




---Service------------------------------------------------------------

package com.ibm.service;
import com.ibm.User;
public interface UserService {
User getUserByID(String id);
}


package com.ibm.service.impl;

import org.springframework.stereotype.Service;
import com.ibm.User;
import com.ibm.service.UserService;

@Service("userService") 
public class UserServiceImpl implements UserService {  

public User getUserByID(String id) {
User user = new User();
user.setAge(20);
user.setUserName("mike");
return user;
}

}


---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:tx="http://www.springframework.org/schema/tx" 
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    ">

 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
 <context:component-scan base-package="com.ibm.service" /><!-- 扫描包 -->
 <context:annotation-config/>  
           <!--问题应该出在这个xml,但不知如何解决-->
</beans>
[解决办法]
如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

把你的applicationContext.xml里的
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>换成
<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>或者
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 
试试~
[解决办法]
<context:component-scan base-package="com.ibm.service" />


改为
<context:component-scan base-package="com.ibm" />
[解决办法]
<?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:tx="http://www.springframework.org/schema/tx" 
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  ">
  
  <mvc:annotation-driven />
  <context:annotation-config/>
  <context:component-scan base-package="com.ibm" />
  
  
[解决办法]
@Controller  //------ 这要加这个
public class UserAction extends ActionSupport {
8  
9 private User user;
10
11 @Resource(name="userService")
12 private UserService userService;
13
14 @Action("/user/detail")
15 public String detail() throws Exception {
16 user = userService.getUserByID("1");
17 return SUCCESS;
18 }
19 }
[解决办法]
Resource改成Autowired试试
[解决办法]
网页打开404你懂的,如何用mvn命令下载,求指教~基于Annotation的Struts2+Spring3, Spring无法向acition中注入service解决思路
[解决办法]
你的Action类有问题!!
7 public class UserAction extends ActionSupport {
8   
9 private User user;
10
11 @Resource(name="userService")
12 private UserService userService;
13
14 @Action("/user/detail")
15 public String detail() throws Exception {
16 user = userService.getUserByID("1");
17 return SUCCESS;
18 }

 这个你们也太粗心了,声明了变量,为什么不给他们付get 和set方法,这样才能和前台相互传递值啊,否则当然会空指针,仔细想想基础问题吧

热点排行