SpringBoot学习笔记(4)-@Value注解以及@PropertySource & @ImportResource & @Bean注解

一、@Value注解

之前我们都用的

@ConfigurationProperties(prefix = "xxxx")

这个注解拿到配置文件中的值,其实我们还可以利用@Value注解

 再运行下

                          @Value获取值和@ConfigurationProperties获取值比较

  @ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型(例如Map、List)封装 支持 不支持

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

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

配置文件注入值数据校验

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * <bean/>
     */

   //lastName必须是邮箱格式
    @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;

    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

二、@PropertySource & @ImportResource & @Bean

我们把application.properties的内容删掉,重新创建一个配置文件

@PropertySource:加载指定的配置文件;

我们把代码更改成下面这个样子:

 也就是加上了@PropertySource注解

运行测试

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

先写一个Spring的配置文件,并且创建对应的配置类helloService

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

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

然后进行测试,测试代码如下,来看看ioc容器中是否含有我们定义的这个bean:

package com.dm.springboot;

import com.dm.bean.Person;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class Test {
    @Autowired
    Person person;
    @Autowired
    ApplicationContext ioc;

    @org.junit.Test
    public void TestHelloService(){
        boolean a=ioc.containsBean("helloService");
        System.out.println(a);
    }
    
    @org.junit.Test
    public void contextLoads(){
        System.out.println(person);
    }
}

 可以看到结果是false

我们此时在启动类上加注解:

@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效

 再次运行:

就可以看到Ioc容器中已经包含我们注入的bean了

SpringBoot推荐给容器中添加组件的方式;推荐使用全注解的方式

1、配置类@Configuration------>Spring配置文件

2、使用@Bean给容器中添加组件

先把启动类的@ImportResource加上注解

 然后创建一个配置类

代码:

/**
 * @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
 *
 * 在配置文件中用<bean><bean/>标签添加组件
 *
 */
@Configuration
public class MyAppConfig {
    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService02(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }
}

为了验证容器中这个组件默认的id就是方法名,我们先不改测试类的代码也就是这里

先进行测试

 更改后在再进行测试

猜你喜欢

转载自www.cnblogs.com/dmzna/p/13364948.html