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);
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章