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. Alibaba Java诊断工具Arthas之快速安装和简单使用

    Alibaba Java诊断工具Arthas简单介绍 : 当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决: 1.这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception ...

  2. p1211 Prime Cryptarithm

    直接深搜+检验. #include <iostream> #include <cstdio> #include <cmath> #include <algor ...

  3. CF-831D Office Keys 思维题

    http://codeforces.com/contest/831/problem/D 题目大意是在一条坐标轴上,给出n个人,k把钥匙(k>=n)以及终点的坐标,所有人都可以同时运动,但不可以公 ...

  4. IntelliJ IDEA的调试方法

    快捷键F9          resume programe       恢复程序 Alt+F10       show execution point    显示执行断点 F8          S ...

  5. apache2反向代理

    1.安装 Apache2 sudo apt-get install apache2 2.重启服务器 sudo /etc/init.d/apache2 restart 3.虚拟主机配置 启用这几个模块 ...

  6. 浅谈线程runnable和callable的使用及区别

    线程使用比较广泛,但实际上一般项目很少用上线程,线程常用于优化复杂的程序执行流程,把一些与业务关系关系不大但是必须要执行的流程使用线程的方式让子线程去执行,主流程只返回跟业务有关的信息 runnabl ...

  7. ASP.NET Core WebAPI 开发-新建WebAPI项目 转

    转 http://www.cnblogs.com/linezero/p/5497472.html ASP.NET Core WebAPI 开发-新建WebAPI项目   ASP.NET Core We ...

  8. 函数使用五:MIR7 发票预制 BAPI_INCOMINGINVOICE_PARK

    引自:http://blog.csdn.net/champaignwolf/article/details/51422329 FUNCTION zincominginvoice_park. *&quo ...

  9. 月日加四位尾数编号生成 VB方式

    <%Private Sub Form_Click()MsgBox "生成两位后缀"ClsFor i = 1 To 99barcod= Format(Right(Year(Da ...

  10. 利用JsonSchema校验json数据内容的合规性(转)

    原文地址:Json schema 背景: 复杂的AJAX应用程序可以与数百个不同的JSON服务进行交互,因此,引入对客户端验证的需求. 在处理校验问题方面有着很多的工具,但是通常可以将它们归为以下几类 ...