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中一般默认是 ...
随机推荐
- 概率分布之间的推导关系 | Univariate Distribution Relationships
Univariate Distribution Relationships APPL: A Probability Programming Language Maplesoft- Software f ...
- day1-6 字符串、列表、元组、字典、类型转换
day1 1.python历史. 宏观上:python2 与 python3 区别: python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码. 2.python的环 ...
- shiro会话管理
Shiro提供了完整的企业级会话管理功能,不依赖于底层容器(如web容器tomcat),不管JavaSE还是JavaEE环境都可以使用,提供了会话管理.会话事件监听.会话存储/持久化.容器无关的集群. ...
- 成功解决You are using pip version 9.0.1, however version 9.0.3 is available. You should consider upgra
解决问题 You are using pip version 9.0.3, however version 10.0.1 is available.You should consider upgrad ...
- Leetcode 999. 车的可用捕获量
999. 车的可用捕获量 显示英文描述 我的提交返回竞赛 用户通过次数255 用户尝试次数260 通过次数255 提交次数357 题目难度Easy 在一个 8 x 8 的棋盘上,有一个白色车(r ...
- call、apply、bind三者的区别
先构造函数let xiaowang={ name1:"小王", age:", sex:"男", say:function(){ console.log ...
- 提高Bash使用效率的方法
环境:centos6.5 1.移动 Ctrl + a :移到命令行首Ctrl + e :移到命令行尾 Ctrl + xx:在命令行首和光标之间移动 左右键移动字符 Ctrl+左右键移动单词(不记快捷键 ...
- tar打包时的排除选项
1. 在命令行排除文件时,用 --exclude, 可以用多个--exclude . 可以用=号,也可以不用 如 tar -cvf ./aaa.tar ./tvc --exclude ...
- vue虚拟DOM源码学习-vnode的挂载和更新流程
代码如下: <div id="app"> {{someVar}} </div> <script type="text/javascript& ...
- 一、JAVA内存区域与内存溢出异常
在虚拟机自动内存管理机制的帮助下,不在需要为每一个操作区写相对应的delete/free代码来进行内存释放.进而不容易出现内存泄露和内存溢出的问题,由虚拟机管理内存,貌似这一切看起来很好.也正是因为j ...