Springboot注解@ConfigurationProperties、@Value、@PropertySource、@ImportResource、@Configuration的使用方法

1.@ConfigurationProperties与@Value

在application.properties当中写一个person的属性

id=3
person.name=tom
person.age=16

创建一个person类

package com.example.springboot;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//使用@Component和@ConfigurationProperties(prefix = "person")注入application.properties当中的person属性
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private int age;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

新建一个test.java

package com.example.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class test {
	//@Value("${id}")可以直接获取application.properties当中的属性
    @Value("${id}")
    private int id;
    // @Autowired可以创建一个person类
    @Autowired
    private Person person;
    @GetMapping("/hello")
    public String hello(){
        System.out.println(id);
        System.out.println("--------");
        System.out.println(person);
        return "hello";
    }
}

2.@PropertySource
在application.properties当中加入

cyy.name=abc

创建一个PropertySourceMain类

package com.example.springboot;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration // 表明 自定义配置类
@PropertySource("classpath:application.properties") // 指定自定义配置类
@EnableConfigurationProperties(PropertySourceMain.class)
@ConfigurationProperties(prefix = "cyy")
public class PropertySourceMain {
    private String name; // 配置文件 top-descr 使用松散绑定
    @Override
    public String toString() {
        return "person{" +
                "name='" + name + '\'' +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

test.java:

package com.example.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class test {

    @Autowired
    private PropertySourceMain propertySourceMain;
    @GetMapping("/hello")
    public String hello(){
        System.out.println(propertySourceMain);
        return "hello";
    }
}

3.@ImportResource
在resouces目录下新建一个beans.xml文件

bean标签当中的class表示该项目java文件夹下的目录结构

<?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="BeanXML" class="com.example.springboot.BeanXML"></bean>
</beans>

在SpringbootApplication.java文件中加入@ImportResource(“classpath:beans.xml”)注解

package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource("classpath:beans.xml")
@SpringBootApplication
public class SpringbootApplication {

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

}

BeanXML.java

package com.example.springboot;

public class BeanXML {
}

test.java:

package com.example.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class test {

    @Autowired
    private ApplicationContext applicationContext;
    @GetMapping("/hello")
    public String hello(){
        System.out.println(applicationContext.containsBean("BeanXML")); // 这里指的是ID
        return "hello";
    }
}

4.@Configuration
新建一个BeanConfiguration .java类

package com.example.springboot;

public class BeanConfiguration {
    
}

新建一个BeanConfigurationConfig.java类

package com.example.springboot;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.example.springboot.BeanConfigurationConfig;

@Configuration
public class BeanConfigurationConfig {

    @Bean
    public BeanConfiguration beanConfiguration() {
        return new BeanConfiguration();
    }
}

test.java:

package com.example.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class test {

    @Autowired
    private ApplicationContext applicationContext;


    @GetMapping("/hello")
    public String hello(){
        System.out.println(applicationContext.containsBean("beanConfiguration"));
        return "hello";
    }
}

目录结构:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值