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常用配置的更多相关文章

  1. Spring 常用配置、Bean

    spring模块 Spring-Core: Core包是框架的最基础部分,并提供依赖注入(Dependency Injection)管理Bean容器功能. Spring-Context:(Spring ...

  2. vue第二单元(webpack的配置-学习webpack的常用配置)

    第二单元(webpack的配置-学习webpack的常用配置) #课程目标 掌握webpack的常用配置 掌握如何根据实际的需求修改webpack的对应配置 了解webpack-dev-server的 ...

  3. Spring常用配置使用示例

    上篇介绍了Spring配置的基本情况,本篇介绍Spring常用配置具体如何使用.关于基础的配置,比如Configuration之类的就不示例,主要示例相对用的比较多同时可能比较复杂的标签或属性. 1) ...

  4. SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池

    三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...

  5. SpringBoot学习(二)-->Spring的Java配置方式

    二.Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @Bean Spring的Java配置方式是通过 @ ...

  6. Spring常用配置

    ----------------------------------------------------------------------------------------------[版权申明: ...

  7. Spring常用配置示例

    Spring 是一款Java平台的开源框架,是为解决企业级应用程序开发的复杂性而创建的,通过良好的分层架构让开发人员能够专注于业务逻辑的开发. Spring框架是一个分层架构,由不同的模块组成,构成s ...

  8. Springboot学习:SpringMVC自动配置

    Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...

  9. .net学习笔记----WebConfig常用配置节点介绍

    一.配置文件入门 .Net提供了一种保存项目配置信息的办法,就是利用配置文件,配置文件的后缀一般是.config.在WinForm程序中配置文件一般是App.config.在Asp.net中一般默认是 ...

随机推荐

  1. python cook 2

    迭代器 iterator  生成器 generator 1.手动遍历迭代器 2.代理迭代 解释:将迭代操作代理到容器内部的对象上 操作:使用__iter()__,  for 循环遍历对象时,会自动调用 ...

  2. C#特性-表达式树

    表达式树ExpressionTree   表达式树基础 转载需注明出处:http://www.cnblogs.com/tianfan/ 刚接触LINQ的人往往觉得表达式树很不容易理解.通过这篇文章我希 ...

  3. OSPF - 1,基础

    1,OSPF知识点a)在OSPF中,如果是环回口宣告进OSPF,不管宣告时配置的是多少位掩码,路由器收到的都是32位.(EIGRP配了多少位就收到多少位).好处:EIGRP中,在PING包发起时如果在 ...

  4. linux权限管理之基本权限

    基本权限 UGO ======================================================== 文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件 ...

  5. Search中的剪枝-奇偶剪枝

    设有一矩阵如下: 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 从为 0 的格子走一步,必然走向为 1 的格子 .//只能走四 ...

  6. loj#2020. 「AHOI / HNOI2017」礼物

    题意:给定xy数组求 \(\sum_{i=0}^{n-1}(x_i+y_{(i+k)\modn}+c)^2\) 题解:先化简可得 \(n*c^2+2*\sum_{i=0}^{n-1}x_i-y_i+\ ...

  7. win php安装 oracle11 g

    1.下载plsql和oracle11g plsql安装比较简单,就是普通的安装.oracle11 g不用安装, 下面我讲解一下win 64位的系统配置oracle: (1).首先我使用的是warpse ...

  8. WDA基础十二:FREE PROGRAM SH (WDA TREE)

    一个需要用TREE展示搜索帮助的需求: 1.创建WDA程序:ZCATEGORY 2.Component Controller中添加节点: (说明,此节点仅在搜索帮助程序中使用,可以不用interfac ...

  9. PostgreSQL导出一张表到MySQL

    1. 查看PostgreSQL表结构,数据量,是否有特殊字段值 region_il=# select count(*) from result_basic; count --------- ( row ...

  10. 【转】SQLServer汉字转全拼音函数

    USE Test go IF OBJECT_ID('Fn_GetQuanPin','Fn') IS NOT NULL DROP FUNCTION fn_GetQuanPin go create fun ...