@Property:加载指定的配置文件。
创建person.properties文件
person.lastName=李四
person.age=18
person.boss=false
person.birth=2017/12/12
person.maps.k1=v1
person.maps.k2=v2
person.lists=lisi,zhaoliu
person.dog.name=小狗
person.dog.age=2
使用方式
package com.atguigu.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*
* 将配置文件的每一个属性的值,银蛇到这个组件中
* @ConfigurationProperties:告诉SpringBoot降本类中的所有属性和配置文件中相关的配置进行绑定
* prefix = "person":配置文件中那个下面的所有属性进行一一映射
* 只有这个组件是容器中的组件,才嫩给容器提供的@ConfigurationProperties功能
*
* @ConfigurationProperties默认从全局配置文件中获取值
*
*
* */
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
/*
*
* <bean class="person">
*
* <property name = "lastName" value="字面量/${key}从环境变量、配置文件中获取/#{SpEL}"></property>
*
* </bean>
* */
// @Value("${person.lastName}")
private String lastName;
// @Value("#{11*7}")
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
SpringBoot 里面没有Soring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource配置类上;
@ImportResource(locations ={"classpath:beans.xml"})导入spring的配置文件
代码实现如下
创建beans.xml文件 使用IDEA 中的new--xml configuration file ---Spring conifg
<?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="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>
创建HelloService组件
package com.atguigu.springboot.service;
public class HelloService
{
}
给springboot主程序添加
package com.atguigu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//使用@ImportResource给主程序添加HelloService组件
@ImportResource(locations ={"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
}
}
测试判断是否添加组件成功,在springboot test 的测试程序中添加代码如下
@Autowired
ApplicationContext ioc;
@Test
public void testHelloService(){
boolean b = ioc.containsBean("helloService");
System.out.println(b);
}
SpringBoot推荐给容器中添加组件的方式:推荐使用全注解的方式
1.配置类====SpringBoot文件
2.使用@Bean给容器中添加组件
创建配置MyAppConfig类,代码如下
package com.atguigu.springboot.config;
import com.atguigu.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Configuration 指明当前类是一个配置类,就是来替代之前的spring配置文件
*
* 在配置文件中用<bena></bean>标签添加
* */
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
}