Spring2.4——FactoryBean、获取Bean的id
来源:互联网 发布:淘宝售假申诉的凭证 编辑:程序博客网 时间:2024/06/15 23:54
1、FactoryBean——Spring提供的一个特殊接口
FactoryBean接口是工厂Bean的标准接口,实现该接口的Bean通常作为工厂Bean使用,
当我们把工厂Bean部署在容器中、并通过getBean()方法来获取时,容器返回的不是FactoryBean实例,而是返回FactoryBean的产品(也就是该工厂Bean的getObject方法的返回值)。
实现FactoryBean接口要实现3个方法:
- Object getObject():该方法负责返回工厂的产品。
- getObjectType() :该方法返回产品的类型。
- isSingleton():该方法返回产品是否为单例。
eg:
实现FactoryBean接口获取任意类的、任意静态field的值
(1)GetFieldFactoryBean.java
public class GetFieldFactoryBean implements FactoryBean<Object>{private String className;private String targetField;public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getTargetField() {return targetField;}public void setTargetField(String targetField) {this.targetField = targetField;}@Overridepublic Object getObject() throws Exception {//获取任意类的、任意静态field的值。Class clazz = Class.forName(className);Field field = clazz.getField(targetField);// Field是静态的,不需要用对象即可获取值return field.get(null);}// 多余的@Overridepublic Class<?> getObjectType() {return Object.class;}@Overridepublic boolean isSingleton() {return false;}}
Dinasaur.java
public class Dinosaur implements Dog{//如果需要通过FactoryBean获取静态field的值,也需要提供get和set方法public static int flag = 1; public static int getFlag() {return flag;}public static void setFlag(int flag) {Dinosaur.flag = flag;}}(2)beans.xml
<bean id="getField" class="cony.factory.GetFieldFactoryBean"abstract="true"/><bean id="val1" parent="getField"p:className="cony.domain.Dinosaur"p:targetField="flag"/><bean id="val2" parent="getField"p:className="java.sql.Connection"p:targetField="TRANSACTION_SERIALIZABLE"/>
(3)测试
ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");System.out.println(appContext.getBean("val1"));System.out.println(appContext.getBean("val2"));测试结果:
2、 获取Bean的id
需要让Bean实现BeanNameAware接口以及实现该接口中setBeanName()方法。
eg:
(1)Puppy.java
public class Puppy implements ApplicationContextAware,BeanNameAware{String kind;private int age;private ApplicationContext ctx;private String beanId;public void bark(){System.out.println("提前获取的id: "+beanId);System.out.println(ctx.getMessage("bark", null,Locale.getDefault(Category.FORMAT)));}@Overridepublic void setBeanName(String beanId) {System.out.println("调用setBeanName方法");this.beanId = beanId;}@Overridepublic void setApplicationContext(ApplicationContext ctx)throws BeansException {System.out.println("调用setApplicationContext方法");this.ctx = ctx;}public String getKind() {return kind;}//setter、getter...}(2)beans.xml
<bean id="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basenames"><list><value>mess</value></list></property></bean><bean id="puppy"class="cony.domain.Puppy"p:kind="牧羊犬"p:age="9"/>
(3)测试
ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");Puppy puppy = appContext.getBean("puppy",Puppy.class);puppy.bark();System.out.println();测试结果:可以看出setBeanName方法先于setApplicationContext方法被调用
阅读全文
0 0
- Spring2.4——FactoryBean、获取Bean的id
- Spring入门学习——使用Spring的FactoryBean创建Bean
- Spring2.5—— Bean的生命周期、作用域不同步的Bean
- Spring2.3——抽象Bean和子Bean
- Spring通过FactoryBean创建bean的实例
- Spring中通过实现FactoryBean接口获取bean实例
- Spring学习之使用factorybean获取bean实例
- Spring2.2——实例化Bean的3种方式
- spring源码(10)从缓存中获取到单例bean的后续操作(对FactoryBean的处理)
- Spring进阶之路(6)-抽象Bean与工厂Bean以及获取Bean的id
- 从界面获取bean的id时注意
- 16.笔记JAVA Spring框架学习————通过FactoryBean配置Bean
- spring学习笔记(11)——使用FactoryBean创建bean
- spring FactoryBean配置Bean
- Spring bean 之 FactoryBean
- FactoryBean工厂Bean
- Spring - bean配置-FactoryBean
- bean的加载(二) FactoryBean的使用
- Bootstrap-表格
- 程序员如何提高工作效率
- VMware下配置CenterOS7的静态ip
- Prime算法
- 集群间相互ssh免密访问
- Spring2.4——FactoryBean、获取Bean的id
- Laravel Debugbar 扩展包安装及使用教程
- maven 手动安装jar包 笔记
- C语言中strcpy、strcmp、strlen、strcat、memcpy、 memset的用法。
- Bootstrap-路径分页标签和徽章组件
- 八种排序方法(三)——插入排序
- 机器学习知识点总结
- zstu2017新生赛
- 个人推荐:SpringBoot整合Swagger2