一、Spring基础
1、核心
(1)IoC/Dependency Injection
l IoC/Dependency Injection(依赖注入):Beans不依赖于框架;容器注入依赖
l 轻量级Spring容器:配置和管理Beans
<beans>
<bean id="exampleBean" class="eg.ExampleBean"/>
<bean id="anotherExample" class="eg.ExampleBeanTwo"/>
</beans>
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;
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"/>
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>
<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>
<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>
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>
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>
<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>
<property name="listProperty">
<list>
<value>a list element</value>
<ref bean="otherBean"/>
<ref bean="anotherBean"/>
</list>
</property>
<property name="setProperty">
<set>
<value>a set element</value>
<ref bean="otherBean"/>
<ref bean="anotherBean"/>
</set>
</property>
<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>
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"/>
public class ExampleBean {
public void init() {
// do some initialization work
}
}
<bean id="exampleBean" class="eg.ExampleBean" init-method="init"/>
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>
MySingleton singleton = (MySingleton)ctx.getBean("mySingleton");
FactoryBean factory = (FactoryBean)ctx.getBean("&mySingleton");