Spring
- 生命周期回调函数
@PostConstruct
@PreDestroy
- 测试
package test.service;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Service
public class LifeCycleService implements InitializingBean, DisposableBean {
public LifeCycleService() {
System.out.println("do constructor");
}
public void init() {
System.out.println("default init");
}
/**
* 构造方法执行完之后 执行,和afterPropertiesSet()起同等作用
*/
@PostConstruct
public void postConstructMethod() {
System.out.println("do postConstructMethod");
}
@PreDestroy
public void preDestroyMethod() {
System.out.println("do preDestroyMethod");
}
/**
* -- 执行完之后 执行
* @throws Exception
*/
public void afterPropertiesSet() throws Exception {
System.out.println("do afterPropertiesSet");
}
public void destroy() throws Exception {
System.out.println("do destroy");
}
}
- 如果 beanOne 初始化前需要用到 manager,那么可用 @Dependson 注解,那么manager就会在 beanOne前完成初始化
@Lazy
@ComponentScan
-
* 对于这个注解的一个值,excludeFilters,可以传入一个注解,表示不扫描所有包含此注解得类
-
* 可以传入一个类,表示不扫描哪个类
-
* 可以传入一个包,表示不扫描哪个包
- 不扫描注解
package test.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
@Configuration
@ComponentScan(basePackages = {"test"},
excludeFilters = @ComponentScan.Filter(classes = {Service.class}))
public class MyAppConfig {
}
package test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import test.config.MyAppConfig;
public class SpringTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MyAppConfig.class);
}
}
- 不扫描类
package test.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;
@Configuration
@ComponentScan(basePackages = {"test"},
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX,
pattern = "test.service.LifeCycleService"))
public class MyAppConfig {
}
- 不扫描包
package test.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;
@Configuration
@ComponentScan(basePackages = {"test"},
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX,
pattern = "test.service.*"))
public class MyAppConfig {
}
@Qualifier
- @Autowired 默认按类型注入,当类型存在多个时使用 @Qualifier按名称指定注入
@Primary
- 当类型存在多个时,给类指定@Primary作为首选注入即可
对于大型项目的扫描优化
- 当类非常多时,可以添加下面的依赖,在编写程序时,spring会自动建立扫描索引,使得启动spring项目速度增快
spring 注解的替代
profile
- 适应不同环境
- 示例:配置生产环境和开发环境的配置文件
- 示例:配置生产环境和开发环境的Bean,灵活切换开发/生产环境
- 使profile生效
-
* 先不传入 配置类,不进行初始化容器(扫描),先配置 环境
-
* 再传入配置类
-
* 最后使用 refresh() 初始化容器,扫描