`
zero_dian
  • 浏览: 17844 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Struts2 + Spring 2 集成配置

阅读更多

英文内容来源:http://struts.apache.org/2.0.6/docs/spring-plugin.html

http://blog.csdn.net/jameslee666/archive/2007/03/28/1543802.aspx

Added by ctran, last edited by Ted Husted on Feb 04, 2007 (view change)

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上面运行,能增强核心框架对象的创建能力。当一个对象被创建时,这个对象会使用Struts 配置的类属性来装配Spring配置里的id属性。如果找不到类,Spring会设法按常规装配地创建一个。在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里自动创建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创建Action、拦截器和记录集
  • Struts创建对象之后自动跟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.propertiesConstant Configuration的XML文件里设置struts.objectFactory 属性:

struts.properties
struts.objectFactory = spring
struts.xml
<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 的属性

Wiring Mode
装配模式
struts.objectFactory.spring.autoWire = type

The autowire property can be set to several options.
自动装配属性有几个选项可设置。

name type auto constructor
Auto-wire by matching the name of the bean in Spring with the name of the property in your action. This is the default
自动装配会根据Spring里的bean的名字和你的Action里的属性名字来装配。这个选项是默认值。
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里只能注册一个唯一的类型。
Spring will attempt to auto-detect the best method for auto-wiring your action
Spring会尝试自动发觉装配你的action的最佳方法。
Spring will auto-wire the parameters of the bean's constructor
Spring会自动装配bean的构造器参数。

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去创建Spring的全部对象。如果Spring不能创建对象,那框架会创建。

Enabling Spring integration for other application objects is a two-step process.
两个步骤可使Spring能跟其他应用对象集成。

  • Configure the Spring listener
    设置Spring监听器
web.xml
<listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
  • Register your objects via the Spring configuration
  • 通过Spring的配置来注册你的对象
applicationContext.xml
<?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用的是标准监听器,它支持applicationContext.xml之外的配置文件。在web.xml里加入以下代码,会使Spring的应用容器以所给的文件装配方式来初始化:

<!-- Context Configuration locations for Spring XML files --><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.
看到被所有参数描述的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去创建Action并根据默认的自动装配行为去指定装配依赖。

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类的属性会被修改。

struts.xml
<!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配置像如下代码:

applicationConext.xml
<?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目录下来完成安装。不需要拷贝或复制其他文件。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics