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. zookeeper在搭建的时候,解决后台启动为standalone模式问题

    今天在搭建zookeeper,搭建完成之后,启动一直报错: 上网查了好多资料:有几种解决方案: 1.在配置文件conf目录下,将zoo_sample.cfg删除,只留zoo.cfg(然而就我的情况而言 ...

  2. openstack中数据库连接数太多--pymysql.err.OperationalError,1040, u'Too many connections'

    1.出现问题: openstack运行过程中出现如下问题: OperationalError: (pymysql.err.OperationalError) (1040, u'Too many con ...

  3. java控制流

    目录 1.引用数据类型 2.流程控制语句 2.1 条件控制语句if 2.2 if语句与三元运算符的互换 2.3 循环语句 2.4 循环嵌套 2.5 跳转语句 2.6 选择结构switch 3.猜数字案 ...

  4. 暂时关闭 windows 病毒防护

  5. 【IDEA】【6】Maven打包

    1,打包成jar包 右侧工具栏Maven Projects->项目名称->Lifecycle->package 2,打包时去掉test 右侧工具栏Maven Projects,打开后 ...

  6. python--model进阶

    一.QuerySet 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句.  >>> Entry.objects.a ...

  7. poj1002 大数的 n的m次

    import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(S ...

  8. iOS 面试总结

    APP崩溃 启动秒退 在新 iOS 上正常的应用,到了老版本 iOS 上秒退最常见原因是系统动态链接库或Framework无法找到.这种情况通常是由于 App 引用了一个新版操作系统里的动态库(或者某 ...

  9. Qt Widgets——子区域和子窗口

    QMdiArea 一般使用于主窗口QMainWindow,用于容纳多个子窗口QMdiSubWindow qt creator 3.0的设计师有MdiArea可直接拖入使用. 界面如下,图中灰色框即是个 ...

  10. SpringBoot主程序类,主入口类

    主程序类,主入口类 /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootApplication publi ...