如今你們都追趕新的技術潮流,我來逆行一下。java
其實Spring Boot 隱藏了大量的細節,有大量的默認配置,其實經過xml配置的方式也能夠達到和Spring Boot同樣的效果。spring
在Spring Boot項目中咱們經過application.properties
中的設置來配置使用哪一個配置文件application-dev.properties
,application-prod.properties
等等apache
spring.profiles.active=dev
Spring 3.0之後就包含了Profile功能,在xml中能夠這麼寫,不過全部的bean須要顯式的配置。須要弄清楚本身項目的依賴關係,在Spring中第三方包如何初始化。app
<beans profile="dev,test"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application-dev.properties" /> </bean> </beans> <beans profile="prod"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application-prod.properties" /> </bean> </beans>
在Spring Boot中大量的Jave Bean都包含了默認初始化的功能,只須要配置預先設置好的屬性名稱,可是在xml中須要顯式的初始化Bean,而且能夠在初始化的時候用Placeholder
來配置。maven
在 Spring Boot 項目中application.properties
和application-xxx.properties
中的變量會自動放到 Environment中,而且能夠經過@Value
直接注入到變量中。ide
若是使用 ClassPathXmlApplicationContext 初始化項目,能夠看到源代碼裏 Environment 是一個 StandardEnvironment 實例,僅僅包含系統變量和環境變量,爲了把application-xxx.properties
放到 Environment 當中咱們須要擴展一下 ClassPathXmlApplicationContext,下面是CustomApplicationContext
和CustomEnvironment
spring-boot
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; public class CustomApplicationContext extends ClassPathXmlApplicationContext { public CustomApplicationContext(){ super(); } public CustomApplicationContext(String configLocation) { super(new String[]{configLocation}, true, null); } @Override public ConfigurableEnvironment createEnvironment() { return new CustomEnvironment(); } }
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePropertySource; import java.io.IOException; public class CustomEnvironment extends StandardEnvironment { private static final String APPCONFIG_PATH_PATTERN = "classpath:application-%s.properties"; @Override protected void customizePropertySources(MutablePropertySources propertySources) { super.customizePropertySources(propertySources); try { propertySources.addLast(initResourcePropertySourceLocator()); } catch (IOException e) { logger.warn("failed to initialize application config environment", e); } } private PropertySource<?> initResourcePropertySourceLocator() throws IOException { String profile = System.getProperty("spring.profiles.active", "dev"); String configPath = String.format(APPCONFIG_PATH_PATTERN, profile); System.out.println("Using application config: " + configPath); Resource resource = new DefaultResourceLoader(this.getClass().getClassLoader()). getResource(configPath); PropertySource resourcePropertySource = new ResourcePropertySource(resource); return resourcePropertySource; } }
Spring Boot 默認使用的是logback,在logback-spring.xml 的配置文件中可使用Spring Profile,並且還有一個默認的CONSOLE Appender測試
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <springProfile name="dev,test"> <logger name="org.springframework" level="DEBUG" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> </springProfile> <springProfile name="prod"> <logger name="org.springframework" level="INFO" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> </springProfile> </configuration>
在沒有使用Spring Boot的狀況下,不能在logback的config中使用Spring Profile,只能分拆成多個文件,而後根據環境變量讀取不一樣的配置文件,須要添加依賴org.logback-extensions
。ui
<dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.4</version> </dependency>
logback-dev.xmlthis
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%15.15t] %-40.40logger{39} : %m%n}"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> </appender> <logger name="org.springframework" level="DEBUG" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> </configuration>
logback-prod.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%15.15t] %-40.40logger{39} : %m%n}"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>utf8</charset> </encoder> </appender> <logger name="org.springframework" level="INFO" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> </configuration>
下面的代碼根據環境變量讀取不一樣的配置文件
private static final String LOGCONFIG_PATH_PATTERN = "classpath:logback-%s.xml"; public static void main(String[] args) throws FileNotFoundException, JoranException { String profile = System.getProperty("spring.profiles.active", "dev"); System.setProperty("file.encoding", "utf-8"); // logback config String logConfigPath = String.format(LOGCONFIG_PATH_PATTERN, profile); System.out.println("Using logback config: " + logConfigPath); LogbackConfigurer.initLogging(logConfigPath); SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); ConfigurableApplicationContext context = new CustomApplicationContext("classpath:applicationContext.xml"); }
有Spring Boot 的時候TestCase寫起來很方便,在類上添加兩行註解便可,在src\test\resources
下的application.properties
中設置spring.profiles.active=test
便可指定Profile爲test
@RunWith(SpringRunner.class) @SpringBootTest public class TestStockService { @Autowired StockService stockService; @Before public void setUp() { } @After public void tearDown() { } @Test public void testMissingBbTickerEN() { } }
不使用Spring Boot的狀況下,須要指定好幾個配置。
@RunWith(SpringRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") @ActiveProfiles(profiles = "test") @TestPropertySource("classpath:application-test.properties") public class TestStockService { @Autowired StockService stockService; @Before public void setUp() { } @After public void tearDown() { } @Test public void testMissingBbTickerEN() { } }
Spring Boot 會把項目和所依賴的 Jar 包打包成一個大 Jar 包,直接運行這個 Jar 包就能夠。這個功能是經過spring-boot-maven-plugin
實現的。
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
不使用Spring Boot 以後,咱們須要配置maven-jar-plugin
,可是依賴包沒法像Spring Boot同樣打包成一個大的 Jar 包,須要咱們指定classpath。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <mainClass>com.exmaple.demo.DemoApplication</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> </configuration> </plugin>
注意:
當用java -jar yourJarExe.jar來運行一個通過打包的應用程序的時候,你會發現如何設置-classpath參數應用程序都找不到相應的第三方類,報ClassNotFound錯誤。實際上這是因爲當使用-jar參數運行的時候,java VM會屏蔽全部的外部classpath,而只以自己yourJarExe.jar的內部class做爲類的尋找範圍。因此須要在jar包mainfest中添加classpath。
使用下面的maven配置幫你把全部的依賴包複製到targetlib目錄下,方便咱們部署或者是測試時複製依賴包。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>target/lib</outputDirectory> <overWriteIfNewer>true</overWriteIfNewer> <excludeGroupIds> junit,org.hamcrest,org.mockito,org.powermock,${project.groupId} </excludeGroupIds> </configuration> </execution> </executions> <configuration> <verbose>true</verbose> <detail>true</detail> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </plugin>
運行時經過指定命令行參數 -Dspring.profiles.active=prod 來切換profile
java -jar -Dspring.profiles.active=prod demo.jar
Spring Boot很大程度上方便了咱們的開發,可是隱藏了大量的細節,咱們使用xml配置spring能夠達到差很少一樣的效果,可是在結構和配置上更加清晰。