借鑒: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
/** * @Description: 自定義Single實例 * @ClassName: CustomSingleService * @author OnlyMate * @Date 2018年9月13日 上午10:34:36 * */ @Service //默認為Sinleton,相當於@Scope("singleton") @Scope(value="singleton") public class CustomSingleService { }
定義一個Prototype的Bean
/** * @Description: 自定義Prototype實例 * @ClassName: CustomPrototypeService * @author OnlyMate * @Date 2018年9月13日 上午10:34:36 * */ @Service @Scope(value="prototype") public class CustomPrototypeService { }
Bean的Scope配置
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * @Description: 自定義Bean的Scope配置類 * @ClassName: CustomScopConfig * @author OnlyMate * @Date 2018年9月13日 上午10:59:54 * */ @Configuration @ComponentScan(value="com.only.mate.springboot.basic.scope") public class CustomScopConfig { }
測試類
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.only.mate.springboot.configure.basic.CustomScopConfig; public class CustomScopeMain { public static void main(String[] args) { // AnnotationConfigApplicationContext作為spring容器,接受一個配置類作為參數 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomScopConfig.class); CustomSingleService cs1 = context.getBean(CustomSingleService.class); CustomPrototypeService cp1 = context.getBean(CustomPrototypeService.class); CustomSingleService cs2 = context.getBean(CustomSingleService.class); CustomPrototypeService cp2 = context.getBean(CustomPrototypeService.class); System.out.println("cs1與cs2是否相等:" + cs1.equals(cs2)); System.out.println("cp1與cp2是否相等:" + cp1.equals(cp2)); context.close(); } }
結果:
三、Bean的初始化和銷毀
在我們實際開發的時候,經常會遇到在bean使用之前或者之后做一些必要的操作,spring 對bean的生命周期的操作提供了支持。在使用java配置和注解配置下提供如下兩種方式:
- java配置方式:使用@Bean的initMethod和destroyMethod(相當於xml配置的init-method和destory-method)
- 注解方式:利用JSR-250的@PostConstruct和@PreDestroy
1、增加JSR250支持
<!--增加JSR250支持--> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>
2、使用@Bean形式的bean
/** * @Description: 自定義@Bean方式的初始化和銷毀方法 * @ClassName: CustomBeanWay * @author OnlyMate * @Date 2018年9月13日 上午11:15:41 * */ public class CustomBeanWay { public CustomBeanWay() { super(); System.out.println("@Bean初始化構造方法 ==> CustomBeanWay method"); } public void init() { System.out.println("@Bean初始化方法 ==> init method"); } public void destroy() { System.out.println("@Bean銷毀方法 ==> destroy method"); } }
3、使用JSR250形式的bean
/** * @Description: 自定義JSR250方式的初始化和銷毀方法 * @ClassName: CustomJSR250Way * @author OnlyMate * @Date 2018年9月13日 上午11:15:41 * */ public class CustomJSR250Way { public CustomJSR250Way() { super(); System.out.println("JSR250初始化構造方法 ==> CustomJSR250Way method"); } @PostConstruct public void init() { System.out.println("JSR250初始化方法 ==> init method"); } @PreDestroy public void destroy() { System.out.println("JSR250銷毀方法 ==> destroy method"); } }
4、配置
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import com.only.mate.springboot.basic.lifecycle.CustomBeanWay; import com.only.mate.springboot.basic.lifecycle.CustomJSR250Way; @Configuration @ComponentScan(value="com.only.mate.springboot.basic.lifecycle") public class CustomLifeCycleConfig { @Bean(initMethod = "init",destroyMethod = "destroy") public CustomBeanWay customBeanWay(){ return new CustomBeanWay(); } @Bean public CustomJSR250Way customJSR250Way(){ return new CustomJSR250Way(); } }
5、啟動
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.only.mate.springboot.configure.lifecycle.CustomLifeCycleConfig; @SuppressWarnings("unused") public class CustomLifeCycleMain { public static void main(String[] args) { // AnnotationConfigApplicationContext作為spring容器,接受一個配置類作為參數 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomLifeCycleConfig.class); CustomBeanWay customBeanWay = context.getBean(CustomBeanWay.class); CustomJSR250Way customJSR250Way = context.getBean(CustomJSR250Way.class); context.close(); } }
6、效果圖
可見init方法和destory方法在構造方法之后,bean銷毀之前執行。
四、Spring EL和資源調用
spring EL-Spring表達式語言,支持在xml和注解中使用表達式,類似於jsp的EL表達式語言。
spring開發中經常涉及調用各種資源的情況,包含普通文件,網址,配置文件,系統環境變量等,我們可以使用 spring表達式語言實現資源的注入。
spring主要在注解@Vavle的參數中使用表達式。
下面演示一下幾種情況:
- 注入普通字符串
- 注入操作系統屬性
- 注入表達式運算結果
- 注入其他Bean的屬性
- 注入文件內容
- 注入網址內容
- 注入屬性文件
1、准備,增加commons-io可簡化文件相關的操作,本例使用commons-io將file轉換成字符串。
<!--增加commons-io可簡化文件相關操作--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency>
2、創建文件
在resources下簡歷files文件夾,並創建el.properties和test.txt文件
內容如下:
el.properties
book.author=onlymate book.name=Java is s magic
test.txt
這是test.txt里面的內容,很高興認識大家
3、需被注入的bean
@Component public class CustomElBean { //注入普通字符串 @Value("其他類屬性") private String another; public String getAnother() { return another; } public void setAnother(String another) { this.another = another; } }
4、配置類
import java.nio.charset.Charset; 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; /** * @Description: 自定義el配置類 * @ClassName: CustomElConfig * @author OnlyMate * @Date 2018年9月13日 上午10:59:54 * */ @Configuration @ComponentScan(basePackages="com.only.mate.springboot.basic.el") //注入配置文件需要使用@PropertySource指定文件地址,若使用@Value注入,則要配置一個PropertySourcesPlaceholderConfigurer的bean //注意,@ @Value("${book.name}")使用的是$而不是# //注入Properties還可以從Environment中獲得 @PropertySource("classpath:files/el.properties") public class CustomElConfig { //注入普通字符串 @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; //注入其他的bean屬性 @Value("#{customElBean.another}") private String fromAnother; //注入文件資源 @Value("classpath:files/test.txt") private Resource testFile; //注入網址資源 @Value("http://www.baidu.com") private Resource testUrl; //注入配置文件 @Value("${book.name}") private String bookNmame; //注入環境 @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(), Charset.defaultCharset())); System.out.println(IOUtils.toString(testUrl.getInputStream(), Charset.defaultCharset())); System.out.println(bookNmame); System.out.println(environment.getProperty("book.author")); }catch (Exception e){ e.printStackTrace(); System.out.println(e); } } }
5、啟動運行
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.only.mate.springboot.configure.el.CustomElConfig; public class CustomElMain { public static void main(String [] args){ //AnnotationConfigApplicationContext作為spring容器,接受一個配置類作為參數 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomElConfig.class); CustomElConfig elConfig = context.getBean(CustomElConfig.class); elConfig.outputResource(); context.close(); } }
6、效果圖
五、Profile
1、基礎練習
Profile為在不同環境下使用不同的配置提供了支持(開發環境下的配置和生產環境下的配置不同,比如數據庫)
- 通過設定Enviroment的ActiveProfiles來設定當前context需要使用的配置環境。在開發中使用@Profile注解類或者方法,達到在不同情況下選擇實例化不同的Bean
- 通過設定jvm的spring.profiles.active參數來設置配置環境
- Web項目設置在Servlet的context parameter中
1、定義一個bean
/** * @Description: 定義一個bean * @ClassName: CustomProfileBean * @author OnlyMate * @Date 2018年9月13日 下午4:26:22 * */ public class CustomProfileBean { private String content; public CustomProfileBean(String content) { super(); this.content = content; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
2、配置
/** * @Description: 自定義Profile的配置類 * @ClassName: CustomProfileConfig * @author OnlyMate * @Date 2018年9月13日 下午4:27:17 * */ @Configuration public class CustomProfileConfig { @Bean @Profile("dev")//Profile為dev時實例化devCustomProfileBean public CustomProfileBean devCustomProfileBean(){ return new CustomProfileBean("from development pfofile"); } @Bean @Profile("prod")//Profile為prod時實例化prodCustomProfileBean public CustomProfileBean prodCustomProfileBean(){ return new CustomProfileBean("from production profile"); } }
3、啟動運行
/** * @Description: * @ClassName: CustomProfileMain * @author OnlyMate * @Date 2018年9月13日 下午4:26:22 * */ public class CustomProfileMain { public static void main(String [] args){ //AnnotationConfigApplicationContext作為spring容器,接受一個配置類作為參數 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //先將活動的Profile設置為prod context.getEnvironment().setActiveProfiles("prod"); //后置注冊Bean配置類,不然會報bean未定義的錯誤 context.register(CustomProfileConfig.class); //刷新容器 context.refresh(); CustomProfileBean demoBean = context.getBean(CustomProfileBean.class); System.out.println(demoBean.getContent()); context.close(); } }
4、效果圖
2、日志信息的配置
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="true"><!-- debug="true"設置調試模式 --> <!--定義日志文件的存儲地址 勿在 LogBack 的配置中使用相對路徑,文件要以logback-spring.xml命名--> <springProfile name="test"> <property name="catalina.base" value="/home/webapp/logs/spring-boot" /> </springProfile> <springProfile name="prod"> <property name="catalina.base" value="/app/webapp/logs/spring-boot" /> </springProfile> <springProfile name="dev"> <property name="catalina.base" value="H:/logs/spring-boot" /> </springProfile> <!--<springProperty scope="context" name="catalina.base" source="catalina.base"/>--> <!-- 日志地址 --> <!--<property name="catalina.base" value="H:/logs"></property>--> <!-- 控制台輸出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} 耗時:%r 日志來自:%logger{50} 日志類型: %-5p 日志內容:%m%n</pattern> </encoder> </appender> <!-- 按照每天生成日志文件 --> <appender name="DEFAULT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${catalina.base}/logs/common-default.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件輸出的文件名 --> <FileNamePattern>${catalina.base}/logs/common-default-%d{yyyy-MM-dd}.log</FileNamePattern> <!--日志文件保留天數 --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> <!--日志文件最大的大小 --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!-- 按照每天生成日志文件 --> <appender name="INFO-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>${catalina.base}/logs/info-log.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件輸出的文件名 --> <FileNamePattern>${catalina.base}/logs/info-log-%d{yyyy-MM-dd}.log</FileNamePattern> <!--日志文件保留天數 --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!-- 格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> <!--日志文件最大的大小 --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <logger name="com.google.code.yanf4j" level="ERROR" /> <!-- show parameters for hibernate sql 專為 Hibernate 定制 --> <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" /> <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" /> <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" /> <!--myibatis log configure--> <logger name="org.apache.ibatis" level="DEBUG"/> <logger name="java.sql.Connection" level="DEBUG"/> <logger name="java.sql.Statement" level="DEBUG"/> <logger name="java.sql.PreparedStatement" level="DEBUG"/> <logger name="net.rubyeye.xmemcached" level="INFO"/> <logger name="org.springframework" level="INFO"/> <logger name="net.sf.ehcache" level="INFO"/> <logger name="org.apache.zookeeper" level="INFO" /> <!-- 指定某一個包或者某一個類的打印級別以及是否傳入root進行打印 --> <!-- addtivity:是否向上級loger傳遞打印信息。默認是true。--> <!-- <loger>可以包含零個或多個<appender-ref>元素,標識這個appender將會添加到這個loger。--> <!-- name:用來指定受此loger約束的某一個包或者具體的某一個類。--> <!-- level: 用來設置打印級別,大小寫無關:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,還有一個特俗值INHERITED或者同義詞NULL,代表強制執行上級的級別。 如果未設置此屬性,那么當前loger將會繼承上級的級別。--> <!-- 為所有開頭為dao的類打印sql語句 --> <!-- <logger name="dao" level="DEBUG"> <appender-ref ref="INFO-APPENDER" /> </logger> --> <logger name="com.only.mate" level="DEBUG" additivity="true"> <appender-ref ref="INFO-APPENDER" /> </logger> <!-- 也是<loger>元素,但是它是根loger。只有一個level屬性,應為已經被命名為"root". --> <root level="DEBUG"> <appender-ref ref="STDOUT"/> <appender-ref ref="DEFAULT-APPENDER"/> </root> </configuration>
這里有興趣的自己自己嘗試。
3、Java代碼中根據系統環境處理邏輯
創建一個服務,實現ApplicationContextAware接口
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Service; @Service public class CustomProfileService implements ApplicationContextAware{ private ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public void doSomething() { //獲取當前系統環境 String[] springActives = applicationContext.getEnvironment().getActiveProfiles(); String springActive = ""; if(springActives.length > 0) { springActive = springActives[0]; }else { springActive = applicationContext.getEnvironment().getDefaultProfiles()[0]; } System.out.println("當前的開發環境:"+ springActive); } }
配置類
/** * @Description: 自定義Profile的配置類 * @ClassName: CustomProfileConfig * @author OnlyMate * @Date 2018年9月13日 下午4:27:17 * */ @Configuration @ComponentScan(basePackages="com.only.mate.springboot.basic.profile") public class CustomProfileConfig { @Bean @Profile("dev")//Profile為dev時實例化devCustomProfileBean public CustomProfileBean devCustomProfileBean(){ return new CustomProfileBean("from development pfofile"); } @Bean @Profile("prod")//Profile為prod時實例化prodCustomProfileBean public CustomProfileBean prodCustomProfileBean(){ return new CustomProfileBean("from production profile"); } }
啟動類
/** * @Description: * @ClassName: CustomProfileMain * @author OnlyMate * @Date 2018年9月13日 下午4:26:22 * */ public class CustomProfileMain { public static void main(String [] args){ //AnnotationConfigApplicationContext作為spring容器,接受一個配置類作為參數 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //先將活動的Profile設置為prod context.getEnvironment().setActiveProfiles("prod"); //后置注冊Bean配置類,不然會報bean未定義的錯誤 context.register(CustomProfileConfig.class); //刷新容器 context.refresh(); CustomProfileBean customProfileBean = context.getBean(CustomProfileBean.class); System.out.println(customProfileBean.getContent()); CustomProfileService customProfileService = context.getBean(CustomProfileService.class); customProfileService.doSomething(); context.close(); } }
效果圖