一、Spring基础
1、核心
(1)IoC/Dependency Injection
l IoC/Dependency Injection(依赖注入):Beans不依赖于框架;容器注入依赖
l 轻量级Spring容器:配置和管理Beans
(2)BeanFactory
l 轻量级Bean容器
l 载入Bean定义,包括:
? id/name
? class
? singleton或原型
? 属性
? 构造参数
? 初始化方法
? 析构方法
(3)XmlBeanFactory
l BeanFactory实现
l Beans定义的例子:
<beans>
<bean id="exampleBean" class="eg.ExampleBean"/>
<bean id="anotherExample" class="eg.ExampleBeanTwo"/>
</beans>
l 使用的例子:
InputStream input = new FileInputStream("beans.xml");
BeanFactory factory = new XmlBeanFactory(input);
ExampleBean eb = (ExampleBean)factory.getBean("exampleBean");
ExampleBeanTwo eb2 = (ExampleBeanTwo)factory.getBean("anotherExample");
可能会抛出NoSuchBeanDefinitionException;
ExampleBean eb = (ExampleBean)factory.getBean("exampleBean", ExampleBean.class);
可能会抛出BeanNotOfRequiredTypeException;
(4)Bean协作者
l 你的Bean工作所需要的其它Bean
package eg;
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
public void setBeanOne(AnotherBean b) { beanOne = b; }
public void setBeanTwo(YetAnotherBean b) { beanTwo = b; }
}
<bean id="exampleBean" class="eg.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo"><ref bean="yetAnotherBean"/></property>
</bean>
<bean id="anotherExampleBean" class="eg.AnotherBean"/>
<bean id="yetAnotherBean" class="eg.YetAnotherBean"/>
(5)Bean属性
l 设置Bean属性
package eg;
public class ExampleBean {
private String s;
private int i;
public void setStringProperty(String s) { this.s = s; }
public void setIntegerProperty(int i) { this.i = i; }
}
<bean id="exampleBean" class="eg.ExampleBean">
<property name="stringProperty"><value>Hi!</value></property>
<property name="integerProperty"><value>1</value></property>
</bean>
(6)属性编辑器
l 转换String类型为各种对象类型
l 实现java.beans.PropertyEditor
l 标准Java类型:Bool、Byte、Color、Double、Float、Font、Int、Long、Short、String
l 标准Spring类型:Class、File、Locale、Properties、StringArray、URL
l 定制Spring类型:CustomBoolean、CustomDate、CustomNumber、CustomStringTrimmer
(7)标准属性编辑器
l 例子:
<property name="intProperty"><value>7</value></property>
<property name="doubleProperty"><value>0.25</value></property>
<property name="booleanProperty"><value>true</value></property>
<property name="colorProperty"><value>0,255,0</value></property>
java.awt.Color是用RGB值来初始化的
(8)Spring属性编辑器
l 例子:
<property name="classProperty">
<value>java.lang.Object</value>
</property>
<property name="fileProperty">
<value>/home/ziba/file.txt</value>
</property>
<property name="localeProperty">
<value>pt_BR</value>
</property>
<property name="urlProperty">
<value>http://java.net</value>
</property>
<property name="stringArrayProperty">
<value>foo,bar,baz</value>
</property>
(9)定制属性编辑器
l Date例子:
DateFormat fmt = new SimpleDateFormat("d/M/yyyy");
CustomDateEditor dateEditor = new CustomDateEditor(fmt, false);
beanFactory.registerCustomEditor(java.util.Date.class, dateEditor);
<property name="date"><value>19/2/2004</value></property>
l StringTrimming例子:Trim字符串,转换空串为null值
StringTrimmerEditor trimmer = new StringTrimmerEditor(true);
beanFactory.registerCustomEditor(java.lang.String.class, trimmer);
<property name="string1"><value> hello </value></property>
<property name="string2"><value></value></property>
<property name="string2"><null/></property>
(10)java.util.Properties
l Properties的例子:
<property name="propertiesProperty">
<value>
foo=1
bar=2
baz=3
</value>
</property>
<property name="propertiesProperty">
<props>
<prop key="foo">1</prop>
<prop key="bar">2</prop>
<prop key="baz">3</prop>
</props>
</property>
(11)java.util.List
l List的例子:
<property name="listProperty">
<list>
<value>a list element</value>
<ref bean="otherBean"/>
<ref bean="anotherBean"/>
</list>
</property>
(12)java.util.Set
l Set的例子:
<property name="setProperty">
<set>
<value>a set element</value>
<ref bean="otherBean"/>
<ref bean="anotherBean"/>
</set>
</property>
(13)java.util.Map
l Map的例子:
<property name="mapProperty">
<map>
<entry key="yup an entry">
<value>just some string</value>
</entry>
<entry key="yup a ref">
<ref bean="otherBean"/>
</entry>
</map>
</property>
(14)构造方法
l 构造方法的例子:
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(AnotherBean b1, YetAnotherBean b2, int i) {
this.beanOne = b1;
this.beanTwo = b2;
this.i = i;
}
}
<bean id="exampleBean" class="eg.ExampleBean">
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
<constructor-arg><value>1</value></constructor-arg>
</bean>
<bean id="anotherExampleBean" class="eg.AnotherBean"/>
<bean id="yetAnotherBean" class="eg.YetAnotherBean"/>
(15)Bean生命周期
l Beans在它第一次使用时,能够被factory初始化
public class ExampleBean {
public void init() {
// do some initialization work
}
}
<bean id="exampleBean" class="eg.ExampleBean" init-method="init"/>
l Beans在不再使用时,能够被清除
public class ExampleBean {
public void cleanup() {
// do some destruction work
}
}
<bean id="exampleBean" class="eg.ExampleBean" destroy-method="cleanup"/>
(16)PropertyPlaceholderConfigurer
l 从外部Properties文件中并入属性
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
jdbc.properties
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
l 安装Configurer
InputStream input = new FileInputStream("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(input);
Properties props = new Properties();
props.load(new FileInputStream("jdbc.properties"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setProperties(props);
cfg.postProcessBeanFactory(factory);
DataSource ds = (DataSource)factory.getBean("dataSource");
(17)MethodInvokingFactoryBean
l 暴露使用Singleton模式的Bean
package eg;
public class MySingleton {
private static MySingleton instance = new MySingleton();
private MySingleton() {}
public static MySingleton getInstance() {
return instance;
}
}
<bean name="mySingleton"
class="...beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod">
<value>eg.MySingleton.getInstance</value>
</property>
</bean>
FactoryBean代表另一个类创建Bean
(18)FactoryBean引用
l 获得FactoryBean自己的引用:
MySingleton singleton = (MySingleton)ctx.getBean("mySingleton");
获得由factory创建的Bean
FactoryBean factory = (FactoryBean)ctx.getBean("&mySingleton");
获得factory
(19)高级特性
l 单例/原型
l Autowiring:对于type,要求每种需要的类型具有单实例;对于name,要求匹配每个属性名字的Bean名字(对非简单属性)
l 依赖检查
l BeanWrapper
l InitializingBean/DisposableBean接口
l BeanFactoryAware/BeanNameAware接口