Bean、消息、事件

[size=xx-large]Bean的基本管理[/size]

[size=x-large]BeanFactory[/size]
BeanFactory负责读取Bean定义文件:管理对象的加载、生成;维护Bean对象与Bean对象之间的依赖关系;负责它的生命周期。

[size=x-large]ApplicationContext[/size]
它是基于Beanfactory而建立的。提供一个应用程序所需的更完整的框架功能:
提供更方法地提取资源文件的方法;提供解析文字消息的方法;支持国际化;可以发布事件。
ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
Business business = (Business) context.getBean("business");

在多个xml文件时可以采用在<beans>中用<import>标签的形式导入,或者用beans*.xml形式使用所有已beans开头的xml。

[size=x-large]Bean的识别名称和别名[/size]
由于有些文件中的命名规则不一样,所以有些bean需要有一些别名。
<beans...>
<bean id="app:dataSource" class="...">
<alias name="app:dataSource" alias="device:dataSource">
<alias name="app:dataSource" alias="user:dataSource">
//或者<alias name="app:dataSource" alias="device:dataSource,user:dataSource">
</bean>
这样,在特定文件中就可以种特定的别名了。

[size=x-large]Bean的实例化[/size]
最基本就是在xml里配置<bean>,还有就是建立一个接口,定义类里的静态方法实现接口,在xml配置<bean>中多谢一个 factory-method="方法名"。
scope设置为prototype时,每次取得Bean都是一个新的实例。

[size=x-large]Bean的生命周期[/size]
如果使用BeanFactory来生成管理Bean,会尽量支持一下的生命周期:
Bean的建立,由Beanfactory读取Bean定义文件,生成各个实例。
属性注入,执行相关的bean属性依赖注入。
执行一些方法。最后在程序结束之前,Bean定义文件上所设置的destroy方法将会执行。
使用getBean方法时菜真正实例化。ApplicationContext则会预先实例化。

[size=x-large]Bean定义的继承[/size]
加入某个Bean定义中许多属性会有同样的值可以考虑继承。
配置如下:
<bean id="inSomeBean" abstract="true">
<property name="name">
<value>guest</value>
</property>
<property name="age">
<value>11</value>
</property>
</bean>
<bean id="some" class="..." parent="inSomeBean">
<property name="name">
<value>justin</value>
</property>
</bean>
inSomeBean中的age属性将被some继承。

[size=x-large]Bean以来设置[/size]
Setter的设置不再说,说下Constructor的设置。
首先Bean要有无参和有参的构造方法。
然后再xml中这样配置:
<bean id="helloBean" class="...">
<constructor-arg index="0">
<value>justin</value>
</constructor-arg>
</bean>

[size=x-large]依赖的值设置与参考[/size]
上例可以简写
<constructor=arg value="justin">

如果属性为null
<property name="name"><null/><property>

如果为“”
<property name="name"><value></value><property>

如果在Bean定义中已经有一个定义的Bean实例,参考某个属性这个实例:
<bean id="helloBean" class="...">
<constructor-arg>
<ref bean="date"/>
</constructor-arg>
</bean>

需要在取得某个Bean钱先实例化别的bean:
<bean id="beanoOne" class="..." depends-on="beanTwo">

[size=x-large]自动绑定[/size]
分三种情况:
byType,在bean中寻找类型符合的。<bean ....autowire="byType">
byName,查找setXXX,名字相同的。<bean ....autowire="byName">
constructor,查找与构造方法类型相同的。<bean ....autowire="constructor">
autodetect,先尝试constructor,然后尝试byType。

[size=x-large]集合对象[/size]
数组,list,set,map类型的依赖注入。
数组:
<bean ...>
<property name="someStrArray">
<list>
<value>...</value>
<value>...</value>
<ref bean=".."/>
</list>
</property>
</bean>
list:
<bean ...>
<property name="someList">
<list>
<value>...</value>
<value>...</value>
<ref bean=".."/>
</list>
</property>
</bean>
map:
<bean ...>
<property name="someList">
<map>
<entry key="" value=""/>
<entry key="" ref=""/>
</map>
</property>
</bean>
set:
<bean ...>
<property name="someList">
<set>
<value>...</value>
<value>...</value>
<ref bean=".."/>
</set>
</property>
</bean>

[size=xx-large]Bean的高级管理[/size]

[size=x-large]非xml方式的配置[/size]
可以用.properties文件配置。
beans-config.properties:
helloBean.(class)=com.johnnyze.HelloBean
helloBean.helloWord=Welcome
SpringDemo:
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("beans-config.properties"));
BeanFactory factory = (BeanFactory) reg;
HelloBean hello = (HelloBean) factory.getBean("helloBean");

还可以在程序中直接写出:
//设置属性
MutablePropertyValues properties = new MutablePropertyValues();
properties.addPropertyValue("helloWord","Hello!");
//设置Bean定义
RootBeanDefinition definition = new RootBeanDefinition(HelloBean.class,properties);
//注册Bean定义与Bean别名
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
reg.registerBeanDefinition("helloBean", definition);


[size=xx-large]资源、消息、事件[/size]

[size=x-large]资源[/size]
Spring提供了对资源文件的泛型存取。
Resource resource = context.getResource("Classpath:...");

[size=x-large]解析文字消息[/size]
实现国际化:
beans-config.xml:
<bean id="" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="message"/>
</bean>

messages_en_US.properties:
userLogin=User {0} login at {1}
messages_zh_TW.properties:
userLogin=用户 {0} 于{1} 登陆

SpringDemo.java:
ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
Object[] arguments = new Object[]{"1",Calendar.getInstance().getTime()};
System.out.println(context.getMessage("userLogin",arguments,Locale.US));
System.out.println(context.getMessage("userLogin",arguments,Locale.TAIWAN));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值