SpringFramework(2)

来源:互联网 发布:网络兼容模式怎么设置 编辑:程序博客网 时间:2024/05/02 02:38
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

一、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);
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击