iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot2底层注解@ConfigurationProperties配置绑定
  • 765
分享到

SpringBoot2底层注解@ConfigurationProperties配置绑定

2024-04-02 19:04:59 765人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot2底层注解@ConfigurationProperties的配置绑定配置绑定验证另一种方式springBoot2底层注解@ConfigurationPrope

springBoot2底层注解@ConfigurationProperties的配置绑定

我们通常会把一些经常变动的东西放到配置文件里。

比如之前写在配置文件application.properties里的端口号server.port=8080,另外常见的还有数据库的连接信息等等。

那么,我的数据库连接信息放在配置文件里,我要使用的话肯定得去解析配置文件,解析出的内容在 bean 里面去使用。

整个场景其实就是把配置文件里的所有配置,绑定到 java bean 里面。

要完成这个场景,基于 java 原生代码编写还是有点麻烦的。通常会做一个封装,读取到properties文件中的内容,并且把它封装到JavaBean中:

public class getProperties {
     public static void main(String[] args) throws FileNotFoundException, IOException {
         Properties pps = new Properties();
         pps.load(new FileInputStream("a.properties"));
         Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
         while(enum1.hasMoreElements()) {
             String strKey = (String) enum1.nextElement();
             String strValue = pps.getProperty(strKey);
             System.out.println(strKey + "=" + strValue);
             //封装到JavaBean
             ... ...
         }
     }

这里就是使用Properties类来加载配置文件a.properties,然后遍历配置文件中的每一个k-v,获取之后就可以用到对应的地方。

在 springboot 中简化了这个过程,这就是配置绑定。

配置绑定

通过使用注解@ConfigurationProperties来完成配置绑定,注意需要结合@Component使用。

新建一个组件Car,有2个属性分别是品牌和价格:

@Component
public class Car {
    private String brand;
    private Integer price;
// get set tostring 就不贴了

在配置文件application.properties,设置一些属性值,比如:

mycar.brand=QQmycar.price=9999

使用@ConfigurationProperties注解,加到组件上:

mycar.brand=QQ
mycar.price=9999

传进去的 prefix 是配置文件里的前缀,这里就是 mycar。

验证

现在来测试一下是否绑定成功,在之前的HelloController继续增加一个控制器方法:

@RestController
public class HelloController {
    @Autowired
    Car car;
    @RequestMapping("/car")
    public Car car() {
        return car;
    }
    @RequestMapping("/hello")
    public String Hello() {
        return "Hello SpringBoot2 你好";
    }
}

部署一下应用,浏览器访问Http://localhost:8080/car:

绑定成功。

另一种方式

除上述方法之外,还可以使用@EnableConfigurationProperties + @ConfigurationProperties的方式来完成绑定。

注意,@EnableConfigurationProperties注解要使用在配置类上,表示开启属性配置的功能:

//@ConditionalOnBean(name = "pet1")
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = true)
@ImportResource("classpath:beans.xml")  //配置文件的类路径
@EnableConfigurationProperties(Car.class) //开启属性配置的功能
public class MyConfig {
 
    @Bean("user1")
    public User user01(){
        User pingguo = new User("pingguo",20);
        pingguo.setPet(TomcatPet());
        return pingguo;
    }
 
    @Bean("pet22")
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}

@EnableConfigurationProperties(Car.class)传入要开启配置的类,这可以自动的把 Car 注册到容器中,也就是说之前 Car 上的@Component就不需要了。

//@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;

重新部署访问下地址,依然可以。

关于第二种的使用场景,比如这里的 Car 是一个第三方包里的类,但是人家源码没有加@Component注解,这时候就可以用这种方式进行绑定。

最后,要记住当使用@ConfigurationProperties(prefix = "mycar")这个配置绑定时,是跟 springboot 核心配置文件 application.properties文件的内容建立的绑定关系。

以上就是SpringBoot2底层注解@ConfigurationProperties配置绑定的详细内容,更多关于SpringBoot2注解配置绑定的资料请关注编程网其它相关文章!

--结束END--

本文标题: SpringBoot2底层注解@ConfigurationProperties配置绑定

本文链接: https://www.lsjlt.com/news/150005.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作