初识springboot @propertysource @importresource @bean

@propertysource

加载指定的配置文件。

@PropertySource(value={"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated//校验
public class person {
    //@Value("${person.lastname}")//赋值
    //@Email//lastname 必须填成邮箱格式和@validated配套使用
    private String lastname;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String,Object>maps;
    private List<Object> lists;
    private Dog dog;

@importresource导入spring中的配置文件,让配置文件的内容生效。

我们自己编写的beans.xml没用,spring boot里面没有spring的配置文件,

想让spring配置加载进来需要这个注解。

标注在一个配置类上。

在主函数中

@ImportResource(locations = {"classpath:beans.xml"})

不编写spring的配置文件了。

<?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>

spring boot推荐的给容器中添加组件的方式:

1.配置类==配置文件

@Configuration//指明当前类是一个配置类
//就是来替代spring配置文件
public class myappconfig {

    @Bean//这个注解来添加容器
    //将方法的返回值添加到容器中 容器中这个组件默认的id是这个方法名
    public helloservice helloservice1()//测试时返回的是函数名是后面的这个helloservice
    {
        System.out.println("添加组件了");
        return new helloservice();

    }

测试模块的代码

@Test
public void testhelloservice()
{
    boolean b=ioc.containsBean("helloservice1");//双引号里要和上面的函数名相同。
    System.out.println(b);
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值