请大家先看下我的代码,问题是我的action中取spring注入的service始终是null,就是下面action中的代码红色注释部份,谢谢.我的接口及service如下,方法的代码省略
//接口
public interface DAO {//方法}//实现DAO接口的抽象类
@Transactional
public abstract class DaoSupport implements DAO {//实现}//service接口
public interface CategoryService extends DAO{}//实现service接口的类
@Service
@Transactional
public class CategoryServiceImpl extends DaoSupport implements CategoryService{}然后我的action如下
@Controller
public class IndexAction extends ActionSupport {
@Resource(name="categoryServiceImpl")
private CategoryService categoryService ;

public String myTest() throws Exception {
categoryService.getScrollResult(Category.class);//调用的时候报空指针
System.out.println(categoryService);
return SUCCESS;
}
}
我的spring配置如下
<?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:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 添加自动扫描 -->
<context:component-scan base-package="biz.sihe"/>

<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>Struts.xml配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="categoryServiceImpl" extends="struts-default" namespace="/">
<action name="myindex" class="biz.sihe.web.action.IndexAction" method="myTest">
<result name="success">/WEB-INF/page/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>

</struts>
    
web.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <welcome-file-list>
    <welcome-file>index.action</welcome-file>
  </welcome-file-list>
  
  <filter>
   <filter-name>struts2</filter-name>
   <!--
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
   -->
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
        
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <filter>   
     <filter-name>Spring character encoding filter</filter-name>   
           <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
        <init-param>   
            <param-name>encoding</param-name>   
            <param-value>GBK</param-value>   
        </init-param>   
    </filter>   
    
    <filter-mapping>   
        <filter-name>Spring character encoding filter</filter-name>   
        <url-pattern>/*</url-pattern>   
    </filter-mapping>
    
  
   <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  
</web-app>我的jar包如下
antlr-2.7.6.jar
cglib-2.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
commons-collections-3.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
freeer-2.3.15.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jsf-api.jar
jsf-impl.jar
jstl-1.2.jar
jta-1.1.jar
mysql-connector-java-3.1.13-bin.jar
ognl-2.7.3.jar
org.springframework.aop-3.0.2.RELEASE.jar
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.aspects-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.context.support-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expression-3.0.2.RELEASE.jar
org.springframework.instrument-3.0.2.RELEASE.jar
org.springframework.instrument.tomcat-3.0.2.RELEASE.jar
org.springframework.jdbc-3.0.2.RELEASE.jar
org.springframework.jms-3.0.2.RELEASE.jar
org.springframework.orm-3.0.2.RELEASE.jar
org.springframework.oxm-3.0.2.RELEASE.jar
org.springframework.test-3.0.2.RELEASE.jar
org.springframework.transaction-3.0.2.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar
org.springframework.web.portlet-3.0.2.RELEASE.jar
org.springframework.web.servlet-3.0.2.RELEASE.jar
org.springframework.web.struts-3.0.2.RELEASE.jar
slf4j-api-1.5.8.jar
slf4j-nop-1.5.8.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar

解决方案 »

  1.   

    然后我的action如下
    @Controller
    public class IndexAction extends ActionSupport {
    @Resource(name="categoryServiceImpl")
    private CategoryService categoryService ;没有加set方法啦 所以为NULL了。。
      

  2.   

    自动注入不需要set方法吧,我增加了get和set方法后还是为null
      

  3.   

    没有set应该直接抛异常说找不到setxxx的方法喔.!  这里应该是它省略了set
      

  4.   

    找来去,发现我少拷贝了一个struts2-spring-plugin-2.1.8.1.jar文件,没有将struts托管给spring,郁闷死了,我老是去找代码问题,结果不是代码问题
      

  5.   

    ~~~汗. 找问题基本都不看包的.!  竟然少包了.! 那这个LZ就是你自己粗心的问题了.!整合Spring你不加怎么可以捏.!
      

  6.   

    偶还以为是service层的注入有问题呢。原来是包呀……