俗话说得好“工欲善其事必先利其器”,现如今springboot与springcloud已成为快速构建web应用的利器。作为一个爪洼工程师,知道如下的spring扩展点,可能会让你编写出扩展性、维护性更高的代码。
spring提供的钩子,你知道哪些
bean的生命周期
BeanFactoryPostProcessor接口
spring源码中对该类功能描述如下:
/*Factory hook that allows for custom modification of an application context's
bean definitions, adapting the bean property values of the context's underlying
bean factory.*/
翻译过来的意思大致是:工厂钩子允许自定义应用上下文中的bean定义,调整上下文bean
工厂中bean的属性值。直白点的意思就是允许修改bean定义的属性值。
通过一个简单的例子,来了解下钩子的简单应用:定义一个类GoodEntity和DemoBeanFactoryProcessor。自定义的DemoBeanFactoryProcessor功能很简单就是从BeanFactory中获取goodEntity的bean定义,然后设置了goodentity的name属性值。
@Slf4j
@Component
@Data
public class GoodEntity {
private String name;
}
@Component
public class DemoBeanFactoryProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition goodEntity = beanFactory.getBeanDefinition("goodEntity");
goodEntity.getPropertyValues().addPropertyValue("name", "测试设置货品名称");
}
}
@SpringBootApplication
public class CourseApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(CourseApplication.class, args);
System.err.println(run.getBean(GoodEntity.class).getName());
}
}
输出结果如下,可以到打印出了我们设置name属性值。
2021-01-15 15:33:28.818 INFO 7112 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/mall] : Initializing Spring embedded WebApplicationContext
2021-01-15 15:33:28.818 INFO 7112 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 819 ms
2021-01-15 15:33:28.943 INFO 7112 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-15 15:33:29.054 INFO 7112 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/mall'
2021-01-15 15:33:29.062 INFO 7112 --- [ main] c.i.s.s.course.CourseApplication : Started CourseApplication in 1.409 seconds (JVM running for 2.186)
测试设置货品名称
PS:通常情况下如果对两个bean的初始化顺序有要求,我们可以通过BeanDefinition的setDependsOn,来保证两个Bean的初始顺序。
BeanDefinitionRegistryPostProcessor接口
该接口是BeanFactoryPostProcessor的扩展接口,允许在普通的BeanFactoryPostProcessor生效前,向beanfactory中注册BeanDefinition。通过一段代码我们了解下该接口的作用
@Component
public class DemoBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
String beanClassName = OrderEntity.class.getName();
AbstractBeanDefinition beanDefinition =
BeanDefinitionBuilder.genericBeanDefinition(beanClassName).getBeanDefinition();
beanDefinition.getPropertyValues().addPropertyValue("orderId", "id-0001");
String beanName = AnnotationBeanNameGenerator.INSTANCE.generateBeanName(beanDefinition, registry);
registry.registerBeanDefinition(beanName, beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OrderEntity implements Serializable {
private String orderId;
private String address;
private List goods;
private LocalDateTime createTime;
private String userId;
private Integer pay;
private BigDecimal cost;
}
@SpringBootApplication
public class CourseApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(CourseApplication.class, args);
System.err.println(run.getBean(OrderEntity.class).getOrderId());
}
}
大致意思是通过DemoBeanDefinitionRegistryPostProcessor向beanfactory中注册bean定义,然后从spring容器中获取该bean,打印属性。
BeanPostProcessor接口
源码中对该类的描述
/*Factory hook that allows for custom modification of new bean instances —
for example, checking for marker interfaces or wrapping beans with proxies.*/
翻译过来的大致意思是:工厂钩子允许自定义修改新的bean实例,例如检查标记的接口或者使用代理包裹bean。下面我们通过代码来看看该钩子的作用,before修改orderid,after打印。
@Component
public class DemoBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof OrderEntity) {
OrderEntity entity = (OrderEntity)bean;
System.err.println("before");
System.err.println("orderId " + entity.getOrderId());
entity.setOrderId("id-0002");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof OrderEntity) {
OrderEntity entity = (OrderEntity)bean;
System.err.println("after");
System.err.println("orderId " + entity.getOrderId());
}
return bean;
}
}
输出结果如下:
2021-01-15 17:41:17.783 INFO 9488 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
before
orderId id-0001
after
orderId id-0002
2021-01-15 17:41:17.904 INFO 9488 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/mall'
2021-01-15 17:41:17.912 INFO 9488 --- [ main] c.i.s.s.course.CourseApplication : Started
PS:感兴趣的朋友可以看看,spring的aop生成代理对象就是依赖该钩子完成的。
Aware接口
BeanFactoryAware —— 获取BeanFactory对象
BeanClassLoaderAware —— 获取加载Bean的ClassLoader
BeanNameAware —— 获取beanName
ApplicationContextAware —— 获取ApplicationContext
EnvironmentAware —— 获取spring的上下文环境Environment
看看具体的示例:
@Slf4j
@Component
@Data
public class DemoAware
implements BeanFactoryAware, BeanClassLoaderAware, BeanNameAware, ApplicationContextAware, EnvironmentAware {
private ClassLoader classLoader;
private BeanFactory beanFactory;
private String name;
private ApplicationContext applicationContext;
private Environment environment;
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
log.info("classloader {}", classLoader.getClass());
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
log.info("beanFactory {}", beanFactory.getClass());
}
@Override
public void setBeanName(String name) {
this.name = name;
log.info("name {}", name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
log.info("applicationContext {}", applicationContext.getClass());
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
log.info("environment {}", environment.getClass());
}
}
PS:这些个Aware我个人最喜欢用的是ApplicationContextAware和EnvironmentAware。可以获取Spring的上下文和Spring运行时的环境配置信息,就可以随意获取bean和环境配置信息了。
InitializingBean接口
spring中对该接口功能的描述
/* Interface to be implemented by beans that need to react once all their properties
have been set by a {@link BeanFactory}: e.g. to perform custom initialization,
or merely to check that all mandatory properties have been set*/
翻译过来的意思大致是:实现该接口的bean,会在所有的属性都被(BeanFactory)设置完成后执行。例如,实现自定义的初始化,或者检查必要的属性是否被设置了。
@Component
public class DemoInitializingBean implements InitializingBean {
@Autowired
private OrderEntity orderEntity;
private String name;
@Override
public void afterPropertiesSet() throws Exception {
if (orderEntity != null) {
System.out.println("setted");
}
name = "already";
}
}
在整个spring启动的过程中,提供了很多钩子可以供我们来定制化spring启动。实现我们自身的业务逻辑。今天只是给大家介绍了一部分的钩子,还有其他的也请大家留言告诉我:我们共勉。。