借鉴:https://blog.csdn.net/j903829182/article/details/74906948

一、Spring Boot 启动注解说明

@SpringBootApplication开启了Spring的组件扫描和Spring Boot的自动配置功能。实际上, @SpringBootApplication将三个有用的注解组合在了一起。

  • Spring的@Configuration:标明该类使用Spring基于Java的配置。虽然本书不会写太多配置,但我们会更倾向于使用基于Java而不是XML的配置。
  • Spring的@ComponentScan:启用组件扫描,这样你写的Web控制器类和其他组件才能被自动发现并注册为Spring应用程序上下文里的Bean。默认扫描@SpringBootApplication 所在类的同级目录以及它的子目录。本章稍后会写一个简单的Spring MVC控制器,使用@Controller进行注解,这样组件扫描才能找到它。
  • Spring Boot 的 @EnableAutoConfiguration: 这 个 不 起 眼 的 小 注 解 也 可 以 称 为@Abracadabra①,就是这一行配置开启了Spring Boot自动配置的魔力,让你不用再写成篇的配置了。

在Spring Boot的早期版本中,你需要在ReadingListApplication类上同时标上这三个注解,但从Spring Boot 1.2.0开始,有@SpringBootApplication就行了。

二、Bean的scope

scope描述了spring容器如何新建bena的实例,spring的scope有以下几种,通过@Scope注解来实现

  • Singleton:一个spring容器中只有一个bena的实例,此为spring的默认配置,全容器共享一个实例的bean。
  • Prototype:每次调用新建一个bean的实例。
  • Request:web项目中,给每一个http request新建一个Bean实例。
  • Session :web项目中,给每一个http session新建一个实例。
  • GlobalSession:这个只在portal应用中有用,给每一个global http session新建一个bean实例。

另外,在spring batch中还有一个Scope是使用@StepScope,用在批处理中。

实例:

定义一个Single的Bean

  1. /**
  2. * @Description: 自定义Single实例
  3. * @ClassName: CustomSingleService
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 上午10:34:36
  6. *
  7. */
  8. @Service
  9. //默认为Sinleton,相当于@Scope("singleton")
  10. @Scope(value="singleton")
  11. public class CustomSingleService {
  12.  
  13. }

定义一个Prototype的Bean

  1. /**
  2. * @Description: 自定义Prototype实例
  3. * @ClassName: CustomPrototypeService
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 上午10:34:36
  6. *
  7. */
  8. @Service
  9. @Scope(value="prototype")
  10. public class CustomPrototypeService {
  11.  
  12. }

Bean的Scope配置

  1. import org.springframework.context.annotation.ComponentScan;
  2. import org.springframework.context.annotation.Configuration;
  3.  
  4. /**
  5. * @Description: 自定义Bean的Scope配置类
  6. * @ClassName: CustomScopConfig
  7. * @author OnlyMate
  8. * @Date 2018年9月13日 上午10:59:54
  9. *
  10. */
  11. @Configuration
  12. @ComponentScan(value="com.only.mate.springboot.basic.scope")
  13. public class CustomScopConfig {
  14.  
  15. }

测试类

  1. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  2. import com.only.mate.springboot.configure.basic.CustomScopConfig;
  3.  
  4. public class CustomScopeMain {
  5. public static void main(String[] args) {
  6. // AnnotationConfigApplicationContext作为spring容器,接受一个配置类作为参数
  7. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomScopConfig.class);
  8. CustomSingleService cs1 = context.getBean(CustomSingleService.class);
  9. CustomPrototypeService cp1 = context.getBean(CustomPrototypeService.class);
  10.  
  11. CustomSingleService cs2 = context.getBean(CustomSingleService.class);
  12. CustomPrototypeService cp2 = context.getBean(CustomPrototypeService.class);
  13. System.out.println("cs1与cs2是否相等:" + cs1.equals(cs2));
  14. System.out.println("cp1与cp2是否相等:" + cp1.equals(cp2));
  15. context.close();
  16.  
  17. }
  18. }

结果:

三、Bean的初始化和销毁

  在我们实际开发的时候,经常会遇到在bean使用之前或者之后做一些必要的操作,spring 对bean的生命周期的操作提供了支持。在使用java配置和注解配置下提供如下两种方式:

  • java配置方式:使用@Bean的initMethod和destroyMethod(相当于xml配置的init-method和destory-method)
  • 注解方式:利用JSR-250的@PostConstruct和@PreDestroy

1、增加JSR250支持

  1. <!--增加JSR250支持-->
  2. <dependency>
  3. <groupId>javax.annotation</groupId>
  4. <artifactId>jsr250-api</artifactId>
  5. <version>1.0</version>
  6. </dependency>

2、使用@Bean形式的bean

  1. /**
  2. * @Description: 自定义@Bean方式的初始化和销毁方法
  3. * @ClassName: CustomBeanWay
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 上午11:15:41
  6. *
  7. */
  8. public class CustomBeanWay {
  9. public CustomBeanWay() {
  10. super();
  11. System.out.println("@Bean初始化构造方法 ==> CustomBeanWay method");
  12. }
  13.  
  14. public void init() {
  15. System.out.println("@Bean初始化方法 ==> init method");
  16. }
  17.  
  18. public void destroy() {
  19. System.out.println("@Bean销毁方法 ==> destroy method");
  20. }
  21. }

3、使用JSR250形式的bean

  1. /**
  2. * @Description: 自定义JSR250方式的初始化和销毁方法
  3. * @ClassName: CustomJSR250Way
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 上午11:15:41
  6. *
  7. */
  8. public class CustomJSR250Way {
  9. public CustomJSR250Way() {
  10. super();
  11. System.out.println("JSR250初始化构造方法 ==> CustomJSR250Way method");
  12. }
  13.  
  14. @PostConstruct
  15. public void init() {
  16. System.out.println("JSR250初始化方法 ==> init method");
  17. }
  18.  
  19. @PreDestroy
  20. public void destroy() {
  21. System.out.println("JSR250销毁方法 ==> destroy method");
  22. }
  23. }

4、配置

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4.  
  5. import com.only.mate.springboot.basic.lifecycle.CustomBeanWay;
  6. import com.only.mate.springboot.basic.lifecycle.CustomJSR250Way;
  7.  
  8. @Configuration
  9. @ComponentScan(value="com.only.mate.springboot.basic.lifecycle")
  10. public class CustomLifeCycleConfig {
  11.  
  12. @Bean(initMethod = "init",destroyMethod = "destroy")
  13. public CustomBeanWay customBeanWay(){
  14. return new CustomBeanWay();
  15. }
  16.  
  17. @Bean
  18. public CustomJSR250Way customJSR250Way(){
  19. return new CustomJSR250Way();
  20. }
  21.  
  22. }

5、启动

  1. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  2.  
  3. import com.only.mate.springboot.configure.lifecycle.CustomLifeCycleConfig;
  4.  
  5. @SuppressWarnings("unused")
  6. public class CustomLifeCycleMain {
  7.  
  8. public static void main(String[] args) {
  9. // AnnotationConfigApplicationContext作为spring容器,接受一个配置类作为参数
  10. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomLifeCycleConfig.class);
  11. CustomBeanWay customBeanWay = context.getBean(CustomBeanWay.class);
  12. CustomJSR250Way customJSR250Way = context.getBean(CustomJSR250Way.class);
  13.  
  14. context.close();
  15.  
  16. }
  17. }

6、效果图

可见init方法和destory方法在构造方法之后,bean销毁之前执行。

四、Spring EL和资源调用

  spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于jsp的EL表达式语言。
  spring开发中经常涉及调用各种资源的情况,包含普通文件,网址,配置文件,系统环境变量等,我们可以使用  spring表达式语言实现资源的注入。
  spring主要在注解@Vavle的参数中使用表达式。
下面演示一下几种情况:

  • 注入普通字符串
  • 注入操作系统属性
  • 注入表达式运算结果
  • 注入其他Bean的属性
  • 注入文件内容
  • 注入网址内容
  • 注入属性文件

1、准备,增加commons-io可简化文件相关的操作,本例使用commons-io将file转换成字符串。

  1. <!--增加commons-io可简化文件相关操作-->
  2. <dependency>
  3. <groupId>commons-io</groupId>
  4. <artifactId>commons-io</artifactId>
  5. <version>2.3</version>
  6. </dependency>

2、创建文件

在resources下简历files文件夹,并创建el.properties和test.txt文件

内容如下:

el.properties

  1. book.author=onlymate
  2. book.name=Java is s magic

test.txt

  1. 这是test.txt里面的内容,很高兴认识大家

3、需被注入的bean

  1. @Component
  2. public class CustomElBean {
  3. //注入普通字符串
  4. @Value("其他类属性")
  5. private String another;
  6.  
  7. public String getAnother() {
  8. return another;
  9. }
  10.  
  11. public void setAnother(String another) {
  12. this.another = another;
  13. }
  14. }

4、配置类

  1. import java.nio.charset.Charset;
  2.  
  3. import org.apache.commons.io.IOUtils;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.annotation.PropertySource;
  10. import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
  11. import org.springframework.core.env.Environment;
  12. import org.springframework.core.io.Resource;
  13.  
  14. /**
  15. * @Description: 自定义el配置类
  16. * @ClassName: CustomElConfig
  17. * @author OnlyMate
  18. * @Date 2018年9月13日 上午10:59:54
  19. *
  20. */
  21. @Configuration
  22. @ComponentScan(basePackages="com.only.mate.springboot.basic.el")
  23. //注入配置文件需要使用@PropertySource指定文件地址,若使用@Value注入,则要配置一个PropertySourcesPlaceholderConfigurer的bean
  24. //注意,@ @Value("${book.name}")使用的是$而不是#
  25. //注入Properties还可以从Environment中获得
  26. @PropertySource("classpath:files/el.properties")
  27. public class CustomElConfig {
  28. //注入普通字符串
  29. @Value("I Love YOU!")
  30. private String normal;
  31. //注入操作系统属性
  32. @Value("#{systemProperties['os.name']}")
  33. private String osName;
  34. //注入表达式结果
  35. @Value("#{T(java.lang.Math).random()*100.0}")
  36. private double randomNumber;
  37. //注入其他的bean属性
  38. @Value("#{customElBean.another}")
  39. private String fromAnother;
  40. //注入文件资源
  41. @Value("classpath:files/test.txt")
  42. private Resource testFile;
  43. //注入网址资源
  44. @Value("http://www.baidu.com")
  45. private Resource testUrl;
  46. //注入配置文件
  47. @Value("${book.name}")
  48. private String bookNmame;
  49.  
  50. //注入环境
  51. @Autowired
  52. private Environment environment;
  53. @Bean
  54. public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
  55. return new PropertySourcesPlaceholderConfigurer();
  56. }
  57.  
  58. public void outputResource(){
  59. try {
  60. System.out.println(normal);
  61. System.out.println(osName);
  62. System.out.println(randomNumber);
  63. System.out.println(fromAnother);
  64. System.out.println(IOUtils.toString(testFile.getInputStream(), Charset.defaultCharset()));
  65. System.out.println(IOUtils.toString(testUrl.getInputStream(), Charset.defaultCharset()));
  66. System.out.println(bookNmame);
  67. System.out.println(environment.getProperty("book.author"));
  68.  
  69. }catch (Exception e){
  70. e.printStackTrace();
  71. System.out.println(e);
  72. }
  73. }
  74.  
  75. }

5、启动运行

  1. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  2. import com.only.mate.springboot.configure.el.CustomElConfig;
  3.  
  4. public class CustomElMain {
  5.  
  6. public static void main(String [] args){
  7. //AnnotationConfigApplicationContext作为spring容器,接受一个配置类作为参数
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomElConfig.class);
  9. CustomElConfig elConfig = context.getBean(CustomElConfig.class);
  10. elConfig.outputResource();
  11. context.close();
  12. }
  13.  
  14. }

6、效果图

五、Profile

1、基础练习

Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置不同,比如数据库)

  • 通过设定Enviroment的ActiveProfiles来设定当前context需要使用的配置环境。在开发中使用@Profile注解类或者方法,达到在不同情况下选择实例化不同的Bean
  • 通过设定jvm的spring.profiles.active参数来设置配置环境
  • Web项目设置在Servlet的context parameter中

1、定义一个bean

  1. /**
  2. * @Description: 定义一个bean
  3. * @ClassName: CustomProfileBean
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 下午4:26:22
  6. *
  7. */
  8. public class CustomProfileBean {
  9. private String content;
  10.  
  11. public CustomProfileBean(String content) {
  12. super();
  13. this.content = content;
  14. }
  15.  
  16. public String getContent() {
  17. return content;
  18. }
  19.  
  20. public void setContent(String content) {
  21. this.content = content;
  22. }
  23. }

2、配置

  1. /**
  2. * @Description: 自定义Profile的配置类
  3. * @ClassName: CustomProfileConfig
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 下午4:27:17
  6. *
  7. */
  8. @Configuration
  9. public class CustomProfileConfig {
  10. @Bean
  11. @Profile("dev")//Profile为dev时实例化devCustomProfileBean
  12. public CustomProfileBean devCustomProfileBean(){
  13. return new CustomProfileBean("from development pfofile");
  14. }
  15.  
  16. @Bean
  17. @Profile("prod")//Profile为prod时实例化prodCustomProfileBean
  18. public CustomProfileBean prodCustomProfileBean(){
  19. return new CustomProfileBean("from production profile");
  20. }
  21.  
  22. }

3、启动运行

  1. /**
  2. * @Description:
  3. * @ClassName: CustomProfileMain
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 下午4:26:22
  6. *
  7. */
  8. public class CustomProfileMain {
  9.  
  10. public static void main(String [] args){
  11. //AnnotationConfigApplicationContext作为spring容器,接受一个配置类作为参数
  12. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  13. //先将活动的Profile设置为prod
  14. context.getEnvironment().setActiveProfiles("prod");
  15. //后置注册Bean配置类,不然会报bean未定义的错误
  16. context.register(CustomProfileConfig.class);
  17. //刷新容器
  18. context.refresh();
  19. CustomProfileBean demoBean = context.getBean(CustomProfileBean.class);
  20. System.out.println(demoBean.getContent());
  21. context.close();
  22. }
  23. }

4、效果图

2、日志信息的配置

logback-spring.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration debug="true"><!-- debug="true"设置调试模式 -->
  3. <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径,文件要以logback-spring.xml命名-->
  4. <springProfile name="test">
  5. <property name="catalina.base" value="/home/webapp/logs/spring-boot" />
  6. </springProfile>
  7. <springProfile name="prod">
  8. <property name="catalina.base" value="/app/webapp/logs/spring-boot" />
  9. </springProfile>
  10. <springProfile name="dev">
  11. <property name="catalina.base" value="H:/logs/spring-boot" />
  12. </springProfile>
  13.  
  14. <!--<springProperty scope="context" name="catalina.base" source="catalina.base"/>-->
  15.  
  16. <!-- 日志地址 -->
  17. <!--<property name="catalina.base" value="H:/logs"></property>-->
  18.  
  19. <!-- 控制台输出 -->
  20. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  21. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  22. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} 耗时:%r 日志来自:%logger{50} 日志类型: %-5p 日志内容:%m%n</pattern>
  23. </encoder>
  24. </appender>
  25. <!-- 按照每天生成日志文件 -->
  26. <appender name="DEFAULT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
  27. <File>${catalina.base}/logs/common-default.log</File>
  28. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  29. <!--日志文件输出的文件名 -->
  30. <FileNamePattern>${catalina.base}/logs/common-default-%d{yyyy-MM-dd}.log</FileNamePattern>
  31. <!--日志文件保留天数 -->
  32. <MaxHistory>30</MaxHistory>
  33. </rollingPolicy>
  34. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  35. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  36. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
  37. </encoder>
  38. <!--日志文件最大的大小 -->
  39. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  40. <MaxFileSize>10MB</MaxFileSize>
  41. </triggeringPolicy>
  42. </appender>
  43. <!-- 按照每天生成日志文件 -->
  44. <appender name="INFO-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
  45. <File>${catalina.base}/logs/info-log.log</File>
  46. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  47. <!--日志文件输出的文件名 -->
  48. <FileNamePattern>${catalina.base}/logs/info-log-%d{yyyy-MM-dd}.log</FileNamePattern>
  49. <!--日志文件保留天数 -->
  50. <MaxHistory>30</MaxHistory>
  51. </rollingPolicy>
  52. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  53. <!-- 格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
  54. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
  55. </encoder>
  56. <!--日志文件最大的大小 -->
  57. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  58. <MaxFileSize>10MB</MaxFileSize>
  59. </triggeringPolicy>
  60. </appender>
  61.  
  62. <logger name="com.google.code.yanf4j" level="ERROR" />
  63.  
  64. <!-- show parameters for hibernate sql 专为 Hibernate 定制 -->
  65. <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" />
  66. <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" />
  67. <logger name="org.hibernate.SQL" level="DEBUG" />
  68. <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" />
  69. <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" />
  70.  
  71. <!--myibatis log configure-->
  72. <logger name="org.apache.ibatis" level="DEBUG"/>
  73. <logger name="java.sql.Connection" level="DEBUG"/>
  74. <logger name="java.sql.Statement" level="DEBUG"/>
  75. <logger name="java.sql.PreparedStatement" level="DEBUG"/>
  76.  
  77. <logger name="net.rubyeye.xmemcached" level="INFO"/>
  78. <logger name="org.springframework" level="INFO"/>
  79. <logger name="net.sf.ehcache" level="INFO"/>
  80.  
  81. <logger name="org.apache.zookeeper" level="INFO" />
  82.  
  83. <!-- 指定某一个包或者某一个类的打印级别以及是否传入root进行打印 -->
  84. <!-- addtivity:是否向上级loger传递打印信息。默认是true。-->
  85. <!-- <loger>可以包含零个或多个<appender-ref>元素,标识这个appender将会添加到这个loger。-->
  86. <!-- name:用来指定受此loger约束的某一个包或者具体的某一个类。-->
  87. <!-- level:
  88. 用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,还有一个特俗值INHERITED或者同义词NULL,代表强制执行上级的级别。
  89. 如果未设置此属性,那么当前loger将会继承上级的级别。-->
  90. <!-- 为所有开头为dao的类打印sql语句 -->
  91. <!-- <logger name="dao" level="DEBUG">
  92. <appender-ref ref="INFO-APPENDER" />
  93. </logger> -->
  94. <logger name="com.only.mate" level="DEBUG" additivity="true">
  95. <appender-ref ref="INFO-APPENDER" />
  96. </logger>
  97. <!-- 也是<loger>元素,但是它是根loger。只有一个level属性,应为已经被命名为"root". -->
  98. <root level="DEBUG">
  99. <appender-ref ref="STDOUT"/>
  100. <appender-ref ref="DEFAULT-APPENDER"/>
  101. </root>
  102.  
  103. </configuration>

这里有兴趣的自己自己尝试。

3、Java代码中根据系统环境处理逻辑

创建一个服务,实现ApplicationContextAware接口

  1. import org.springframework.beans.BeansException;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;
  4. import org.springframework.stereotype.Service;
  5.  
  6. @Service
  7. public class CustomProfileService implements ApplicationContextAware{
  8. private ApplicationContext applicationContext = null;
  9.  
  10. @Override
  11. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  12. this.applicationContext = applicationContext;
  13. }
  14.  
  15. public void doSomething() {
  16. //获取当前系统环境
  17. String[] springActives = applicationContext.getEnvironment().getActiveProfiles();
  18. String springActive = "";
  19. if(springActives.length > 0) {
  20. springActive = springActives[0];
  21. }else {
  22. springActive = applicationContext.getEnvironment().getDefaultProfiles()[0];
  23. }
  24. System.out.println("当前的开发环境:"+ springActive);
  25. }
  26. }

配置类

  1. /**
  2. * @Description: 自定义Profile的配置类
  3. * @ClassName: CustomProfileConfig
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 下午4:27:17
  6. *
  7. */
  8. @Configuration
  9. @ComponentScan(basePackages="com.only.mate.springboot.basic.profile")
  10. public class CustomProfileConfig {
  11. @Bean
  12. @Profile("dev")//Profile为dev时实例化devCustomProfileBean
  13. public CustomProfileBean devCustomProfileBean(){
  14. return new CustomProfileBean("from development pfofile");
  15. }
  16.  
  17. @Bean
  18. @Profile("prod")//Profile为prod时实例化prodCustomProfileBean
  19. public CustomProfileBean prodCustomProfileBean(){
  20. return new CustomProfileBean("from production profile");
  21. }
  22.  
  23. }

启动类

  1. /**
  2. * @Description:
  3. * @ClassName: CustomProfileMain
  4. * @author OnlyMate
  5. * @Date 2018年9月13日 下午4:26:22
  6. *
  7. */
  8. public class CustomProfileMain {
  9.  
  10. public static void main(String [] args){
  11. //AnnotationConfigApplicationContext作为spring容器,接受一个配置类作为参数
  12. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  13. //先将活动的Profile设置为prod
  14. context.getEnvironment().setActiveProfiles("prod");
  15. //后置注册Bean配置类,不然会报bean未定义的错误
  16. context.register(CustomProfileConfig.class);
  17. //刷新容器
  18. context.refresh();
  19. CustomProfileBean customProfileBean = context.getBean(CustomProfileBean.class);
  20. System.out.println(customProfileBean.getContent());
  21.  
  22. CustomProfileService customProfileService = context.getBean(CustomProfileService.class);
  23. customProfileService.doSomething();
  24.  
  25. context.close();
  26. }
  27. }

效果图

Spring Boot实践——基础和常用配置的更多相关文章

  1. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  2. Spring Boot实践——Spring Boot 2.0 新特性和发展方向

    出自:https://mp.weixin.qq.com/s/EWmuzsgHueHcSB0WH-3AQw 以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Jav ...

  3. Spring Boot实践——Spring AOP实现之动态代理

    Spring AOP 介绍 AOP的介绍可以查看 Spring Boot实践——AOP实现 与AspectJ的静态代理不同,Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改 ...

  4. Spring Boot实践——AOP实现

    借鉴:http://www.cnblogs.com/xrq730/p/4919025.html     https://blog.csdn.net/zhaokejin521/article/detai ...

  5. 自定义的Spring Boot starter如何设置自动配置注解

    本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...

  6. Spring Boot应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

  7. Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

    配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...

  8. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  9. JAR(Spring Boot)应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

随机推荐

  1. 51nod-1455-dp/缩小范围

    1455 宝石猎人  题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 苏塞克岛是一个有着30001个小岛的群岛,这 ...

  2. linux-android-adt

    本打算带着唐帅弟弟研究几天手机开发,没想到这老弟天天遇到问题,原来认为他除了错误,没想到啊,我一上手也到处 是错误,折磨我们哥俩了,今天我俩干 了几把扑克,PK了一次羽毛球,回来研究了一会,阿门问题解 ...

  3. Xcode6中手动添加Precompile Prefix Header

    Xcode5中创建一个工程的时候,系统会自动创建一个以以工程名为名字的pch(Precompile Prefix Header)文件,开发的过程中可以将广泛使用的头文件以及宏包含在该文件下,编译器就会 ...

  4. avast! 2014正式版下载

    avast!官方简体中文网站: http://www.avast.com/zh-cn/index avast!官方英文网站: http://www.avast.com/index avast!免费版官 ...

  5. DIY远程移动图像监测(tiny6410+USB摄像头+motion+yeelink+curl)

    看到有博客上采用motion搭建移动图像监测系统,感觉很强大,但大多缺少远程监测能力,大多局限于局域网.OK,笔者手头刚好有一个30W像素的USB摄像头,那么借用yeelink服务,也来DIY一把,哈 ...

  6. eclipse加入c标签

    在MyEclipse中使用jstl标签只需导读jstl.jar就能使用,但是在Eclipse中还需要一点小套路 步骤: 一.导入jstl.jar 二.导入导入standard.jar 三.在WEB-I ...

  7. Word操作(基于word2013)【非编程类】

    一.生成目录 1.word支持自动生成目录功能,生成地点为操作时光标的落点 2.生成方式:打开首选项,进入引用标签,如下图所示最左边即为目录选项. 一般有3个内置目录类型 a:手动目录,生成目录后只有 ...

  8. 20155230 2016-2017-2 《Java程序设计》第八周学习总结

    20155230 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 java.util.logging包提供了日志功能相关类与接口,使用日志的起点是logger ...

  9. POI2011题解

    POI2011题解 2214先咕一会... [BZOJ2212][POI2011]Tree Rotations 线段树合并模板题. #include<cstdio> #include< ...

  10. cocos2dx切换场景

    第一屏必须: auto scene = GameMenu::createScene(); director->runWithScene(scene); 然后是主场景: auto scene = ...