SpringFramework(2)

一、Spring基礎

1、核心

1IoC/Dependency Injection

l         IoC/Dependency Injection(依賴注入):Beans不依賴於框架;容器注入依賴

l         輕量級Spring容器:配置和管理Beans

2BeanFactory

l         輕量級Bean容器

l         載入Bean定義,包括:

?         id/name

?         class

?         singleton或原型

?         屬性

?         構造參數

?         初始化方法

?         析構方法

3XmlBeanFactory

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

4Bean協作者

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"/>

5Bean屬性

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類型:BoolByteColorDoubleFloatFontIntLongShortString

l         標準Spring類型:ClassFileLocalePropertiesStringArrayURL

l         定製Spring類型:CustomBooleanCustomDateCustomNumberCustomStringTrimmer

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值來初始化的

8Spring屬性編輯器

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>

10java.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>

11java.util.List

l         List的例子:

<property name="listProperty">

<list>

<value>a list element</value>

<ref bean="otherBean"/>

<ref bean="anotherBean"/>

</list>

</property>

12java.util.Set

l         Set的例子:

<property name="setProperty">

<set>

<value>a set element</value>

<ref bean="otherBean"/>

<ref bean="anotherBean"/>

</set>

</property>

13java.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"/>

15Bean生命週期

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"/>

16PropertyPlaceholderConfigurer

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");

17MethodInvokingFactoryBean

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

18FactoryBean引用

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接口


發佈了0 篇原創文章 · 獲贊 2 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章