实例化 Spring Bean 的几种方式?
程序员文章站
2022-05-23 10:19:22
...
1. 常规方式
通过构造器
通过构造器实例化 spring bean 就是最常见的 xml 配置 bean 及其相关属性,再通过 BeanFactory 获取 bean。相关代码如下所示:
创建 POJO 实体 User 类:
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
xml 文件中配置 bean:
<bean id="user" class="org.test.spring.ioc.overview.domain.User">
<property name="id" value="1"/>
<property name="name" value="彭于晏"/>
</bean>
最后在主类中获取 bean:
BeanFactory beanFactory = new ClassPathXmlApplicationContext("META-INF/bean-instantiation-context.xml");
User user = beanFactory.getBean("user")
System.out.println(user)
代码运行结果如下所示:
通过静态工厂方法
首先在 User 类中添加静态函数:
public static User createUser(){
User user = new User();
user.setId(3L);
user.setName("刘德华");
return user;
}
在 xml 文件中配置该 Bean:
<!--静态工厂方法实例化 bean -->
<bean id="user-by-static-method" class="org.test.spring.ioc.overview.domain.User" factory-method="createUser"/>
<bean id="userFactory" class="org.test.spring.bean.factory.DefaultUserFactory"/>
<!--Bean 工厂实例方法实例化 bean -->
<bean id="user-by-instance-method" factory-bean="userFactory" factory-method="createUser"/>
<!--factoryBean 实例化 bean-->
<bean id="user-by-factory-bean" class="org.test.spring.bean.factory.UserFactoryBean"/>
通过主函数类获取该 Bean:
BeanFactory beanFactory = new ClassPathXmlApplicationContext("META-INF/bean-instantiation-context.xml");
User user = beanFactory.getBean("user-by-static-method", User.class);
User userInstance = beanFactory.getBean("user-by-instance-method", User.class);
User userFactory = beanFactory.getBean("user-by-factory-bean", User.class);
System.out.println(user);
System.out.println(userInstance);
System.out.println(userFactory);
System.out.println(user == userInstance);
System.out.println(user == userFactory);
通过 Bean 工厂方法
首先 DefaultUserFactory 实现接口 Userfactory:
public class DefaultUserFactory implements UserFactory {
}
其他类的实现方式看上面代码哈
通过 FactoryBean
FactoryBean 的实现方式是定义一个类实现官方 FactoryBean 接口:
public class UserFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
return User.createUser();
}
@Override
public Class<?> getObjectType() {
return User.class;
}
}
其他方式上个楼看看
2.特殊方式
通过 ServiceLoaderFactoryBean
首先,我们需要在 resources/META-INF 目录下创建 services 文件下,同时创建以 Userfactory 路径的文本文件:
文件中的内容为 UserFactory 的实现类 DefaultUserFactory 的路径:
在 xml 文件中配置 bean 标签,其中 id 和 class 为官方指定的属性,property 为 Userfactory:
<bean id="userFactoryServiceLoader" class="org.springframework.beans.factory.serviceloader.ServiceLoaderFactoryBean">
<property name="serviceType" value="org.test.spring.bean.factory.UserFactory"/>
</bean>
然后就是主函数类,打印出该类:
ApplicationContext beanFactory = new ClassPathXmlApplicationContext("META-INF/special-bean-instantiation-context.xml");
ServiceLoader<UserFactory> userFactoryServiceLoader = beanFactory.getBean("userFactoryServiceLoader", ServiceLoader.class);
Iterator<UserFactory> iterator = userFactoryServiceLoader .iterator();
while(iterator.hasNext()){
UserFactory userFactory = iterator.next();
System.out.println(userFactory.createUser());
}
通过 AutowireCapableBeanFactory#createBean()
该种方式比较简单,就是调用官方给的 api:
AutowireCapableBeanFactory autowireCapableBeanFactory = beanFactory.getAutowireCapableBeanFactory();
UserFactory bean = autowireCapableBeanFactory.createBean(DefaultUserFactory.class);
System.out.println(bean.createUser());
通过 BeanDefinitionRegistry#registerBeanDefinition()
该种方式比较简单:具体代码如下所示:
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(User.class);