自动装配即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.xml中beans的默认自动装配策略是no,所以如果没有设置<beansdefault-autowire="autodetect">或者bean的autowire="byName",则Action中的属性比如personManager不会进行自动装配。
n 如果获取不到,则调用buildBean(beanClazz, extraContext)。
u 如果struts.objectFactory.spring.autoWire.alwaysRespect为true,此时会根据Struts定义的自动装配策略(struts.objectFactory.spring.autoWire)进行自动装配。
u 如果struts.objectFactory.spring.autoWire.alwaysRespect为false,则按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) throws Exception { 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.xml中action的class属性值与application-context.xml的bean的id相同。
第二种:
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.xml(struts.xml中action的class属性值与application-context.xml的bean的id不同,而是设置为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才有可能起作用。