SpringBoot -- 常用注解详解

1、@ConfigurationProperties(prefix = "")

作用是告诉SpringBoot将本类中的所有属性和配置文件中相关的属性进行绑定;

将配置文件中配置的每一个属性的值,映射到这个组件中。

(1) 导入依赖

<!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐configuration‐processor</artifactId>
<optional>true</optional>
</dependency>

(2)application.yml:

person:
  laseName: wang
  age: 19
  boss: false
  birthday: 1999/12/12
  map : {"address1": "上\n海", "address2": '松\n江'}
  list:
    - 宝马
    - 奔驰
  dog:
    name: 拉布拉多
    age: 3

(3)Person.java

需要加入容器中:只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能

/**
 * 将配置文件中属性的值,映射到这个组件
 * @ConfigurationProperties: 告诉springboot将本类中所有属性和配置文件的配置进行绑定
 * 同时,需要保证这个组件是容器中的组件 -> @Component
 * @author zhou
 * @create 2020/5/9
 */

@Component
@ConfigurationProperties(prefix = "person")
//@PropertySource(value = {"classpath:person.properties"})
public class Person {
    private String laseName;
    private Integer age;
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> map;
    private List<Object> list;
    private Dog dog;

(4) properties配置文件在idea中默认utf-8可能会乱码

配置文件yml还是properties他们都能获取到值; 

2、@Value 

也可以用来获取值

(1) application.properties

person.lase-name=李四${random.uuid}
person.age=${random.int}
person.boss=true
person.birthday=2019/11/11
person.map.key='li'
person.map.value='四'
person.list='li','四'
person.dog.name=${person.lase-name}_'li'
person.dog.age=4

(2) 将@ConfigurationProperties 注释掉,使用@Value注解

 (3)@Value 不支持复杂类型封装,比如: map

3、@ConfigurationProperties 与  @Value

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

@RestController
public class SayController {
    @Value("${person.lase-name}")
    private String name;

    @GetMapping("/sayhello")
    public String sayHi() {
        return "hello, " + name;
    }

}

4、@PropertySource & @ImportResource & @Bean

4.1 、@PropertySource 

作用:加载指定的配置文件

(1)@ConfigurationProperties(prefix = "person") 默认从全局配置文件中获取值,我们并不会把所有的配置都放到application.properties,有时候想把与SpringBoot无关的配置提取出来,放到一个配置文件中。

(2)person.properties

person.lase-name=老王
person.age=22
person.boss=true
person.birthday=2019/11/11
person.map.key='li'
person.map.value='四'
person.list='li','四'
person.dog.name='li'
person.dog.age=4

(3) 使用 @PropertySource(value = {"classpath:person.properties"}) :告诉SpringBoot加载类路径下的person.properties的内容,并绑定到Person对象中的属性

//@ConfigurationProperties(prefix = "person")
@Component
@PropertySource(value = {"classpath:person.properties"})
public class Person {
    private String laseName;
    private Integer age;
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> map;
    private List<Object> list;
    private Dog dog;

4.2 @ImportResource

作用:导入Spring的配置文件,让配置文件里面的内容生效。

在实际开发,我们  给容器中加组件时,不可能采用这种方式:写一个配置文件,然后将配置文件导入进来生效。不来编写Spring的配置文件了,因为这样非常麻烦。

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件加载进来生效的话,我们需要使用@ImportResource标注在一个配置类上(配置了@SpringBootApplication的类)

(1)beanx.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx">

    <bean id="helloService" class="com.dhu.service.HelloService"></bean>
</beans>    

(2) HelloService.java

package com.dhu.service;

/**
 * @author zhou
 * @create 2020/5/9
 */
public class HelloService {
    public String sayHi(String str){
        return "hi";
    }
}

(3) SpringbootQuickstartApplication.java  主配置类

@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class SpringbootQuickstartApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuickstartApplication.class, args);
    }
}

(4)测试

@RunWith(SpringRunner.class) //使用Spring驱动器运行单元测试
@SpringBootTest
class SpringbootQuickstartApplicationTests {
    @Autowired
    private Person p;
    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloServcie() {
        boolean flag = ioc.containsBean("helloService");
        System.out.println(flag);
    }

4.3  SpringBoot推荐给容器中添加组件的方式:推荐使用全注解的方式 (推荐)

1、配置类@Configuration ------> 代替Spring配置文件
2、使用@Bean给容器中添加组件

(1)配置类 MyConfig.java

@Configuration //此注解代表配置类,当扫描到会在容器中创建对象
public class MyConfig {

    @Bean //当扫描到@Bean,此方法会自动执行,把方法返回值放到spring容器,容器中这个组件默认的id就是方法名
    public HelloService helloService() {
        System.out.println("@Bean给容器中添加组件");
        return new HelloService();
    }
}

(2) 不使用@ImportResource(locations = {"classpath:beans.xml"})

//@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringbootQuickstartApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuickstartApplication.class, args);
    }
}

(3)测试

id 为 : "helloService"

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloServcie() {
        boolean flag = ioc.containsBean("helloService");
        System.out.println(flag);
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值