日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Struts2与Spring集成中的自动装配策略

發布時間:2024/4/17 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2与Spring集成中的自动装配策略 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.blogjava.net/jeffma/archive/2010/11/30/339414.html

自動裝配即bean之間的依賴關系無需手動配置。

1.????與自動裝配有關的配置

org.apache.struts2.StrutsConstants類】

??// Spring應該如何裝配。有效值:’name’, ’type’, ’auto’?’construtctor’

STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE

??//?STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE選擇的自動裝配策略是否總是受重視的。默認是false,遺留行為即試圖根據情況決定最好的策略。設置為true表示使用STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE設置的自動裝配策略。

STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE_ALWAYS_RESPECT

??3// Spring是否使用它的類緩存

STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE

2.????自動裝配原理

首先根據在Struts.xml中定義Action時指定的class屬性值獲取Action實例

appContext.getBean(beanName)

n?如果獲取到,則直接返回。此時所使用的自動裝配策略是applicationContext.xml中設置的裝配策略。

applicationContext.xmlbeans的默認自動裝配策略是no,所以如果沒有設置<beansdefault-autowire="autodetect">或者beanautowire="byName",則Action中的屬性比如personManager不會進行自動裝配。

n?如果獲取不到,則調用buildBean(beanClazz, extraContext)

u?如果struts.objectFactory.spring.autoWire.alwaysRespecttrue,此時會根據Struts定義的自動裝配策略(struts.objectFactory.spring.autoWire)進行自動裝配

u?如果struts.objectFactory.spring.autoWire.alwaysRespectfalse,則按constructor方式進行自動裝配。

參考SpringObjectFactory.java源代碼

????@Override

????public?Object buildBean(String beanName, Map<String, Object> extraContext,boolean?injectInternal)?throws?Exception {

??????? Object o =?null;

????????try?{

??????????? o =?appContext.getBean(beanName);

??????? }?catch?(NoSuchBeanDefinitionException e) {

??????????? Class beanClazz = getClassInstance(beanName);

??????????? o = buildBean(beanClazz, extraContext);//使用Struts定義的自動裝配策略

??????? }

????????if?(injectInternal) {

????????????injectInternalBeans(o);

??????? }

????????return?o;

??? }

????public?Object buildBean(Class clazz, Map<String, Object> extraContext)?throwsException {

??????? Object bean;

????????try?{

????????????// Decide to follow autowire strategy or use the legacy approach which mixes injection strategies

????????????if?(alwaysRespectAutowireStrategy) {

????????????????// Leave the creation up to Spring

??????????????? bean =?autoWiringFactory.createBean(clazz,?autowireStrategy,false);

????????????????injectApplicationContext(bean);

????????????????return?injectInternalBeans(bean);

??????????? }?else?{

??????????????? bean =?autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR,?false);

??????????????? bean =autoWiringFactory.applyBeanPostProcessorsBeforeInitialization(bean, bean.getClass().getName());

????????????????// We don't need to call the init-method since one won't be registered.

??????????????? bean =autoWiringFactory.applyBeanPostProcessorsAfterInitialization(bean, bean.getClass().getName());

????????????????return?autoWireBean(bean,?autoWiringFactory);

??????????? }

??????? }?catch?(UnsatisfiedDependencyException e) {

????????????if?(LOG.isErrorEnabled())

????????????????LOG.error("Error building bean", e);

????????????// Fall back

????????????return?autoWireBean(super.buildBean(clazz, extraContext),autoWiringFactory);

??????? }

??? }

所有有兩種配置方式

第一種:參見1集成步驟中的配置

applicationContext.xml(配置beans的自動裝配策略)

struts.xmlactionclass屬性值與application-context.xmlbeanid相同。

第二種:

applicationContext.xml(不配置beans的自動裝配策略)

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"> <beans>

<bean?id="login"?class="com.jeff.action.LoginAction"?scope="prototype">

</bean>

<bean?id="personManager"?class="com.jeff.service.PersonManager"?scope="prototype">

</bean> ...

</beans>

struts.xmlstruts.xmlactionclass屬性值與application-context.xmlbeanid不同,而是設置為Action的類名)

<package?name="first"?extends="struts-default"?>

<action?name="login1"?class="com.jeff.action.LoginAction1">

<result>/loginInfo.jsp</result>

</action>

</package>

第二種配置方式,struts.objectFactory.spring.autoWire才有可能起作用。



===============

http://hi.baidu.com/mefeng47/item/e5ed9d237b8e56172a0f1cea

truts2+spring 自動裝配

struts2整合spring有兩種方式

1、采用自動裝配方式,即不在spring中注入action;

好處在于:不必在struts.xml中寫了配置文件后,又在spring的配置文件中再寫一遍配置

如:

在struts.xml中寫

<action name="loginAction" class="com.lk.loginAction" />

就可以了。

可以寫一個BaseAction 里面放入所有的service接口,其他Action繼承它就可以根據自動裝備的方式

自動注入自己需要的service.

2、在spring中注入action

缺點在于:

在struts.xml中寫

<action name="loginAction" class="loginAction" />

同時在spring配置文件中需要寫

<bean id="loginAction" class="com.lk.loginAction" >

???? <property ref="service" />

</bean>

這樣看來配置文件要比第一種要繁瑣一些。

但是據稱這種方式。適合配置AOP的內容。而且struts2官方文檔上也是采用的這種配置


總結

以上是生活随笔為你收集整理的Struts2与Spring集成中的自动装配策略的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。