SpringBoot学习笔记3

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值