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
你好
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(); } }