Web服务器启动时加载缓存

来源:互联网 发布:数据交换方式有哪些 编辑:程序博客网 时间:2024/05/19 02:29

Web服务器启动时加载缓存

package service;import javax.annotation.PostConstruct;import org.springframework.context.annotation.Lazy;import org.springframework.stereotype.Component;@Lazy(false)@Componentpublic class CacheManager {    @PostConstruct    private void init() {        System.out.println("hhh");    }}

@Lazy注解

If this annotation is not present on a @Component or @Bean definition, eager initialization will occur. If present and set to true, the @Bean or @Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory. If present and set to false, the bean will be instantiated on startup by bean factories that perform eager initialization of singletons.
只要不设置成true,这里不用此注解或者设置为false,都能实现主动初始化

@PostConstruct注解

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection. The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected. Only one method can be annotated with this annotation.
在方法上加上此注解即可实现Web服务器启动时做一些自定义的逻辑,例如实现自己的程序的缓存加载

0 0
原创粉丝点击