1、通过xml
2、通过注解@Component及其衍生注解@Controller、@Service、@Repository定义bean
例子:
@Component
public void Car{
}
3、使用@Configuration定义成一个配置类
@Configuration
public void Car{
}
4、实现FactoryBean
例子:
public class Car { }
public class CarFactoryBean implements FactoryBean<Car> { @Override public Car getObject() throws Exception { Car car = new Car(); //创建这个bean前,在这边可以自定义处理一些业务逻辑 return car; } @Override public Class<?> getObjectType() { return Car.class; } @Override public boolean isSingleton() { return FactoryBean.super.isSingleton(); } }
@Configuration public class SpConfig { @Bean public CarFactoryBean car(){ return new CarFactoryBean(); } }
5、加载配置类并加载配置文件
例子:
public class Car { }
app.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car" class="com.melax.ailatest.bean.Car"></bean> </beans>
@Configuration @ImportResource("app.xml") public class SpConfig2 { }
6、使用@Import注解方式导入,被导入的bean无需使用注解申明为bean
public class Car { }
@Import({Car.class}) public class SpConfig3 { }
7、导入实现了ImportSelector接口的类,实现了对导入源的编程时处理
示例:
public class Car { }
public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { //各种条件判断 boolean b = importingClassMetadata.hasAnnotation("org.springframework.context.annotation.Import"); System.out.println("flag:"+b); if (b){ return new String[]{"com.melax.ailatest.bean.car"}; }else { return new String[0]; } } }
@Import({MyImportSelector.class}) public class SpConfig4 { }
8、导入实现了ImportBeanDefinitionRegistrar接口的类,通过BeanDefinition注册器注册实名bean,实现对容器中bean的裁定,例如对现有bean的覆盖,进而达成不修改源代码的情况下更换实现的效果文章来源:https://www.toymoban.com/news/detail-739785.html
public class Car { }
public class Dog { }
public class MyImportBeanDefinition implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Dog.class).getBeanDefinition(); //实现对car bean的覆盖,当前car的对象是dog registry.registerBeanDefinition("car",beanDefinition); } }
@Import({MyImportBeanDefinition.class}) public class SpConfig5 { @Bean public Car car(){ return new Car(); } }
9、导入实现了BeanDefinitionRegistryPostProcessor接口的类,通过BeanDefinition的注册器注册实名bean实现对容器中bean的最终定义文章来源地址https://www.toymoban.com/news/detail-739785.html
public class Car { }
public class Car2 { private String name; @Override public String toString() { return "Car2{" + "name='" + name + '\'' + '}'; } }
public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { //各种条件判断 boolean b = importingClassMetadata.hasAnnotation("org.springframework.context.annotation.Import"); System.out.println("flag:"+b); if (b){ return new String[]{"com.melax.ailatest.bean.car"}; }else { return new String[0]; } } }
@Import({MyPostProcessor.class}) public class SpConfig6 { @Bean public Car car(){ return new Car(); } }
到了这里,关于springBoot bean的多种加载方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!