Spring支持如下三种方式创建Bean
1:调用构造器创建Bean
2:调用静态工厂方法创建Bean
3:调用实例工厂方法创建Bean
注解@Bean方式
Xml方式
BeanDefinitionBuilder方式
1.注解@Bean方式
通过@Bean方式创建自定义Bean是最明显的方式,
直接在对应的方法上添加@Bean注解,表明这是Bean,
结合@Configuration,Spring会自动创建Bean,
测试样例及注释如下:
BeanDefinitionByAnnotation.java:
package com.example.springbeandemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanDefinitionByAnnotation {
private static final Logger logger = LoggerFactory.getLogger(BeanDefinitionByAnnotation.class);
@Bean
public User myUserBean(){
return new User("hhh",21);
}
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//注册BeanDefinition
applicationContext.register(BeanDefinitionByAnnotation.class);
//加载或刷新配置(JAVA基础配置/XML/properties等)的持久化描述
//这里关注填充singletonObjects
applicationContext.refresh();
logger.info(">>>>>>>>>>应用程序上下文启动");
User user = applicationContext.getBean(User.class);
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println(user.getClass());
logger.info(">>>>>>>>Bean:{}", user);
applicationContext.close();
logger.info(">>>>>>>>关闭应用程序上下文");
}
}
AnnotationConfigApplicationContext实现了Bean定义的注册,Bean的实例化,事件,生命周期,国际化等功能,使用AnnotationConfigApplicationContext可以实现基于Java的配置类加载Spring的应用上下文。避免使用application.xml进行配置。相比XML配置,更加便捷
2.XML方式
通过XML创建Bean,首先要构建对应的XML文件
bean.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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 在Spring framework中启用@Resource注解 -->
<context:annotation-config />
<!-- 构建User Bean实例:Bean id为user1 -->
<bean id="userA" class="com.example.springbeandemo.User">
<property name="name" value="qiezi" />
<property name="age" value="20" />
</bean>
</beans>
User.java:
package com.example.springbeandemo;
public class User {
private String name;
private int age;
public User(){
}
public User(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
BeanDefinitionByXml.java:
package com.example.springbeandemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanDefinitionByXml {
private static final Logger logger = LoggerFactory.getLogger(BeanDefinitionByXml.class);
public static void main(String[] args) {
String beanConfig = "bean.xml";
//通过xml配置文件构建Bean
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(beanConfig);
User user = applicationContext.getBean("userA", User.class);
System.out.println(user.getName());
System.out.println(user.getAge());
logger.info(">>>>>>>>Creation Bean using XML, User Bean :{}", user);
}
}
ClassPathXmlApplicationContext是Spring读取xml最常用的类,它只能读取放在WEB-INF/classes/目录下的配置文件,所以在使用ClassPathXmlApplicationContext的时候可以将配置文件放在项目的原文件夹下面,这样编译的时候会将配置文件拷贝到WEB-INF下面去。如果是Springboot项目,一般要把文件呢放到resources配置文件夹下,才可以被ClassPathXmlApplicationContext读取到。
3.BeanDefinitionBuilder方式
上面两种创建Bean的方式对于应用开发者而言是显式的,
而,更贴近Spring底层的方式是通过BeanDefinition创建Bean,
Spring创建Bean的第一个过程即注册BeanDefinition,就是下面的样例,
通过显式的方式注册BeanDefinition,
然后再创建单例的Bean,测试样例及注释如下:
package com.example.springbeandemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BeanDefinitionByBuilder {
private static final Logger logger = LoggerFactory.getLogger(BeanDefinitionByBuilder.class);
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//构建BeanDefinition,填充Bean属性
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
beanDefinitionBuilder.addPropertyValue("name","nick");
beanDefinitionBuilder.addPropertyValue("age","20");
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
//注册BeanDefinition
applicationContext.registerBeanDefinition("myBean",beanDefinition);
// 加载或刷新配置(Java基础配置/XML/properties等)的持久化描述,
// 这里关注填充singletonObjects
applicationContext.refresh();
logger.info(">>>>>>>>>>应用程序上下文启动");
User user1 = applicationContext.getBean(User.class);
System.out.println(user1.getName());
System.out.println(user1.getAge());
User user2 = (User) applicationContext.getBean("myBean");
System.out.println(user2.getName());
logger.info(">>>>>>>>>>User:{}, user2:{}", user1, user2);
applicationContext.close();
logger.info(">>>>>>>>关闭应用程序上下文");
}
}
参考:
https://blog.csdn.net/Xin_101/article/details/128334261