struts2和spring整合后通过new ClassPathXmlApplicationContext("applicationContext.xml")测试可以注入到Action但是部署到Tomcat上之后页面请求时没有注入到Action,请各位高手指点。谢谢喽...
    1.Action层
[code=Jav]
         @Controller("fwxxAction")
public class FwxxAction {

private FwxxBiz fwxxBiz;

@Resource
public void setFwxxBiz(FwxxBiz fwxxBiz) {
this.fwxxBiz = fwxxBiz;
}

/**
 * 查询所有的房屋信息
 */
public String toFwxx() {
List fwxxList = fwxxBiz.findAll();
System.out.println(fwxxList.size());
return "index";
}
}[/code]
      2.Biz层[code=Jav]
         @Service("fwxxBiz")
public class FwxxBizImpl implements com.xaccp.rent.biz.FwxxBiz {

private FwxxDao fwxxDao;

@Resource
public void setFwxxDao(FwxxDao fwxxDao) {
this.fwxxDao = fwxxDao;
} public List findAll() {
return fwxxDao.findAll();
}
}
[/code]
      3.Dao层[code=Jav]
         @Component("fwxxDao")
public class FwxxDaoImpl implements FwxxDao {

@PersistenceContext(unitName = "zfUnit")
private EntityManager em; public List findAll() {
return em.createQuery("from Fwxx").getResultList();
}
}
[/code]
     4.persistence.xml[code=Jav]
<persistence-unit name="zfUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<class>com.xaccp.rent.model.Fwlx</class>
<class>com.xaccp.rent.model.Fwxx</class>
<class>com.xaccp.rent.model.Jd</class>
<class>com.xaccp.rent.model.Qx</class>
<class>com.xaccp.rent.model.User</class> <!-- 数据库连接信息 -->
<properties>
<property name="hibernate.connection.url"
value="jdbc:sqlserver://localhost:1433;databaseName=zf" />
<property name="hibernate.connection.driver_class"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
</properties>
</persistence-unit>
[/code]
5.applicationContext.xml[code=Jav]
<context:annotation-config />
<context:component-scan base-package="com.xaccp.rent" /> <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="zfUnit" />
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
</bean> <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://localhost:1433; databaseName=zf" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.xaccp.rent.model</value>
</list>
</property>
</bean> <bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
autowire="byName" /> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED"
read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="bizMethod"
expression="execution(* com.xaccp.rent.biz.impl.*.*(..))" />
<aop:advisor pointcut-ref="bizMethod" advice-ref="txAdvice" />
</aop:config>
[/code]
6.web.xml[code=Jav]
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
<!-- com.xaccp.rent.util.MyContextLoaderListener -->
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <filter>
<filter-name>sessionManager</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionManager</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
[/code]

解决方案 »

  1.   

    struts2和spring整合后通过new ClassPathXmlApplicationContext("applicationContext.xml")测试可以注入到Action。但是部署到Tomcat上之后页面请求时没有注入到Action,请各位高手指点。谢谢喽... 1.Action层[code=Jav]
    @Controller("fwxxAction")
    public class FwxxAction {

    private FwxxBiz fwxxBiz;

    @Resource
    public void setFwxxBiz(FwxxBiz fwxxBiz) {
    this.fwxxBiz = fwxxBiz;
    }

    /**
     * 查询所有的房屋信息
     */
    public String toFwxx() {
    List fwxxList = fwxxBiz.findAll();
    System.out.println(fwxxList.size());
    return "index";
    }
    }[/code]
    2.Biz层[code=Jav]
    @Service("fwxxBiz")
    public class FwxxBizImpl implements com.xaccp.rent.biz.FwxxBiz {

    private FwxxDao fwxxDao;

    @Resource
    public void setFwxxDao(FwxxDao fwxxDao) {
    this.fwxxDao = fwxxDao;
    } public List findAll() {
    return fwxxDao.findAll();
    }
    }[/code]
    3.Dao层[code=Jav]
    @Component("fwxxDao")
    public class FwxxDaoImpl implements FwxxDao {

    @PersistenceContext(unitName = "zfUnit")
    private EntityManager em; public List findAll() {
    return em.createQuery("from Fwxx").getResultList();
    }
    }[/code]
    4.persistence.xml[code=Jav]
    <persistence-unit name="zfUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>com.xaccp.rent.model.Fwlx</class>
    <class>com.xaccp.rent.model.Fwxx</class>
    <class>com.xaccp.rent.model.Jd</class>
    <class>com.xaccp.rent.model.Qx</class>
    <class>com.xaccp.rent.model.User</class> <!-- 数据库连接信息 -->
    <properties>
    <property name="hibernate.connection.url"
    value="jdbc:sqlserver://localhost:1433;databaseName=zf" />
    <property name="hibernate.connection.driver_class"
    value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="hibernate.connection.username" value="sa" />
    <property name="hibernate.connection.password" value="" />
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <!-- <property name="hibernate.hbm2ddl.auto" value="create" /> -->
    </properties>
    </persistence-unit>[/code]
    5.applicationContext.xml[code=Jav]
    <context:annotation-config />
    <context:component-scan base-package="com.xaccp.rent" /> <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="zfUnit" />
    <property name="persistenceXmlLocation"
    value="classpath:META-INF/persistence.xml" />
    </bean> <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url"
    value="jdbc:sqlserver://localhost:1433; databaseName=zf" />
    <property name="username" value="sa" />
    <property name="password" value="" />
    </bean> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    </props>
    </property>
    <property name="packagesToScan">
    <list>
    <value>com.xaccp.rent.model</value>
    </list>
    </property>
    </bean> <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    autowire="byName" /> <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="find*" propagation="REQUIRED"
    read-only="true" />
    <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="bizMethod"
    expression="execution(* com.xaccp.rent.biz.impl.*.*(..))" />
    <aop:advisor pointcut-ref="bizMethod" advice-ref="txAdvice" />
    </aop:config>[/code]
    6.web.xml[code=Jav]
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    <!-- com.xaccp.rent.util.MyContextLoaderListener -->
    </listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param> <filter>
    <filter-name>sessionManager</filter-name>
    <filter-class>
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>sessionManager</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping> <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    org.apache.struts2.dispatcher.FilterDispatcher
    </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>[/code]
      

  2.   

    看了下你的service和action,在action中,我通常是把@Resource写在实例变量前,如下:
    @Resource
    private FwxxBiz fwxxBiz; 
    另外你的BIZ中少了@Transactional,可能将导致数据库操作无法commit。
      

  3.   

    我是通过spring配置文件来管理事务的,还有就是最关键就是Action中无法注入Biz
      

  4.   

    但是可以通过AopplicationContext的getBean()方法可以拿到Action,结果是Biz注入值了,就是部署到Tomcat上之后无法注入,原因????????服务启动不出错...