spring boot加载配置文件
1.通过value("${}")
-
pom
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
-
application.yml
spring: application: name: configdemo server: port: 8081 test: name: tom password: admin123
-
启动类
package com.zjb; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author zhaojianbo */ @SpringBootApplication public class ConfigDemoApplication { public static void main(String[] args) { SpringApplication.run(ConfigDemoApplication.class); } }
-
dao层
package com.zjb.dao; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author zhaojianbo */ @Component public class User { @Value("${test.name}") private String name; @Value("${test.password}") private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
-
controller
package com.zjb.controller; import com.zjb.dao.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author zhaojianbo */ @RestController @RequestMapping("/config") public class HelloController { @Autowired User user; @RequestMapping("hello") public String hello() { return user.toString(); } }
2.通过注解自动注入
-
pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
-
dao层
package com.zjb.dao; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * @author zhaojianbo */ @Component @ConfigurationProperties(prefix = "test") //如果是默认的配置文件则不用写PropertySource @PropertySource("classpath:application.yaml") //@ConfigurationProperties(locations = "classpath:application.yaml", prefix = "test") public class Person { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
-
controller
package com.zjb.controller; import com.zjb.dao.Person; import com.zjb.dao.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author zhaojianbo */ @RestController @RequestMapping("/config") public class HelloController { @Autowired User user; @Autowired Person person; @RequestMapping("user") public String user() { return user.toString(); } @RequestMapping("person") public String person() { return person.toString(); } }
3.通过Bean和ConfigurationProperties
-
@bean,表示的是一个bean,该注解放在方法上,方法表示为配置文件中的id,返回值表示配置文件中的class
-
dao层
package com.zjb.dao; /** * @author zhaojianbo */ public class People { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "People{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
-
设置配置类
package com.zjb; import com.zjb.dao.People; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; /** * @author zhaojianbo */ @SpringBootApplication public class ConfigDemoApplication { @Bean @ConfigurationProperties(prefix = "test") public People peopleInstance() { return new People(); } public static void main(String[] args) { SpringApplication.run(ConfigDemoApplication.class); } }
-
controller
package com.zjb.controller; import com.zjb.dao.People; import com.zjb.dao.Person; import com.zjb.dao.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author zhaojianbo */ @RestController @RequestMapping("/config") public class HelloController { @Autowired User user; @Autowired Person person; @Autowired People people; @RequestMapping("user") public String user() { return user.toString(); } @RequestMapping("person") public String person() { return person.toString(); } @RequestMapping("people") public String people() { return people.toString(); } }
4.ConfigurationProperties以及EnableConfigurationProperties注解
当ConfigurationProperties没有与component一起使用时,ConfigurationProperties标注的类没有加载到容器中,EnableConfigurationProperties使类可以加载到容器中
-
dao类
package com.zjb.dao; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @author zhaojianbo */ @ConfigurationProperties(prefix = "test") public class Man { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "Man{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
-
controller
package com.zjb.controller; import com.zjb.dao.Man; import com.zjb.dao.People; import com.zjb.dao.Person; import com.zjb.dao.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author zhaojianbo */ @RestController @RequestMapping("/config") @EnableConfigurationProperties(Man.class) public class HelloController { @Autowired User user; @Autowired Person person; @Autowired People people; @Autowired Man man; @RequestMapping("user") public String user() { return user.toString(); } @RequestMapping("person") public String person() { return person.toString(); } @RequestMapping("people") public String people() { return people.toString(); } @RequestMapping("man") public String man() { return man.toString(); } }