@PropertySource注解是将配置文件中 的值赋值给POJO

项目结构如下

一.创建一个Person.Java文件:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated; import javax.validation.constraints.Email;
import java.util.List;
import java.util.Map; @Component
@ConfigurationProperties(prefix = "person") //将全局配置文件(application.properties或者application.yml中的person)赋值给该bean
//下面这个注解的作用获取配置文件中的值,可以加载多个配置文件,放在{}里面,classpath表示类路径
@PropertySource(value={"classpath:person.properties"})
@Validated //表明这个类中的数据赋值要进行数据效验
public class Person { private String lastName; private Integer age; private Boolean boss;
private Map<String,Object> maps;
private List<String> lists; public Map<String, Object> getMaps() {
return maps;
} public void setMaps(Map<String, Object> maps) {
this.maps = maps;
} public List<String> getLists() {
return lists;
} public void setLists(List<String> lists) {
this.lists = lists;
} public Dog getDog() {
return dog;
} public void setDog(Dog dog) {
this.dog = dog;
} private Dog dog; @Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", maps=" + maps +
", lists=" + lists +
", 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;
}
}
dog类的如下:
package com.gan.springboot03.bean;

public class Dog {
private String name;
private Integer age; @Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

 创建一个person.properties文件,该文件的作用是给Person赋值
person.lastName=张三
person.age=12
person.birth=2017/2/2
persion.boss=true
person.maps.k1=v1
person.maps.k2=12
person.dog.name=dog
person.dog.age=15

在测试类中测试有没有获取到Person的值
package com.gan.springboot03;

import com.gan.springboot03.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest
public class SpringBoot03ApplicationTests {
@Autowired
public Person person; @Test
public void contextLoads() {
System.out.println(person);
} }

测试结果如下

 @@ImportResource注解是获取bean.xml配置文件

一般情况下,都是在applicationContext中配置bean组件,例如

在service包下创建一个HelloService类,类中的代码如下:

package com.gan.springboot03.service;

public class HelloService {
}

创建一个配置文件用来把HelloService组件放入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.gan.springboot03.service.HelloService"></bean>
</beans>

在核心类中,引入该配置文件
/**
* @improtResource注解也可以加入多个配置文件,不过在开发中写一个配置文件再配置,很麻烦
* springboot推荐使用组件,既创建一个配置类
*/
@ImportResource(locations = {"classpath:bean.xml"})
@SpringBootApplication
public class SpringBoot03Application { public static void main(String[] args) {
SpringApplication.run(SpringBoot03Application.class, args);
} }

在测试类中测试
package com.gan.springboot03;

import com.gan.springboot03.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest
public class SpringBoot03ApplicationTests { @Autowired
ApplicationContext ioc; /**
* 该方法是用来判断@importSource注解,该注解的作用是导入Spring里的配置文件
* 一般是加载在主配置类上,既启动类上
*/
@Test
public void helloService(){
//判断是否含有helloService这个bean
Boolean b=ioc.containsBean("helloService");
System.out.println(b);
}

测试结果如下,返回true

 @Bean注解是为了解决每创建一个bean,就创建一个Bean配置文件,再使用注解将配置文件引入到核心类中

再config包中创建一个类MyConfig,

package com.gan.springboot03.config;

import com.gan.springboot03.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* 配置类,用来加载Spring里面的配置文件
* Configuration注解表明这是一个配置类
*/
@Configuration
public class MyConfig { /**
* @Bean注解将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名
* @return
*/
@Bean
public HelloService helloService(){
System.out.println("配置类给HelloService配置了一个组件");
return new HelloService();
}
}

核心类配置 如下:注解掉引入配置文件的那行代码
package com.gan.springboot03;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource; /**
* @improtResource注解也可以加入多个配置文件,不过在开发中写一个配置文件再配置,很麻烦
* springboot推荐使用组件,既创建一个配置类
*/
//@ImportResource(locations = {"classpath:bean.xml"})
@SpringBootApplication
public class SpringBoot03Application { public static void main(String[] args) {
SpringApplication.run(SpringBoot03Application.class, args);
} }

测试类如下:
package com.gan.springboot03;

import com.gan.springboot03.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest
public class SpringBoot03ApplicationTests { @Autowired
ApplicationContext ioc; /**
* 该方法是用来判断@importSource注解,该注解的作用是导入Spring里的配置文件
* 一般是加载在主配置类上,既启动类上
*/
@Test
public void helloService(){
//判断是否含有helloService这个bean
Boolean b=ioc.containsBean("helloService");
System.out.println(b);
}
}

测试结果如下:返回一个true

												

Spring学习(三)——@PropertySource,@ImportResource,@Bean注解的更多相关文章

  1. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  2. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  3. (转)Spring的三种实例化Bean的方式

    http://blog.csdn.net/yerenyuan_pku/article/details/52832793 Spring提供了三种实例化Bean的方式. 使用类构造器实例化. <be ...

  4. Spring boot @PropertySource, @ImportResource, @Bean

    @PropertySource:加载指定的配置文件 /** * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉SpringBoot将本类 ...

  5. Spring基础学习(三)—详解Bean(下)

    一.Bean的生命周期 1.概述      Spring IOC容器可以管理Bean的生命周期,Spring 允许在Bean的生命周期的特定点执行定制的任务.      Spring IOC容器对Be ...

  6. Spring学习(5)---Bean的定义及作用域的注解实现

    Bean管理的注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean <context:annotation-config/> @Component,@Repository ...

  7. Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】

    自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器中注册了,要怎么去区分对哪一个Bean进行注入呢? 如下情况, ...

  8. Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】

    通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XML 的功能,也提供了自动装配的功能,采用了 ...

  9. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

随机推荐

  1. CSS-fontface

    @font-face { font-family: <identifier>; src: <fontsrc> [, <fontsrc>]*; <font> ...

  2. 十九 Listener

    Listener 监听器 一 监听器内部原理:其实就是接口回调 需求:A在执行循环,当循环到5的时候,通知B 事先先把某一个对象传递给A ,当A执行到5的时候,通过这个对象来调用B中的方法 但是不是直 ...

  3. python2学习------基础语法5(常用容器以及相关操作)

    1.list(列表) #生成数据list a=[x for x in range(10)]; #print a; #遍历list for i in a: pass; #print i; #追加元素 a ...

  4. greenplum 存储过程

    参考: https://docs.pivotal.io/search?q=function   官网 https://www.cnblogs.com/greenZ/p/8722081.html htt ...

  5. 12541:TNS无监听状态

    上次在项目上遇见数据库报这个问题,然后网上几乎都是让重新进行配置数据库.配置多次之后还是无效,最后找到了问题的根源. 使用的是Oracle数据库,用PLSQL登录报的这个错误. 在计算机全局搜索:li ...

  6. PAT (Advanced Level) 1128~1131:1128N皇后 1129 模拟推荐系统(set<Node>优化) 1130 中缀表达式

    1128 N Queens Puzzle(20 分) 题意:N皇后问题.按列依次给定N个皇后的行号,问N个皇后是否能同时不存在行冲突.列冲突和主副对角线冲突. 分析: 1.根据题意一定不存在列冲突,所 ...

  7. 我的Grunt之旅-序章

    时间:2018-03-05 13:52  事件:安装Grunt 相关网址: grunt官网:https://gruntjs.com node.js下载地址 :https://nodejs.org/en ...

  8. R WLS矫正方差非齐《回归分析与线性统计模型》page115

    rm(list = ls()) A = read.csv("data115.csv") fm = lm(y~x1+x2,data = A) coef(fm) A.cooks = c ...

  9. C语言中语句的跨行支持总结

    C语言中语句的跨行支持总结: 预处理一行写不下: 把一个预处理指示写成多行要用""续行,因为根据定义,一条预处理指示只能由一个逻辑代码行组成. 正常程序一行写不下: 把C代码写成多 ...

  10. 0105 springMVC开发基础

    背景 已经明确了MVC的思想和SpringMVC的基本流程,下面就都具体的mvc开发细节知识了. @RequestMapping springMVC核心流程中,启动阶段会把注解@RequeestMap ...