用处
根据类型校验和管理application中的bean
举例使用
application.properties的内容
test.name=default
test.age=19
test.friends[0]=Ben
test.friends[1]=Alice
test.utils[0]=DateUtils
test.utils[1]=TimeUtils
新建TestProperties用来接收配置的内容
package com.yxj.spring.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring.properties
* @Description: @ConfigurationProperties(prefix="test"),该注解会自动,将配置文件前缀是test的各项,加载至该bean中
* @Author: 阿杰
* @CreateDate: 2019/1/23 20:00
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/23 20:00
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@ConfigurationProperties(prefix="test")
public class TestProperties {
private String age;
private String name;
private String[] friends;
private List utils;
public String[] getFriends() {
return friends;
}
public TestProperties setFriends(String[] friends) {
this.friends = friends;
return this;
}
public String getAge() {
return age;
}
public TestProperties setAge(String age) {
this.age = age;
return this;
}
public String getName() {
return name;
}
public TestProperties setName(String name) {
this.name = name;
return this;
}
public List getUtils() {
return utils;
}
public TestProperties setUtils(List utils) {
this.utils = utils;
return this;
}
@Override
public String toString() {
return "TestProperties{" +
"age='" + age + '\'' +
", name='" + name + '\'' +
", friends=" + Arrays.toString(friends) +
", utils=" + utils +
'}';
}
}
springboot启动类
package com.yxj.spring;
import com.yxj.spring.properties.TestProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@Import(TestProperties.class)
@SpringBootApplication
public class SpringBootTestRun {
/**
* getBeansOfType(MakeApp.class)会装配bean类型是MakeApp的所有实例
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("------------------分割线------------------");
System.out.println(run.getBean(TestProperties.class));
}
}
测试结果
------------------分割线------------------
TestProperties{age='19', name='default', friends=[Ben, Alice], utils=[DateUtils, TimeUtils]}
注意:
@ConfigurationProperties(prefix="test"),该注解会自动,将配置文件前缀是test的各项,加载至该bean中
需要给定各个属性值的get,set方法
名字需要与配置项的key值对应
可以加载数组,集合
需要@Component注解或者其他方式将该类交由spring管理
解析
1.springBoot启动类会使用@SpringBootApplication
2.点进入源代码发现改注解是一个复合注解,由好几个注解共同组合而成
@SpringBootConfiguration标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。
@EnableAutoConfiguration,自动配置注解,自动载入应用程序所需的所有Bean
@ComponentScan:默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。这也就是springBoot启动类建立在项目根目录的原因
尝试
package com.yxj.spring;
import com.yxj.spring.properties.TestProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@Import(TestProperties.class)
@SpringBootApplication
public class SpringBootTestRun {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("------------------分割线------------------");
System.out.println(run.getBean(TestProperties.class));
}
}
输出结果
------------------分割线------------------
TestProperties{age='19', name='default', friends=[Ben, Alice], utils=[DateUtils, TimeUtils]}
总结
将@SpringBootApplication替换为@EnableAutoConfiguration,还是能正常输出spring管理的bean对象
@EnableAutoConfiguration注解导入了AutoConfigurationImportSelector类,最终的接口类是ImportSelector
源代码注释解释了,ImportSelector 该接口的方法的返回值都会被纳入到spring容器管理中
ImportSelector的具体使用与自定义,可以查看大佬博客https://blog.csdn.net/weixin_34452850/article/details/82883033
步骤
自定义事件,一般是继承ApplicationEvent抽象类
Spring的事件监听器接口,所有的监听器都实现了ApplicationListener接口
发布事件
代码实现
定义自定义事件
package com.yxj.spring.monitor;
import org.springframework.context.ApplicationEvent;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring.monitor
* @Description: 定义事件
* @Author: 阿杰
* @CreateDate: 2019/1/23 22:16
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/23 22:16
* @UpdateRemark: The modified content
* @Version: 1.0
*/
public class MyEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public MyEvent(String source) {
super(source);
}
}
定义事件监听器
package com.yxj.spring.monitor;
import org.springframework.context.ApplicationListener;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring.monitor
* @Description: 定义事件监听器
* @Author: 阿杰
* @CreateDate: 2019/1/23 22:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/23 22:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
public class MyApplicationListener implements ApplicationListener {
@Override
public void onApplicationEvent(MyEvent event) {
System.out.println("接收到事件:"+event.getClass());
}
}
springBoot启动类
package com.yxj.spring;
import com.yxj.spring.monitor.MyApplicationListener;
import com.yxj.spring.monitor.MyEvent;
import com.yxj.spring.properties.TestProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暂无
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@Import(TestProperties.class)
@SpringBootApplication
public class SpringBootTestRun {
public static void main(String[] args) {
ApplicationListener myApplicationListener = new MyApplicationListener();
MyEvent myEvent = new MyEvent("");
SpringApplication springBootTestRun = new SpringApplication(SpringBootTestRun.class);
springBootTestRun.addListeners(myApplicationListener);
ConfigurableApplicationContext run = springBootTestRun.run(args);
run.publishEvent(myEvent);
run.close();
}
}
测试结果
接收到事件:class com.yxj.spring.monitor.MyEvent
重点
将监听器添加至spring容器管理几种方式
1.springBootTestRun.addListeners(myApplicationListener);
2.在类上面加入@Component注解,@Service等注解
3.在application.properties添加参数context.listener.classes=监听器类的项目全路径
4.通过@Eventlistener注解,方法参数为监听的类ApplicationEvent或者其子类,一些自定义的监听器,或者springBoot系统启动时加载初始化信息等都可以通过ApplicationEvent+改变参数来实现监听