1.xml方式声明bean
package com.learning;
import com.learning.bean.Dog;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author wangyouhui
* @Description TODO
**/
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object cat = applicationContext.getBean("cat");
System.out.println(cat);
Dog dog = applicationContext.getBean(Dog.class);
System.out.println(dog);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String bean: beanDefinitionNames) {
System.out.println(bean);
}
}
}
<?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">
<bean id="cat" class="com.learning.bean.Cat"/>
<bean class="com.learning.bean.Dog"/>
</beans>
2.xml+注解方式声明bean
<?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">
<context:component-scan base-package="com.learning"/>
</beans>
package com.learning.bean;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @Author wangyouhui
* @Description
**/
@Component
public class DbConfig {
@Bean
public DruidDataSource getDataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}
3.注解方式声明配置类
package com.learning.bean;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
/**
* @Author wangyouhui
* @Description
**/
@Component
@ComponentScan("com.learning")
public class DbConfig {
@Bean
public DruidDataSource getDataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}
4.实现FactoryBean接口
package com.learning.bean;
import org.springframework.beans.factory.FactoryBean;
/**
* @Author wangyouhui
* @Description TODO
**/
public class DogFactoryBean implements FactoryBean<Dog> {
@Override
public Dog getObject() throws Exception {
return new Dog();
}
@Override
public Class<?> getObjectType() {
return Dog.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
package com.learning;
import com.learning.bean.DogFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
/**
* @Author wangyouhui
* @Description TODO
**/
@ComponentScan("com.learning")
public class DogConfig {
@Bean
public DogFactoryBean dog(){
return new DogFactoryBean();
}
}
5.使用@Import注解导入配置类
package com.learning;
import com.learning.bean.DbConfig;
import org.springframework.context.annotation.Import;
/**
* @Author wangyouhui
* @Description TODO
**/
@Import(DbConfig.class)
public class SpringConfig {
}
6.使用上下文对象在容器初始化完毕后注入bean
package com.learning;
import com.learning.bean.Cat;
import com.learning.bean.Dog;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author wangyouhui
* @Description TODO
**/
public class Application3 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
applicationContext.register(Cat.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String bean: beanDefinitionNames) {
System.out.println(bean);
}
}
}
7.导入实现了ImportSelector接口的类,实现对导入源的编程式处理
package com.learning.selector;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
/**
* @Author wangyouhui
* @Description TODO
**/
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
boolean flag = annotationMetadata.hasAnnotation("org.springframework.context.annotation.Import");
if(flag){
return new String[]{"com.learning.bean.Cat"};
}
return new String[]{"com.learning.bean.Dog"};
}
}
package com.learning;
import com.learning.selector.MyImportSelector;
import org.springframework.context.annotation.Import;
/**
* @Author wangyouhui
* @Description TODO
**/
@Import(MyImportSelector.class)
public class SpringConfig {
}
8.导入实现了ImportBeanDefinitionRegistrar接口的类
1.通过BeanDefinition的注册器注册实名Bean,实现对容器中bean的裁定
2.可以对现有bean的覆盖,达到不修改源代码的情况下更换实现的效果
package com.learning.registry;
import com.learning.bean.Dog;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
/**
* @Author wangyouhui
* @Description TODO
**/
public class MyRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry){
// 1.使用元数据去做判定
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Dog.class).getBeanDefinition();
registry.registerBeanDefinition("bean1",beanDefinition);
}
}
package com.learning;
import com.learning.registry.MyRegistrar;
import org.springframework.context.annotation.Import;
/**
* @Author wangyouhui
* @Description TODO
**/
@Import(MyRegistrar.class)
public class SpringConfig {
}
9.导入实现了BeanDefinitionRegistryPostProcessor接口的类,通过BeanDefinition的注册器注册实名bean,实现对容器中bean的最终裁定
package com.learning.processor;
import com.learning.bean.Dog;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
/**
* @Author wangyouhui
* @Description TODO
**/
public class MyPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Dog.class).getBeanDefinition();
beanDefinitionRegistry.registerBeanDefinition("dog", beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/92337.html