【springboot】引入外部配置文件

一、@PropertySource

在springboot中不是所有的配置内容都写到application.properties中,在配置较多的情况下,我们可能将不同模块或者不同业务的配置写到不同的配置文件中。那么针对这样的配置文件,springboot默认是不认识也不加载的。针对这样的情况,可以使用springboot中的@PropertySource来使配置文件生效。举例如下,定义一个properties文件:student.properties

student.password=lwb123
student.username=lwb123

然后再定义一个javabean对象:

@PropertySource(value = {"classpath:student.properties"})
@ConfigurationProperties(prefix = "student")
@Component
public class Student {
    @Value("${student.username}")
    private String username;
    @Value("${student.password}")
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Student{\n" +
                "username='" + username + '\'' +
                "\n password='" + password + '\'' +
                '}';
    }
}

定义一个service来获取Student对象:

@Service
public class StudentService {

    @Autowired
    private Student student;

    public void getStudent() {
        System.out.println(student);
    }

}

测试输出结果:

@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentServiceTest {

    @Autowired
    private StudentService studentService;

    @Test
    public void getStudentTest() {
        studentService.getStudent();
    }

}

二、@ImportResource导入spring配置文件

在原始的xml配置的spring时代,各种各样的xml配置,非常的多。很多自定义的xml需要导入进来才可以生效,现在使用sringboot中依然可以使用@ImportResource导入配置文件,使其生效。

定义dog.xml,当然由要给Dog的javabean,属性为name和age,这里不再写在展示出来

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="cn.lwb.springboot.dto.Dog">
        <property name="name" value="小gou狗"/>
        <property name="age" value="3" />
    </bean>

</beans>

在springboot的启动类上加上@ImportResource注解

@SpringBootApplication
@ImportResource(locations = {"classpath:dog.xml"})
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DogTest {

    @Autowired
    private Dog dog;

    @Test
    public void test() {
        System.out.println(dog);
    }

}

但是在springboot中,并不推荐使用xml的方式了,可是使用配置类的方式来实现:

//表示这是一个配置类
@Configuration
public class DogConfiguration {
    //表示该返回要注入到spring容器中,默认注入到容器中的bean名称就是方法的名称dog
    @Bean
    public Dog dog() {
        Dog dog = new Dog();
        dog.setAge(3);
        dog.setName("小奶狗");
        return dog;
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class DogTest {

    @Autowired
    private Dog dog;

    @Test
    public void test() {
        System.out.println(dog);
    }

}

三、配置文件占位符

① 随机数

${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
举个例子:

stu.happy=球球
stu.id=${random.uuid}
stu.age=${random.int}
stu.name=${stu.happy}_${stu.age}
//如果stu.hello 没有值,默认为 男
stu.sex=${stu.hello:男}
@RunWith(SpringRunner.class)
@SpringBootTest
public class RandomTest {

    @Value("${stu.id}")
    private String id;
    @Value("${stu.name}")
    private String name;
    @Value("${stu.age}")
    private String age;
    @Value("${stu.sex}")
    private String sex;

    @Test
    public void test() {
        System.out.println("id-->" + id);
        System.out.println("name-->" + name);
        System.out.println("age-->" + age);
        System.out.println("sex-->" + sex);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值