@ 4.配置文件值注入@Value
@Value:1.字面量 2.#{spel} 3.${key}从环境变量配置文件中取值
@Value获取值和@ConfigurationProperties获取值比较
松散语法绑定:last_name = last-name = lastName 他们取的值都是相同的
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
获取配置文件中的某个值:
@ResponseBody @Controller public class Helloword {
@Value("${person.name}")
private String name;
@RequestMapping("/hello")
public String hello(){
return "Hello tow!" + name;
}
}
5@PropertySource,@ImportResource
@PropertySource: 加载指定的配置文件
@PropertySource(value = {"classpath:/person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private boolean boss;
private Date bir;
...
}
@ImportResource
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效; Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别; 想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
@ImportResource(locations = {"classpath:spring.xml"})
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
<?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="hello" class="com.cr.springboot01.ImportSoource.helloword"></bean>
</beans>
@Autowired
ApplicationContext app;
@Test
public void testImportSourcr(){
boolean h = app.containsBean("hello");
System.out.println(h);
}
@Bean
全注解方式:
新建一个配置类
//配置类,指明当前类使配置类,代替xml配置文件
@Configuration
public class MyConfig {
//将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
@Bean
public Helloword hello(){
System.out.println("注解的方式");
return new Helloword();
}
}
@Autowired
ApplicationContext app;
@Test
public void testImportSourcr(){
boolean h = app.containsBean("hello");
System.out.println(h);
}
Spring ApplicationContext 容器
和 BeanFactory 类似,它可以加载配置文件中定义的 bean,将所有的 bean 集中在一起,当有请求的时候分配 bean。
@propertySource(value = {"class path:person.properties"}) 类路径下的
@ImportResource:导入spring的配置文件,并让它的内容生效。
Spring boot 里面没有spring的配置文件,我吗自己编写的配置文件也不能自动识别
想让spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
springboot 中类上添加@Configuration 标签
spring在配置类中用<bean></bean>标签添加组件
@Bean将方法的返回值添加到容器中
容器中这个组件默认的ID就是方法名
1、配置类====Spring配置文件
2、使用@Bean给容器中添加组件
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
public void contextLoads() {
System.out.println(person);
}
@Test
public void test() {
boolean b = ioc.containsBean("hello");
System.out.println(b);
}
}
package com.example.demo.config;
import com.example.demo.service.TestService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
@Bean
public TestService hello() {
System.out.println("配置类@Bean给容器添加组件了");
return new TestService();
}
}