SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource

Spring Boot提倡基于Java的配置。这两篇博文主要介绍springboot 一些常用的注解介绍

v@value

通过@Value可以将外部的值动态注入到Bean中。

添加application.properties的属性,方便后面演示。

domain.name=cnblogs
    @Value("字符串1")
    private String testName; // 注入普通字符串

    @Value("#{systemProperties['os.name']}")
    private String systemPropertiesName; // 注入操作系统属性

    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private double randomNumber; //注入表达式结果

    @Value("${domain.name}")
    private String domainName; // 注入application.properties的配置属性

效果如下:

SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource

v@Import

SpringBoot 的 @Import 用于将指定的类实例注入之Spring IOC Container中。

package com.cnblogs.demo;
public class Dog {
 
}
package com.cnblogs.demo;
 
public class Cat {
 
}

在启动类中需要获取Dog和Cat对应的bean,需要用注解@Import注解把Dog和Cat的bean注入到当前容器中。

package com.cnblogs.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
 
//@SpringBootApplication
@ComponentScan
/*把用到的资源导入到当前容器中*/
@Import({Dog.class, Cat.class})
public class App {
 
    public static void main(String[] args) throws Exception {
 
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        System.out.println(context.getBean(Dog.class));
        System.out.println(context.getBean(Cat.class));
        context.close();
    }
}

v@ImportResource

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

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

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

v@PropertySource

自定义配置文件名称,多用于配置文件与实体属性映射。

person.properties

person.lastName=Jack
person.age=18
person.birth=2018/12/9
person.boss=true
person.maps.key1=value1
person.maps.key2=value2
person.lists=a,b,c
person.dog.name=tom
person.dog.age=1
@PropertySource(value = {"classpath:person.properties"})
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
    private String lastName;
    private Integer age;
    private boolean isBoss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
    ...setter/getter/toString...
}

这样一个注解(@PropertySource(value = {"classpath:person.properties"}))就可以搞定不在主配置里读取,按照不同的功能模块划分出不同的配置文件。

v补充

@Bean注解用在方法上,作用:将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名。springboot不推崇使用配置文件,推崇使用全配置方式开发,如何定义一个配置类呢?在类名上加@Configuration标签标明这是一个配置类,方法上则用@Bean

v源码地址

https://github.com/toutouge/javademosecond/tree/master/hellospringboot


作  者:请叫我头头哥
出  处:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

原文地址:https://www.cnblogs.com/toutou/p/9907753.html