Struts2 和 Spring2 集成
英文內(nèi)容來源:http://struts.apache.org/2.0.6/docs/spring-plugin.html
![]() |
Spring Spring 是一個(gè)輕量級容器,它用“依賴注入”的技術(shù)提供集成和自動的應(yīng)用對象配置。 |
The Spring Plugin works by overriding the Struts ObjectFactory to enhance the creation of core framework objects. When an object is to be created, it uses the class attribute in the Struts configuration to correspond to the id attribute in the Spring configuration. If not found, the class will try to be created as usual, then be
autowired by Spring. In the case of Actions, Spring 2's bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.
Spring 插件在Struts ObjectFactory上面運(yùn)行,能增強(qiáng)核心框架對象的創(chuàng)建能力。當(dāng)一個(gè)對象被創(chuàng)建時(shí),這個(gè)對象會使用Struts 配置的類屬性來裝配Spring配置里的id屬性。如果找不到類,Spring會設(shè)法按常規(guī)裝配地創(chuàng)建一個(gè)。在Action里,Spring 2 的bean scope feature(bean的范圍特性)用來控制Action實(shí)例在session、application的使用范圍,或控制Action實(shí)例的自定義的使用范圍,以便在默認(rèn)的請求范圍基礎(chǔ)上提供高級的定制化服務(wù)。
![]() |
Spring Actions are Optional! Spring Actions只是可選項(xiàng) Remember: registering Actions with Spring is not required. The Spring alternative is there if you need it, but the framework will automatically create Actions objects from the action mappings. But, if you want to use Spring to inject your Actions, the option is there. |
Features
特性
- Allow Actions, Interceptors, and Results to be created by Spring
- Struts-created objects can be autowired by Spring after creation
- Provides two interceptors that autowire actions, if not using the Spring
- 允許Spring創(chuàng)建Action、攔截器和記錄集
- Struts創(chuàng)建對象之后自動跟Spring裝配
- 如果不用Spring的ObjectFactory 對象工廠,框架將提供兩個(gè)攔截器來裝配Action。
Usage
用法
To enable Spring integration, simply include struts2-spring-plugin-x-x-x.jar in your application.
為了能和Spring集成,你需要把struts2-spring-plugin-x-x-x.jar放在你的應(yīng)用里。
If you are using more than one object factory, (for example, by including both the Spring and Plexus plugins in your application,) you will need to set the struts.objectFactory property in struts.properties or in one of several XML files via Constant Configuration:
如果你使用一個(gè)以上的對象工廠(比如在你的應(yīng)用中即用Spring又用Plexus),那么你需要在struts.properties 或Constant Configuration的XML文件里設(shè)置struts.objectFactory 屬性:
struts.objectFactory = spring
<struts>
<constant name="struts.objectFactory" value="spring" />
...
</struts>
Autowiring
自動裝配
The framework enables "autowiring" by default. (Autowiring means to look for objects defined in Spring with the same name as your object property). To change the wiring mode, modify the spring.autowire property.
框架里默認(rèn)是自動裝配的(Autowiring自動裝配是指自動尋找Spring用跟你的對象屬性名字一樣定義的對象 )。要改變裝配模式,那么你可以修改spring.autowire 的屬性
裝配模式
struts.objectFactory.spring.autoWire = type
The autowire property can be set to several options.
自動裝配屬性有幾個(gè)選項(xiàng)可設(shè)置。
name | Auto-wire by matching the name of the bean in Spring with the name of the property in your action. This is the default 自動裝配會根據(jù)Spring里的bean的名字和你的Action里的屬性名字來裝配。這個(gè)選項(xiàng)是默認(rèn)值。 |
---|---|
type | Auto-wire by looking for a bean registered with Spring of the same type as the property in your action. This requires you to have only one bean of this type registered with Spring 自動裝配會自動尋找Spring里注冊的跟你的action里類型屬性一樣的bean。這就要求在Spring里只能注冊一個(gè)唯一的類型。 |
auto | Spring will attempt to auto-detect the best method for auto-wiring your action Spring會嘗試自動發(fā)覺裝配你的action的最佳方法。 |
constructor | Spring will auto-wire the parameters of the bean's constructor Spring會自動裝配bean的構(gòu)造器參數(shù)。 |
By default, the framework will at least try to use Spring to create all its objects. If the object cannot be created by Spring, then the framework will create the object itself.
默認(rèn)情況下,框架會設(shè)法用Spring去創(chuàng)建Spring的全部對象。如果Spring不能創(chuàng)建對象,那框架會創(chuàng)建。
Enabling Spring integration for other application objects is a two-step process.
兩個(gè)步驟可使Spring能跟其他應(yīng)用對象集成。
- Configure the Spring listener
設(shè)置Spring監(jiān)聽器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- Register your objects via the Spring configuration
- 通過Spring的配置來注冊你的對象
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<bean id="personManager" class="com.acme.PersonManager"/>
...
</beans>
![]() |
More applicationContext configuration files needed? 還需要其他applicationContext 配置文件嗎? Since the Spring integration uses a standard Listener, it can be configured to support configuration files other than applicationContext.xml. Adding the following to your web.xml will cause Spring's ApplicationContext to be inititalized from all files matching the given pattern: <!-- Context Configuration locations for Spring XML files --> See the Spring documentation for a full description of this parameter. |
Initializing Actions from Spring
從Spring初始化Action
Normally, in struts.xml you specify the class for each Action. When using the default SpringObjectFactory, the framework will ask Spring to create the Action and wire up dependencies as specified by the default auto-wire behavior.
通常情況下,在struts.xml里,你為每一個(gè)Action指定類。當(dāng)使用默認(rèn)的SpringObjectFactory時(shí),框架會要求Spring去創(chuàng)建Action并根據(jù)默認(rèn)的自動裝配行為去指定裝配依賴。
![]() |
We strongly recommend that you find declarative ways of letting Spring know what to provide for your actions. This includes making your beans able to be |
However, sometimes you might want the bean to be completely managed by Spring. This is useful, for example, if you wish to apply more complex AOP or Spring-enabled technologies, such as Acegi, to your beans. To do this, all you have to do is configure the bean in your Spring applicationContext.xml and then change the class attribute from your Action in the struts.xml to use the bean name defined in Spring instead of the class name.
然而,有時(shí)候你可以讓bean完全被Spring控制。這個(gè)是有用的,舉個(gè)例子,如果你希望申請更多復(fù)雜的AOP或Spring的有用技術(shù),如Acegi,到你的bean里。為此,你只要在你的Spring applicationContext.xml設(shè)置你的bean,然而改變在struts.xml 里的Action的類屬性,用Spring里定義的bean名字來代替類的名字。
Your struts.xml file would then have the Action class attributes changed.
你的 struts.xml 里的Action類的屬性會被修改。
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default">
<action name="foo" class="com.acme.Foo">
<result>foo.ftl</result>
</action>
</package>
<package name="secure" namespace="/secure" extends="default">
<action name="bar" class="bar">
<result>bar.ftl</result>
</action>
</package>
</struts>
Where you have a Spring bean defined in your applicationContext.xml named "bar". Note that the com.acme.Foo Action did not need to be changed, because it can be autowired.
你有一個(gè)在你的applicationContext.xml 以 "bar"命名的Spring bean。注意:com.acme.Foo 不需要做修改,因?yàn)樗鼤蛔詣友b配。
A typical spring configuration for bar could look as following.
一個(gè)典型的bar的spring配置像如下代碼:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<bean id="bar" class="com.my.BarClass" singleton="false"/>
...
</beans>
To use session-scoped components with Spring and Struts, see the Spring Session Components Workarounds analysis.
Settings
The following settings can be customized. See the developer guide.
Setting | Description | Default | Possible Values |
---|---|---|---|
struts.objectFactory.spring.autoWire | The autowire strategy | name | name,type,auto, or constructor |
struts.objectFactory.spring.useClassCache | Whether to have Spring use its class cache or not | true | true or false |
Installation
安裝
This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib directory. No other files need to be copied or created.
插件通過拷貝插件jar到你的應(yīng)用/WEB-INF/lib目錄下來完成安裝。不需要拷貝或復(fù)制其他文件。
posted on 2007-04-26 19:36 MingIsMe 閱讀(790) 評論(0) 編輯 收藏 所屬分類: Struts2學(xué)習(xí)