这篇文章主要介绍“Spring Bean的优先加载方法怎么实现”,在日常操作中,相信很多人在Spring Bean的优先加载方法怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring Bean的优先加载方法怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
在日常的业务开发中,绝大多数我们都是不关注 bean 的加载顺序,然而如果在某些场景下,当我们希望某个 bean 优于其他的 bean 被实例化时,往往并没有我们想象中的那么简单。
在实际的 SpringBoot 开发中,我们知道都会有一个启动类,如果希望某个类被优先加载,一个成本最低的简单实现,就是在启动类里添加上依赖
@SpringBootApplication
publicclass Application {
// 使用构造方法的方式来优先加载DemoBean
public Application(DemoBean demoBean) {
demoBean.print();
}
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
请注意上面的构造方法,如果我们希望在应用启动之前,demoBean就已经被加载了,那就让 Application 强制依赖它,所以再 Application 的 bean 初始化之前,肯定会优先实例化demoBean
借助InstantiationAwareBeanPostProcessorAdapter来实现在 bean 实例化之前优先加载目标 bean。
publicclass ClientBeanProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware {
private ConfigurableListableBeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
thrownew IllegalArgumentException(
"AutowiredAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory: ">
上面的实现比较简单,借助beanFactory#getBean
来手动触发 bean 的实例,通过实现BeanFactoryAware
接口来获取BeanFactory
,因为实现InstantiationAwareBeanPostProcessor
接口的类会优先于 Bean 被实例,以此来间接的达到我们的目的
接下来的问题就是如何让它生效了,我们这里使用 Import 注解来实现
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({ClientAutoConfiguration.class, ClientBeanProcessor.class})
public@interface EnableOrderClient {
}
package com.spring.master.spring.bean.initbean;
import lombok.Getter;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
/**
* @author Huan Lee
* @version 1.0
* @date 2020-09-25 11:58
* @describtion 业精于勤,荒于嬉;行成于思,毁于随。
*/
public class DatasourceLoader {
@Getter
private String mode;
public DatasourceLoader(Environment environment) {
this.mode = environment.getProperty("config.save.mode");
System.out.println("init DatasourceLoader for:" + mode);
}
@PostConstruct
public void loadResourcres() {
System.out.println("开始初始化资源");
}
}
package com.spring.master.spring.bean.initbean;
import org.springframework.stereotype.Component;
/**
* @author Huan Lee
* @version 1.0
* @date 2020-09-25 12:01
* @describtion 业精于勤,荒于嬉;行成于思,毁于随。
*/
@Component
public class DemoBean {
public DemoBean() {
System.out.println("demo bean init!");
}
public void print() {
System.out.println("print demo bean ");
}
}
package com.spring.master.spring.bean.initbean;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @author Huan Lee
* @version 1.0
* @date 2020-09-25 11:59
* @describtion 业精于勤,荒于嬉;行成于思,毁于随。
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({ClientAutoConfiguration.class, ClientBeanProcessor.class})
public @interface EnableOrderClient {
}
package com.spring.master;
import com.spring.master.spring.bean.initbean.DemoBean;
import com.spring.master.spring.bean.initbean.EnableOrderClient;
import com.spring.master.spring.bean.lifecycle.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@EnableOrderClient
@SpringBootApplication
public class SpringMasterApplication {
public SpringMasterApplication(DemoBean demoBean) {
demoBean.print();
}
public static void main(String[] args) {
SpringApplication.run(SpringMasterApplication.class, args);
}
}
启动服务输出:
init DatasourceLoader for:null
开始初始化资源
com.spring.master.spring.bean.initbean.DatasourceLoader@458342d3
demo bean init!
print demo bean
到此,关于“Spring Bean的优先加载方法怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3727895/blog/4647305