BeanDefinition注册
- XML配置元信息
<bean name="...." .../> - Java注解配置元信息: 1.@Bean 2.@Component3.@Import
/**
* 3.通过@Import方式进行导入
*/
@Import(AnnotationBeanDefinitionDemo.Config.class)
public class AnnotationBeanDefinitionDemo {
public static void main(String[] args) {
//创建BeanFactory容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//注册Configuration Class(配置类)
applicationContext.register(AnnotationBeanDefinitionDemo.class);
//启动spring应用上下文
applicationContext.refresh();
//按照类型依赖查找
System.out.println("config 的所有 Beans"+applicationContext.getBeansOfType(Config.class));
System.out.println("user 的所有 Beans"+applicationContext.getBeansOfType(User.class));
//显示的关闭spring应用上下文
applicationContext.close();
}
/**
* 2.通过@component方式 定义当前类作为Spring Bean(组件)
*/
@Component
public static class Config{
/**
* 1.通过Java 注解的方式,定义了一个bean
* @return
*/
@Bean(name={"user","zhangsan-user"})
public User user(){
User user = new User();
user.setId(1L);
user.setName("zhangsan");
return user;
}
}
}
-
Java API 配置元信息
- 命名方式:BeanDefinitionRegistry#registerBeanDefinition(String,BeanDefiniton)
- 非命名方式:
- BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition,BeanDefinitonRegistry)
/**
* 命名Bean注册方式
* @param registry
* @param beanName
*/
public static void registerUserBeanDefinition(BeanDefinitionRegistry registry,String beanName){
BeanDefinitionBuilder beanDefinitionBuilder = genericBeanDefinition(User.class);
beanDefinitionBuilder.addPropertyValue("id",1)
.addPropertyValue("name","zhangsan");
//判断如果BeanName参数存在时
if(StringUtils.hasText(beanName)){
//注册BeanDefinition
registry.registerBeanDefinition(beanName,beanDefinitionBuilder.getBeanDefinition());
}else {
//非命名的注册方法
BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinitionBuilder.getBeanDefinition(),registry);
}
}
/**
* 非命名注册bean
* @param registry
*/
public static void registerUserBeanDefinition(BeanDefinitionRegistry registry){
registerUserBeanDefinition(registry,null);
}
* 配置类方式:AnnotatedBeanDefinitionReader#register(Class....)
Spring Bean的实例化
Bean实例化(Instantiation)
- 常规方式
- 通过构造器(配置元信息:XML、Java注解和Java API)
- 通过静态工厂方法(配置元信息:XML和Java API)
- 通过Bean工厂方法(配置元信息:XML和Java API)
- 通过FactoryBean(配置元信息:XML、Java注解和Java API)
例如:通过xml方式进行相关的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 静态方法实例化bean-->
<bean id="user-by-static-method" class="com.test.ioc.overview.dependency.domian.User"
factory-method="createUser"/>
<!--实例(Bean)的方法实例化Bean-->
<bean class="com.test.thinking.in.spring.bean.factory.DefaultUserFactroy"/>
<!--通过FactoryBean的方式实例化bean-->
<bean id="user-by-factroy-bean" class="com.test.thinking.in.spring.bean.factory.UserFactoryBean"/>
</beans>
- 特殊方式
- 通过ServiceLoaderFactoryBean(配置元信息:XML、Java注解和Java API)
public final class ServiceLoader<S>
implements Iterable<S>
{
private static final String PREFIX = "META-INF/services/";</pre>
ServiceLoader默认的文件路径是"META-INF/services/"下面,我们先新建一个相关目录,然后写入对应的类路径
![](https://upload-images.jianshu.io/upload_images/17885848-8585683ba841f1bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
配置对应的XML文件:
``` xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userFactoryServiceLoader" class="org.springframework.beans.factory.serviceloader.ServiceLoaderFactoryBean">
<property name="serviceType" value="com.test.thinking.in.spring.bean.factory.UserFactory"/>
</bean>
</beans>
通过ServiceLoader对应的API获取:
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("META-INF/special-bean-instination-context.xml");
ServiceLoader<UserFactory> serviceLoader = beanFactory.getBean("userFactoryServiceLoader", ServiceLoader.class);
displayServiceLoader( serviceLoader);
demoServiceLoader();
}
public static void demoServiceLoader(){
ServiceLoader<UserFactory> serviceLoader = ServiceLoader.load(UserFactory.class, Thread.currentThread().getContextClassLoader());
displayServiceLoader(serviceLoader);
}
private static void displayServiceLoader(ServiceLoader<UserFactory> serviceLoader){
Iterator<UserFactory> iterator = serviceLoader.iterator();
while (iterator.hasNext()){
UserFactory userFactory = iterator.next();
System.out.println(userFactory.createUser());
}
}
* 通过AutowireCapableBeanFactory#createBean(java.lang.Class,int,boolen)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/special-bean-instination-context.xml");
//通过ApplicationContext获取AutowireCapableBeanFactory
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
ServiceLoader<UserFactory> serviceLoader = beanFactory.getBean("userFactoryServiceLoader", ServiceLoader.class);
displayServiceLoader( serviceLoader);
// demoServiceLoader();
//创建beanfactory对象,通过AutowireCapableBeanFactory
UserFactory userFactory = beanFactory.createBean(DefaultUserFactroy.class);
userFactory.createUser();
* 通过BeanDefinitionRegistry#registerBeanDefinition(String,BeanDefiniton)