1、spring架构的开发方式:
为实现code与运行环境更好的兼容性,如:数据处理要求能同时处理文本文件和excel文件,两种文件具有不同处理方式,当剥离数据的文件封装方式之后的后续处理方式一致,则可定义接口,分别针对文本文件处理和excel文件处理定义相应的接口实现类,利用spring配置文件,对不同处理定义不同对的实现实例,具有松散偶合的特点;
package com.yiibai.output;
import com.yiibai.output.IOutputGenerator;
public class OutputHelper
{
IOutputGenerator outputGenerator;
public void generateOutput(){
outputGenerator.generateOutput();
}
//=========================通过属性注入接口实例======================
public void setOutputGenerator(IOutputGenerator outputGenerator){
this.outputGenerator = outputGenerator;
}
}
创建一个 Spring bean 的配置文件,并在这里声明所有的Java对象的依赖。
<!-- Spring-Common.xml --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--定义接口辅助类--> <bean id="OutputHelper" class="com.yiibai.output.OutputHelper"> <property name="outputGenerator" ref="CsvOutputGenerator" /> </bean> <!--定义文本处理bean--> <bean id="TxtOutputGenerator" class="com.yiibai.output.impl.CsvOutputGenerator" /> <!--定义excel处理bean--> <bean id="ExcelOutputGenerator" class="com.yiibai.output.impl.JsonOutputGenerator" /> </beans>通过spring调用
package com.yiibai.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiibai.output.OutputHelper;
public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml"});
OutputHelper output = (OutputHelper)context.getBean("OutputHelper");
//=====通过接口函数处理,具体配置实例由怕配置文件确定
output.generateOutput();
}
}
总结:创建接口辅助类,将实现实例类定义为辅助类的属性 (属性注入) ,通过spring配置文件实现灵活的接口实现,无须更改代码即可满足不同场景应用处理需求。
2、Bean对象作用域
bean对象有以下几种生命周期:
-
单例 - 每个Spring IoC 容器返回一个bean实例
-
原型- 当每次请求时返回一个新的bean实例
-
请求 - 返回每个HTTP请求的一个Bean实例
-
会话 - 返回每个HTTP会话的一个bean实例
-
全局会话- 返回全局HTTP会话的一个bean实例
- 其中后三种为web应用对应生命周期,默认情况下在xml文件中定义的bean为单实例对象,需要声明对象为原型实例时,在bean中增加属性:
scope="prototype"
- 原型实例bean配置文件如下示意:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService" class="com.yiibai.customer.services.CustomerService"
scope="prototype"/>
</beans>
原型bean,每getBean时均返回对象实例
3、List、Set、Property、Map等类型属性注入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="CustomerBean" class="com.yiibai.common.Customer">
<!-- java.util.List -->
<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.yiibai.common.Person">
<property name="name" value="yiibaiList" />
<property name="address" value="Hainan Haikou" />
<property name="age" value="28" />
</bean>
</list>
</property>
<!-- java.util.Set -->
<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.yiibai.common.Person">
<property name="name" value="yiibaiSet" />
<property name="address" value="Hainan Haikou" />
<property name="age" value="28" />
</bean>
</set>
</property>
<!-- java.util.Map -->
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="com.yiibai.common.Person">
<property name="name" value="yiibaiMap" />
<property name="address" value="Hainan Haikou" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property>
<!-- java.util.Properties -->
<property name="pros">
<props>
<prop key="admin">admin@yiibai.com</prop>
<prop key="support">support@yiibai.com</prop>
</props>
</property>
</bean>
<bean id="PersonBean" class="com.yiibai.common.Person">
<property name="name" value="yiibai1" />
<property name="address" value="Hainan Haikou 1" />
<property name="age" value="28" />
</bean>
</beans>