王朝网络
分享
 
 
 

SpringFramework(2)

王朝java/jsp·作者佚名  2006-01-30
宽屏版  字体: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有