Spring Boot 学习路(四) 创建Bean类与非全局配置文件

前面说到在全局配置文件中更改配置属性,那么如果在项目中我们需要更改的文件很多,那当然不能全部放在全局配置文件中,这样会使全局配置文件看起来很臃肿,那怎么办 ???这里可以用到 @PropertySource() 注解;

在 resources 文件夹中新建一个以 .properties 结尾的配置文件,

在 Bean 类中添加上面的注解,如:

/**
 * @ConfigurationProperties 告诉SpringBoot,将本类中的所有属性和配置文件中的相关属性进行绑定。
 * prefix = "Person"  是指配置文件中Person 下的所有的属性进行一一映射
 */
//@PropertySource(value = {"classpath:person.properties"})    加载指定名称的非全局配置文件,可以写多个(数组模式)
@PropertySource(value = {"classpath:person.properties"})
//@Component 将该对象添加至spring容器中
@Component
//在配置文件中查找指定(Person)对象下的所有属性,如果没有 @PropertySource 注解则是在全局配置文件中查找,如果有,则是在指定配置文件中查找
@ConfigurationProperties(prefix = "Person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boos;
    private Date birth;

    private Map<String , Object> maps1;
    private List<Object> lists1;
    private Dog dog;

运行结果

 

 

那么如果还要自己手写一个xml文件进行配置呢?这里就可以用到 @importResource() 注解

首先我们随便写一个类,类中可以没有任何东西,如:

public class HelloService {
}

然后写一个 .xml 文件,文件名随便起,我这里叫作 helloService.xml ,如:

<?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.ebring.springboot.service.HelloService"></bean>

</beans>

接着我们在测试类中测试 ID 是 helloService 这个组件有没有加载进spring容器中,

在测试类中编写如下代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot01HelloworldQuickApplicationTests {

    //将 ioc 注入
    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService() {
        //利用ioc对应方法判断容器中有没有名为 helloService 的对象,有则返回true,反之为false
        boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

}

测试后得结果为:

 

说明在未使用 @importResource() 注解情况下,该组件是不能加载进容器里面的

接下来我们在配置类上添加该注解,如:

@SpringBootApplication
//扫描该 xml 文件,并将其中的 id 添加到spring容器中
@ImportResource(locations = {"classpath:helloService.xml"})
public class SpringBoot01HelloworldQuickApplication {


    public static void main(String[] args) {
        SpringApplication.run(SpringBoot01HelloworldQuickApplication.class, args);
    }

}

 

这时候再运行测试程序

此时测试结果为 true

 

但是在项目中这样编写组件会很麻烦,在SpringBoot中,提供的方式是使用配置类进行加载组件。

我们将上面的 @importResource() 注解注释,然后在包里面编写一个配置类,

//@Configuration 该注解是告诉SpringBoot,该类是个配置类,配置类中,此注解必须存在
@Configuration
public class MyAppConfig {

    // @Bean 该注解等同于 xml 中的<bean></bean>标签,是将此方法的返回对象加载进组件中,方法名字同bean标签中的 id 名称。
    @Bean
    public HelloService helloService(){
        return new HelloService();
    }
}

这时候在进行测试,

得到结果仍是 true

我们尝试把方法名进行一下改动

//@Configuration 该注解是告诉SpringBoot,该类是个配置类,配置类中,此注解必须存在
@Configuration
public class MyAppConfig {

    // @Bean 该注解等同于 xml 中的<bean></bean>标签,是将此方法的返回对象加载进组件中,方法名字同bean标签中的 id 名称。
    @Bean
    public HelloService helloService1(){
        return new HelloService();
    }
}

并进行测试

测试结果即为false

此测试这表明,在配置类中经 @Bean 标注的方法,其方法名称就是加载进Spring容器中的组件名称。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值