1、使用场景:配置一个bean的时候需要用到IOC容器中的其他bean的时候
2、使用步骤:
(1)创建FactoryBean类,实现FactoryBean接口,及其三个方法(该类对应的是需要使用FactoryBean的模型类,如:Info类)
(2)通过FactoryBean配置Bean的实例
(3)调用第二步配置的bean
3、bean属性:
class :指向FactoryBean的全类名
property:配置FactoryBean的属性
4、注意事项:通过FactoryBean配置bean,实际返回的实例是FactoryBean中的getObject()方法返回的实例
5、注意事项:
①实现FactoryBean接口的类的参数需要些getset方法
②实现FactoryBean接口的类要写一个返回Class
package com.demo.sshtest.factorybean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestFactoryBean {
public static void main(String[] args) {
factorybean();
}
public static void factorybean(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("factoryBeanContext.xml");
Info perpertiesBean = (Info)applicationContext.getBean("factorybean");
System.out.println(perpertiesBean);
}
}
8、Info.java
package com.demo.sshtest.factorybean;
public class Info {
private Integer id;
private String name;
private String password;
public Info(Integer id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", password=" + password + "]";
}
}
9、InfoFactoryBean.java
package com.demo.sshtest.factorybean;
import org.springframework.beans.factory.FactoryBean;
//实现FactoryBean接口,其中"<>"需要写上需要使用该FactoryBean类的模型类
public class InfoFactoryBean implements FactoryBean<Info>{
//在该类里,可以配置属性的getset方法,配置文件中通过property标签设置值
private Integer factoryId;
public Integer getFactoryId() {
return factoryId;
}
public void setFactoryId(Integer factoryId) {
this.factoryId = factoryId;
}
//返回bean对象
@Override
public Info getObject() throws Exception {
return new Info(factoryId,"nameAA","PSWD");
}
//返回bean的类型
@Override
public Class<?> getObjectType() {
return Info.class;
}
//是否单实例
@Override
public boolean isSingleton() {
return true;
}
}
10、factoryBeanContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 通过FactoryBean配置bean -->
<!--
1、使用场景:配置一个bean的时候需要用到IOC容器中的其他bean的时候
2、使用步骤:
(1)创建FactoryBean类,实现FactoryBean接口,及其三个方法(该类对应的是需要使用FactoryBean的模型类,如:Info类)
(2)通过FactoryBean配置Bean的实例
(3)调用第二步配置的bean
3、bean属性:
class :指向FactoryBean的全类名
property:配置FactoryBean的属性
4、注意事项:通过FactoryBean配置bean,实际返回的实例是FactoryBean中的getObject()方法返回的实例
-->
<bean id="factorybean" class="com.demo.sshtest.factorybean.InfoFactoryBean">
<property name="factoryId" value="10"></property>
</bean>
</beans>
11、项目目录
12、demo
https://download.csdn.net/download/qq_22778717/10472919