代码解读复制代码// DataSourceFactory.java
public class DataSourceFactory implements FactoryBean<DataSource> {
private String url;
private String username;
private String password;
// Setters for url, username, and password
@Override
public DataSource getObject() throws Exception {
// 创建复杂的数据库连接池对象
HikariConfig config = new HikariConfig();
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);
return new HikariDataSource(config);
}
@Override
public Class<?> getObjectType() {
return DataSource.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
// Spring配置文件 (applicationContext.xml)
<bean id="dataSourceFactory" class="com.example.DataSourceFactory">
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<bean id="dataSource" factory-bean="dataSourceFactory" factory-method="getObject"/>
// 测试代码
public class DataSourceFactoryTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) context.getBean("dataSource");
System.out.println(dataSource);
}
}
代码解读复制代码// ExpensiveResourceFactory.java
public class ExpensiveResourceFactory implements FactoryBean<ExpensiveResource> {
@Override
public ExpensiveResource getObject() throws Exception {
// 创建资源密集型对象
return new ExpensiveResource();
}
@Override
public Class<?> getObjectType() {
return ExpensiveResource.class;
}
@Override
public boolean isSingleton() {
return false; // 每次调用都会创建一个新的实例
}
}
// Spring配置文件 (applicationContext.xml)
<bean id="expensiveResource" class="com.example.ExpensiveResourceFactory"/>
// 测试代码
public class ExpensiveResourceFactoryTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ExpensiveResource resource1 = (ExpensiveResource) context.getBean("expensiveResource");
ExpensiveResource resource2 = (ExpensiveResource) context.getBean("expensiveResource");
System.out.println(resource1);
System.out.println(resource2);
System.out.println(resource1 == resource2); // 应该输出 false
}
}
代码解读复制代码// ProxyFactoryBean.java
public class ProxyFactoryBean implements FactoryBean<MyService> {
private MyService target;
public void setTarget(MyService target) {
this.target = target;
}
@Override
public MyService getObject() throws Exception {
// 创建动态代理
return (MyService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
(proxy, method, args) -> {
System.out.println("Before method: " + method.getName());
Object result = method.invoke(target, args);
System.out.println("After method: " + method.getName());
return result;
}
);
}
@Override
public Class<?> getObjectType() {
return MyService.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
// Spring配置文件 (applicationContext.xml)
<bean id="myServiceTarget" class="com.example.MyServiceImpl"/>
<bean id="myServiceProxy" class="com.example.ProxyFactoryBean">
<property name="target" ref="myServiceTarget"/>
</bean>
// 测试代码
public class ProxyFactoryBeanTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyService myService = (MyService) context.getBean("myServiceProxy");
myService.someMethod();
}
}
代码解读复制代码// ConfigurableBeanFactory.java
public class ConfigurableBeanFactory implements FactoryBean<MyBean> {
private String config;
public void setConfig(String config) {
this.config = config;
}
@Override
public MyBean getObject() throws Exception {
// 根据配置创建Bean
MyBean myBean = new MyBean();
myBean.setConfig(config);
return myBean;
}
@Override
public Class<?> getObjectType() {
return MyBean.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
// Spring配置文件 (applicationContext.xml)
<bean id="configurableBeanFactory" class="com.example.ConfigurableBeanFactory">
<property name="config" value="someConfigValue"/>
</bean>
<bean id="myBean" factory-bean="configurableBeanFactory" factory-method="getObject"/>
// 测试代码
public class ConfigurableBeanFactoryTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = (MyBean) context.getBean("myBean");
System.out.println(myBean.getConfig()); // 应该输出 "someConfigValue"
}
}
代码解读复制代码// ThirdPartyBeanFactory.java
public class ThirdPartyBeanFactory implements FactoryBean<ThirdPartyBean> {
@Override
public ThirdPartyBean getObject() throws Exception {
// 使用第三方库的API创建对象
return new ThirdPartyBeanBuilder().build();
}
@Override
public Class<?> getObjectType() {
return ThirdPartyBean.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
// Spring配置文件 (applicationContext.xml)
<bean id="thirdPartyBean" class="com.example.ThirdPartyBeanFactory"/>
// 测试代码
public class ThirdPartyBeanFactoryTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ThirdPartyBean thirdPartyBean = (ThirdPartyBean) context.getBean("thirdPartyBean");
System.out.println(thirdPartyBean);
}
}
FactoryBean
的实现原理涉及Spring框架的核心机制,包括Bean的定义、创建和管理。FactoryBean
接口的核心在于它提供了一种自定义Bean实例化逻辑的方式。以下是FactoryBean
的实现原理:
FactoryBean
接口FactoryBean
接口定义了三个主要方法:
T getObject() throws Exception
: 返回由这个FactoryBean
创建的Bean实例。Class<?> getObjectType()
: 返回这个FactoryBean
创建的Bean的类型。boolean isSingleton()
: 返回这个FactoryBean
创建的Bean是否是单例的。FactoryBean
当Spring容器遇到一个实现了FactoryBean
接口的Bean定义时,它会执行以下步骤:
识别FactoryBean
:Spring容器会检查Bean定义的类是否实现了FactoryBean
接口。如果是,Spring容器会将其识别为一个FactoryBean
。
调用getObject()
方法:当需要获取这个Bean实例时,Spring容器会调用FactoryBean
的getObject()
方法,而不是直接实例化这个类。这允许开发者在getObject()
方法中自定义Bean的创建逻辑。
处理单例和原型:根据isSingleton()
方法的返回值,Spring容器会决定是缓存这个Bean实例(单例)还是每次都调用getObject()
方法来创建新的实例(原型)。
获取Bean类型:Spring容器会调用getObjectType()
方法来确定这个FactoryBean
创建的Bean的类型。这在某些情况下(例如,类型检查或自动装配)非常有用。
下面是一个简单的FactoryBean
实现示例,以及Spring容器如何处理它的示例代码:java
代码解读复制代码public class MyBeanFactory implements FactoryBean<MyBean> {
@Override
public MyBean getObject() throws Exception {
// 自定义Bean的创建逻辑
return new MyBean();
}
@Override
public Class<?> getObjectType() {
return MyBean.class;
}
@Override
public boolean isSingleton() {
return true; // 返回true表示这个Bean是单例的
}
}
假设在Spring配置文件中定义了一个FactoryBean
:xml
代码解读复制代码<bean id="myBeanFactory" class="com.example.MyBeanFactory"/>
当Spring容器启动时,它会进行以下步骤:
Bean定义解析:Spring容器解析myBeanFactory
的Bean定义,并识别它实现了FactoryBean
接口。
实例化FactoryBean
:Spring容器实例化MyBeanFactory
类。
调用getObject()
方法:当应用程序请求myBeanFactory
这个Bean时,Spring容器会调用MyBeanFactory
的getObject()
方法,而不是直接返回MyBeanFactory
实例。
返回实际Bean:getObject()
方法返回的MyBean
实例被返回给请求者。
单例处理:如果isSingleton()
方法返回true
,Spring容器会缓存这个MyBean
实例,以便后续请求直接返回缓存的实例。如果返回false
,每次请求都会调用getObject()
方法创建新的实例。
以下是一个完整的示例,包括Spring配置和测试代码:java
代码解读复制代码// MyBean.java
public class MyBean {
// Bean的属性和方法
}
// MyBeanFactory.java
public class MyBeanFactory implements FactoryBean<MyBean> {
@Override
public MyBean getObject() throws Exception {
// 自定义Bean的创建逻辑
return new MyBean();
}
@Override
public Class<?> getObjectType() {
return MyBean.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
// Spring配置文件 (applicationContext.xml)
<bean id="myBeanFactory" class="com.example.MyBeanFactory"/>
// 测试代码
public class MyBeanFactoryTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean myBean = (MyBean) context.getBean("myBeanFactory");
System.out.println(myBean);
}
}
通过上述步骤和示例,可以看到FactoryBean
的实现原理和Spring容器如何处理FactoryBean
。这一机制使得Spring框架在Bean的创建和管理上具有极大的灵活性。