struts2 - 没有struts2-spring插件的spring集成

时间:2011-12-29 08:02:39

标签: spring struts2 dynamic-proxy struts2-spring-plugin

我想知道如何在不使用struts-spring插件的情况下将spring bean( PersonService )对象分配给 setPersonService 方法,我得到了 JdkDynamicAopProxy 我尝试跟踪动作类方法时的关键字( setPersonService )。但我对 JdkDynamicAopProxy

一无所知

Struts Action

 public class AllPersonsFinder extends ActionSupport {
        private List<Person> persons;
        private transient PersonService personService;

        @Override
        public String execute() throws Exception {
            persons = personService.findAllEmployees();
            return SUCCESS;
        }

        public List<Person> getPersons() {
            return persons;
        }

        public void setPersons(List<Person> persons) {
            this.persons = persons;
        }

        public PersonService getPersonService() {
            return personService;
        }

        public void setPersonService(PersonService personService) {
            this.personService = personService;
        }
    }

Struts config

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="springjpaexample" extends="struts-default">

        <interceptors>
            <interceptor-stack name="appDefaultStack">
                <interceptor-ref name="defaultStack">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="appDefaultStack" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>


        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="error" />
        </global-exception-mappings>

        <action name="personFinder"
            class="edu.ku.it.si.springjpaexample.action.PersonFinder" method="execute">
            <result name="success">/personinfo.jsp</result>
        </action>

        <action name="allPersonsFinder"
            class="edu.ku.it.si.springjpaexample.action.AllPersonsFinder" method="execute">

            <result name="success">/personsinfo.jsp</result>

        </action>

        <action name="*PersonUpdate"
            class="edu.ku.it.si.springjpaexample.action.PersonUpdater" method="{1}">

            <result name="input">/inputpersonupdate.jsp</result>
            <result name="success">/personupdated.jsp</result>

        </action>

        <action name="personDelete"
            class="edu.ku.it.si.springjpaexample.action.PersonDeleter" method="execute">

            <result name="success">/persondeleted.jsp</result>

        </action>

        <action name="*PersonSave" class="edu.ku.it.si.springjpaexample.action.PersonSaver"
            method="{1}">

            <result name="input">/inputpersonsave.jsp</result>
            <result name="success">/personsaved.jsp</result>

        </action>

    </package>

</struts>

Spring Bean

@Service("personService")
public class PersonServiceImpl implements PersonService {

     @Autowired
     private PersonDao personDao;

    @Override
    public Person findbyEmplid(Long emplid) {
        return personDao.findbyEmplid(emplid);
    }

    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Override
    public List<Person> findAllEmployees() {
        return personDao.findAllEmployees() ;
    }

    @Override
    @Transactional
    public void update(Person person) {
        personDao.update(person);
    }

    @Override
    @Transactional
    public void delete(Person person) {
        personDao.delete(person);
    }

    @Override
    @Transactional
    public void save(Person person) {
        personDao.save(person); //Person object's emplid instance field will now have a value
    }
}

Spring config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/jdbc
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- scans the classpath for annotated components (including @Repostory 
    and @Service  that will be auto-registered as Spring beans  -->          
    <context:component-scan base-package="edu.ku.it.si.springjpaexample" />

    <!-- methods or classes needing to run in a complete transaction will
    be annotated with Transactional -->
    <tx:annotation-driven />

    <!-- Creates a data source that can provide a connection to in-memory embedded database populated 
    with test data
    see: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ch12s08.html   -->
   <jdbc:embedded-database id="dataSource">
        <jdbc:script location="classpath:schema.sql"/>
        <jdbc:script location="classpath:test-data.sql"/>
    </jdbc:embedded-database>


    <!-- This will ensure that hibernate or jpa exceptions are automatically translated into
         Spring's generic DataAccessException hierarchy for those classes annotated with Repository
         For example see PersonDaoJpa-->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <!-- JPA Entity Manager Factory -->
    <bean id="entityManagerFactory" 
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:dataSource-ref="dataSource"
          p:persistenceXmlLocation="META-INF/persistence.xml" 
          p:persistenceUnitName="springJpaPersistenceUnit" />          


    <!-- bean post-processor for JPA annotations -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


    <!-- Transaction Config -->
    <bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"/>

    <!-- use declarative transaction management  -->
    <tx:annotation-driven  transaction-manager="transactionManager"/>

 </beans>

0 个答案:

没有答案