 |
Spring is a lightweight container, providing centralized, automated configuration and wiring of your application objects, using a technique called "Dependency Injection"
Spring 是一個輕量級容器,它用“依賴注入”的技術提供集成和自動的應用對象配置。
|
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上面運行,能增強核心框架對象的創(chuàng)建能力。當一個對象被創(chuàng)建時,這個對象會使用Struts 配置的類屬性來裝配Spring配置里的id屬性。如果找不到類,Spring會設法按常規(guī)裝配地創(chuàng)建一個。在Action里,Spring 2 的bean scope feature(bean的范圍特性)用來控制Action實例在session、application的使用范圍,或控制Action實例的自定義的使用范圍,以便在默認的請求范圍基礎上提供高級的定制化服務。
 |
Spring Actions are Optional! Spring Actions只是可選項
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.
請您記住:沒有強制要求注冊Spring 的Action.如果你要用Spring你可以選擇它,然而框架會從action mapping里自動創(chuàng)建Action對象。如果你要用Spring來注入你的Action,那么你就可以用Spring。
|
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 對象工廠,框架將提供兩個攔截器來裝配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放在你的應用里。
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:
如果你使用一個以上的對象工廠(比如在你的應用中即用Spring又用Plexus),那么你需要在struts.properties 或Constant Configuration的XML文件里設置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.
框架里默認是自動裝配的(Autowiring自動裝配是指自動尋找Spring用跟你的對象屬性名字一樣定義的對象 )。要改變裝配模式,那么你可以修改spring.autowire 的屬性
struts.objectFactory.spring.autoWire = type
The autowire property can be set to several options.
自動裝配屬性有幾個選項可設置。
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里的屬性名字來裝配。這個選項是默認值。 |
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里只能注冊一個唯一的類型。 |
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的構造器參數(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.
默認情況下,框架會設法用Spring去創(chuàng)建Spring的全部對象。如果Spring不能創(chuàng)建對象,那框架會創(chuàng)建。
Enabling Spring integration for other application objects is a two-step process.
兩個步驟可使Spring能跟其他應用對象集成。
- Configure the Spring listener
設置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: 因為Spring用的是標準監(jiān)聽器,它支持applicationContext.xml之外的配置文件。在web.xml里加入以下代碼,會使Spring的應用容器以所給的文件裝配方式來初始化:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param>
See the Spring documentation for a full description of this parameter. 看到被所有參數(shù)描述的Spring文檔?
|
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里,你為每一個Action指定類。當使用默認的SpringObjectFactory時,框架會要求Spring去創(chuàng)建Action并根據(jù)默認的自動裝配行為去指定裝配依賴。
 |
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 autowired by either naming your dependent properties on your action the same as the bean defined in Spring which should be provided (to allow for name-based autowiring), or using autowire-by-type and only having one of the required type registered with Spring. It also can include using JDK5 annotations to declare transactional and security requirements rather than having to explicitly set up proxies in your Spring configuration. If you can find ways to let Spring know what it needs to do for your action without needing any explicit configuration in the Spring applicationContext.xml, then you won't have to maintain this configuration in both places. 我 們強烈推薦用“宣告”方式來告知Spring為你的Action提供什么。這個包括使你的bean能夠被自動裝配在你的action里的依賴屬性命名,該 action跟所提供的Spring定義的bean一樣。(允許以名字為基礎的自動裝配),或者用類型自動裝配和在Spring里注冊的只有一種的請求類 型。還可以包括使用JDK5的注釋來聲明事務和安全條件,這個比在Spring設置里明確設置一個代理要好。如果你能找到讓Spring知道它需要為你的 action做什么,而不需要在Spring applicationContext.xml有任何明確配置,那么你沒有必要在這兩個地方修改設置。
|
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.
然而,有時候你可以讓bean完全被Spring控制。這個是有用的,舉個例子,如果你希望申請更多復雜的AOP或Spring的有用技術,如Acegi,到你的bean里。為此,你只要在你的Spring applicationContext.xml設置你的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.
你有一個在你的applicationContext.xml 以 "bar"命名的Spring bean。注意:com.acme.Foo 不需要做修改,因為它會被自動裝配。
A typical spring configuration for bar could look as following.
一個典型的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到你的應用/WEB-INF/lib目錄下來完成安裝。不需要拷貝或復制其他文件。