注解

1.@PostConstruct

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

  1. 通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
  2. 通过 在xml中定义init-method 和  destory-method方法
  3. 通过bean实现InitializingBean和 DisposableBean函数式接口,重写afterPropertiesSet()方法和destroy()方法

2.@controller 

@controller 、@service、@repository 、@component 注解的类,都会把这些类纳入进spring容器中进行管理,也就是注册为bean。

使用这些注解后需要:

1.创建一个配置类,在配置类上添加 @ComponentScan 注解,指定扫描的包。该注解默认会扫描指定的包下所有的组件类,纳入spring容器进行管理

 2.在spring配置文件中添加 <context:component-scan>。

3.@componentScan

@componentScan会扫描特定包下的类,把这些类纳入进spring容器中进行管理,也就是注册为bean。

4. @Profile

Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;

开发环境develop、测试环境test、生产环境master;数据源:(/dev) (/test) (/master) 

 @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件

  •   加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
  •   写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
复制代码
 1 /**
 2  * Profile:
 3  *         Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
 4  * 
 5  * 开发环境develop、测试环境test、生产环境master
 6  * 数据源:(/dev) (/test) (/master)
 7  *
 8  * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
 9  * 
10  * 1) 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
11  * 2) 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
12  * 
13  */
14 @PropertySource("classpath:/dbconfig.properties")
15 @Configuration
16 public class MainConfigOfProfile implements EmbeddedValueResolverAware{
17     
18     @Value("${db.user}")
19     private String user;
20     
21     private String driverClass;
22     
23     @Profile("default")
24     @Bean("test")
25     public DataSource testDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
26         ComboPooledDataSource dataSource = new ComboPooledDataSource();
27         dataSource.setUser(user);
28         dataSource.setPassword(password);
29         dataSource.setDriverClass(driverClass);
30         return dataSource;
31     }
32     
33     @Profile("dev")
34     @Bean("dev")
35     public DataSource devDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
36         ComboPooledDataSource dataSource = new ComboPooledDataSource();
37         dataSource.setUser(user);
38         dataSource.setPassword(password);
39         dataSource.setDriverClass(driverClass);
40         return dataSource;
41     }
42     
43     @Profile("master")
44     @Bean("master")
45     public DataSource masterDataSource(@Value("${db.password}")String password) throws PropertyVetoException {
46         ComboPooledDataSource dataSource = new ComboPooledDataSource();
47         dataSource.setUser(user);
48         dataSource.setPassword(password);
49         dataSource.setDriverClass(driverClass);
50         return dataSource;
51     }
52  
53     public void setEmbeddedValueResolver(StringValueResolver resolver) {
54         String driverClass = resolver.resolveStringValue("${db.driverClass}");
55         this.driverClass = driverClass;
56     }
57  
58 }
View Code
复制代码

 5.@bean

Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。
(和xml配置中的bean标签的作用是一样的)

复制代码
 1 @Configuration
 2 public class JavaConfig {
 3         @Bean
 4         public FunctionService functionService() {
 5             return new FunctionService();
 6         }
 7         @Bean
 8          public UseFunctionService useFunctionService(FunctionService 
 9              functionService){
10              UseFunctionService useFunctionService = new UseFunctionService();
11              useFunctionService.setFunctionService(functionService);
12              return useFunctionService;
13          }
14 }
View Code
复制代码
  • 我们使用的是Java配置的方式和注解混合配置。没有使用xml配置加注解的方式,Java配置的方式是Spring 4.x推荐的配置方式,可以完全代替xml配置。
  • 使用@Configuration表明当前类是一个配置类。此处没有使用包扫描,是因为所有的Bean都在类中定义了。
  • Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。使用@Bean注解的好处就是能够动态获取一个Bean对象,能够根据环境不同得到不同的Bean对象。或者说将Spring和其他组件分离(其他组件不依赖Spring,但是又想Spring管理生成的bean)。这种方法相当于在xml中配置bean标签,配置类相当于配置文件,方法名是bean的id。

Spring中提供了三种主要的装配机制

  • 在xml中进行显式配置
  • 在java中进行显式配置
  • 隐式bean发现机制和自动装配

第一种就是在xml配置文件中使用bean标签声明。
而上面的使用的是就是就是第二种。配置类+@bean注解
第三种的隐式bean发现机制就是Spring支持扫描使用@Service、@Compent、@Repository、@Controller的类,并注册成Bean。注解
自动装配指的就是Spring会尝试满足在使用@Autowired的属性值和方法相对应的依赖(属性值就是本身的实体,方法就是参数中的依赖),并将符合的bean填充过来。

配置类+@bean注解生成bean,如果有依赖其他bean,需要通过参数传递依赖bean;

 

6.@Lazy

bean对象只有在被第一次引用(使用)的时候才会加载,对应的是项目启动就加载bean对象。

posted @   堤苏白  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示