springboot学习章节-spring常用配置
1、Scope
package com.zhen.highlights_spring4.ch2.scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; /**
* @author zhen
* @Date 2018/6/12 11:38
*/
@Service
@Scope("prototype")
public class DemoPrototypeService {
}
package com.zhen.highlights_spring4.ch2.scope; import org.springframework.stereotype.Service; /**
* @author zhen
* @Date 2018/6/12 11:37
*/
@Service
public class DemoSingletonService {
}
package com.zhen.highlights_spring4.ch2.scope; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* @author zhen
* @Date 2018/6/12 11:38
*/
@Configuration
@ComponentScan("com.zhen.highlights_spring4.ch2.scope")
public class ScopeConfig {
}
package com.zhen.highlights_spring4.ch2.scope; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 11:39
*/
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class);
DemoPrototypeService demoPrototypeService = context.getBean(DemoPrototypeService.class);
DemoPrototypeService demoPrototypeService1 = context.getBean(DemoPrototypeService.class);
System.out.println("demoPrototypeService1 == demoPrototypeService 结果是:" + (demoPrototypeService == demoPrototypeService1)); DemoSingletonService demoSingletonService = context.getBean(DemoSingletonService.class);
DemoSingletonService demoSingletonService1 = context.getBean(DemoSingletonService.class);
System.out.println("demoSingletonService1 == demoSingletonService 结果是:" + (demoSingletonService == demoSingletonService1)); context.close();
}
}
2、Spring EL
book.author=wangyunfei
book.name=spring boot
test.properties
你好
test.txt
package com.zhen.highlights_spring4.ch2.el; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; /**
* @author zhen
* @Date 2018/6/12 11:57
*/
@Service
public class DemoService { @Value("其他类的属性")
private String another; public String getAnother() {
return another;
} public void setAnother(String another) {
this.another = another;
}
}
package com.zhen.highlights_spring4.ch2.el; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource; /**
* @author zhen
* @Date 2018/6/12 11:58
*/
@Configuration
@ComponentScan("com.zhen.highlights_spring4.ch2.el")
@PropertySource("classpath:com/zhen/highlights_spring4/ch2/el/test.properties")
public class ElConfig { @Value("I Love You")
private String normal; @Value("#{systemProperties['os.name']}")
private String osName; @Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber; @Value("#{demoService.another}")
private String fromAnother; @Value("classpath:com/zhen/highlights_spring4/ch2/el/test.txt")
private Resource testFile; @Value("http://www.baidu.com")
private Resource testUrl; @Value("${book.name}")
private String bookName; @Autowired
private Environment environment; @Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
return new PropertySourcesPlaceholderConfigurer();
} public void outputResource(){
try{
System.out.println(normal);
System.out.println(osName);
System.out.println(randomNumber);
System.out.println(fromAnother); System.out.println(IOUtils.toString(testFile.getInputStream()));
System.out.println(IOUtils.toString(testUrl.getInputStream()));
System.out.println(bookName);
System.out.println(environment.getProperty("book.author"));
}catch (Exception e){ }
} }
package com.zhen.highlights_spring4.ch2.el; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 12:15
*/
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig reourceService = context.getBean(ElConfig.class); reourceService.outputResource(); context.close();
}
}
3、初始化和销毁
package com.zhen.highlights_spring4.ch2.prepost; /**
* @author zhen
* @Date 2018/6/12 13:13
*/
public class BeanWayService {
public void init(){
System.out.println("@Bean-init-method");
} public BeanWayService(){
super();
System.out.println("初始化构造函数-BeanWayService");
}
public void destory(){
System.out.println("@Bean-destory-method");
}
}
package com.zhen.highlights_spring4.ch2.prepost; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; /**
* @author zhen
* @Date 2018/6/12 13:17
*/
public class JSR250WayService {
@PostConstruct
public void init(){
System.out.println("jsr250-init-method");
} public JSR250WayService(){
super();
System.out.println("初始化构造函数-JSR250WayService");
} @PreDestroy
public void destory(){
System.out.println("jsr250-destory-method");
} }
package com.zhen.highlights_spring4.ch2.prepost; import org.springframework.context.annotation.Bean; /**
* @author zhen
* @Date 2018/6/12 13:19
*/
public class PrePostConfig { @Bean(initMethod = "init", destroyMethod = "destory")
BeanWayService beanWayService(){
return new BeanWayService();
} @Bean
JSR250WayService jsr250WayService(){
return new JSR250WayService();
}
}
package com.zhen.highlights_spring4.ch2.prepost; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 13:20
*/
public class Main { public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class); BeanWayService beanWayService = context.getBean(BeanWayService.class);
JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class); context.close();
} }
4、profile(为不同环境下使用不同配置提供支持)
package com.zhen.highlights_spring4.ch2.profile; /**
* @author zhen
* @Date 2018/6/12 13:24
*/
public class DemoBean { private String content; public DemoBean(String content) {
this.content = content;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}
package com.zhen.highlights_spring4.ch2.profile; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; /**
* @author zhen
* @Date 2018/6/12 13:24
*/
@Configuration
public class ProfileConfig { @Bean
@Profile("dev")
public DemoBean devDemoBean(){
return new DemoBean("from development profile");
} @Bean
@Profile("prod")
public DemoBean prodDemoBean(){
return new DemoBean("from production profile");
}
}
package com.zhen.highlights_spring4.ch2.profile; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 13:28
*/
public class Main { public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getEnvironment().setActiveProfiles("dev");
context.register(ProfileConfig.class);
context.refresh(); DemoBean demoBean = context.getBean(DemoBean.class);
System.out.println(demoBean.getContent()); context.close();
} }
5、事件(为bean与bean消息通信提供支持)
package com.zhen.highlights_spring4.ch2.event; import org.springframework.context.ApplicationEvent; /**
* @author zhen
* @Date 2018/6/12 13:31
*/
public class DemoEvent extends ApplicationEvent { private static final long serialVersionUID = 6639236243302861037L; private String msg; public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} public DemoEvent(Object source, String msg) {
super(source);
this.msg = msg;
}
}
package com.zhen.highlights_spring4.ch2.event; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* @author zhen
* @Date 2018/6/12 13:34
*/
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {
@Override
public void onApplicationEvent(DemoEvent demoEvent) {
String msg = demoEvent.getMsg();
System.out.println("我(bean-demoListener)接收到了bean-demoPublisher发布的信息:" + msg);
}
}
package com.zhen.highlights_spring4.ch2.event; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; /**
* @author zhen
* @Date 2018/6/12 13:36
*/
@Component
public class DemoPublisher {
@Autowired
ApplicationContext applicationContext; public void publish(String msg){
applicationContext.publishEvent(new DemoEvent(this, msg));
}
}
package com.zhen.highlights_spring4.ch2.event; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* @author zhen
* @Date 2018/6/12 13:37
*/
@Configuration
@ComponentScan("com.zhen.highlights_spring4.ch2.event")
public class EventConfig {
}
package com.zhen.highlights_spring4.ch2.event; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @author zhen
* @Date 2018/6/12 13:37
*/
public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);
DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
demoPublisher.publish("hello application event");
context.close();
}
}
springboot学习章节-spring常用配置的更多相关文章
- Spring 常用配置、Bean
spring模块 Spring-Core: Core包是框架的最基础部分,并提供依赖注入(Dependency Injection)管理Bean容器功能. Spring-Context:(Spring ...
- vue第二单元(webpack的配置-学习webpack的常用配置)
第二单元(webpack的配置-学习webpack的常用配置) #课程目标 掌握webpack的常用配置 掌握如何根据实际的需求修改webpack的对应配置 了解webpack-dev-server的 ...
- Spring常用配置使用示例
上篇介绍了Spring配置的基本情况,本篇介绍Spring常用配置具体如何使用.关于基础的配置,比如Configuration之类的就不示例,主要示例相对用的比较多同时可能比较复杂的标签或属性. 1) ...
- SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...
- SpringBoot学习(二)-->Spring的Java配置方式
二.Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @Bean Spring的Java配置方式是通过 @ ...
- Spring常用配置
----------------------------------------------------------------------------------------------[版权申明: ...
- Spring常用配置示例
Spring 是一款Java平台的开源框架,是为解决企业级应用程序开发的复杂性而创建的,通过良好的分层架构让开发人员能够专注于业务逻辑的开发. Spring框架是一个分层架构,由不同的模块组成,构成s ...
- Springboot学习:SpringMVC自动配置
Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...
- .net学习笔记----WebConfig常用配置节点介绍
一.配置文件入门 .Net提供了一种保存项目配置信息的办法,就是利用配置文件,配置文件的后缀一般是.config.在WinForm程序中配置文件一般是App.config.在Asp.net中一般默认是 ...
随机推荐
- idea ----> 使用idea工具整合mybaiti时出现的问题总结
使用idea测试mabtis实例时出现 java.lang.IllegalArgumentException: Mapped Statements collection does not conta ...
- Python入门学习指南--内附学习框架
https://blog.csdn.net/weixin_44558127/article/details/86527360
- Fiddler简介及web抓包
1.Fiddler界面如下 2.Fiddler开关 界面左下角或点击F12控制Fiddler开关,开关是“Capturing”: 启动之后,Fiddler代理永远是开着的. 3.浏览器代理 ...
- New Roads CodeForces - 746G (树,构造)
大意:构造n结点树, 高度$i$的结点有$a_i$个, 且叶子有k个. 先确定主链, 然后贪心放其余节点. #include <iostream> #include <algorit ...
- loj#528. 「LibreOJ β Round #4」求和
求:\(\sum_{i=1}^n\sum_{j=1}^m\mu(gcd(i,j))^2\) 化简可得\(\sum_{i=1}^{min(n,m)}{\lfloor \frac{n}{i} \rfloo ...
- vue项目 sockjs-node一直报错问题
vue3下 vue.config.js中 devServer: { host: '0.0.0.0', port: 8080, proxy: { '/': { target: 'http://127.0 ...
- leetcode-algorithms-10 Regular Expression Matching
leetcode-algorithms-10 Regular Expression Matching Given an input string (s) and a pattern (p), impl ...
- 兼容IE8的video写法
<video width="100%" height="515px" controls preload> <source src=" ...
- 2017-5-5/PHP实现负载均衡的加权轮询
1. 负载均衡算法有哪些? 轮询法:将请求按顺序轮流地分配到后端服务器上,它均衡地对待后端的每一台服务器,而不关心服务器实际的连接数和当前的系统负载. 随机法:通过系统的随机算法,根据后端服务器的列表 ...
- 2015-09-21 css学习1
3.设置背景图片 Background-image:url(相对路径) ----123.jpg 图片拉伸铺满: background-size:cover 铺满方向: background-repea ...