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";
}
}
目录结构: