spring boot 基礎 2018年5月3日
阿新 • • 發佈:2018-05-04
plugins pri info out configure www read ping mapping 主包下運行類@SpringBootApplication 此註解是核心註解,源碼如下
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} )} ) public @interface SpringBootApplication{ }
2:修改banner:http://www.cnblogs.com/whthomas/p/5268743.html
3:spring Boot的配置文件, 全局的配置文件application.properties或者application.yml
修改訪問路徑:
默認端口是8080,我將之改為8081,默認訪問路徑是http://localhost:8080,將之改為http://localhost:8081/helloboot
server.context-path=/helloboot
server.port=8081
設置中文編碼,直接在配置文件添加
專門設置中文,application.properties中添加如下代碼:
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
然後再修改controller中的返回值,就能得到配置的屬性值
簡便方法為: 將properties屬性和一個Bean關聯在一起
- 創建屬性的Bean.properties,然後直接輸入 名字.屬性
- 創建Book Bean,並註入properties文件中的值
- 在Controller中添加如下代碼註入Bean:
- @Autowired private BookBean bookBean;
- 添加路徑映射:
- @RequestMapping("/book") public String book() { return "Hello Spring Boot! The BookName is "+bookBean.getName()+";and Book Author is "+bookBean.getAuthor()+";and Book price is "+bookBean.getPrice(); }
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId></dependency> 自定設置文件的的名稱,直接值properties裏面設置logging.config 選項即可: 日誌的配置 在application.properties中添加如下代碼 # 配置日誌輸出位置 logging.file=/home/sang/workspace/log.log #配置日誌級別。 logging.level.org.springframework.web=debug 啟動熱部署 在 pom.xml 文件中添加如下配置: <!-- 熱部署 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope></dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 沒有該配置,devtools 不生效 --> <fork>true</fork> </configuration> </plugin> </plugins></build> 多環境配置profile。 在 src/main/resources 目錄下創建三個配置文件,並在三個文件中配置不同的信息: application-dev.properties:用於開發環境 application-test.properties:用於測試環境 application-prod.properties:用於生產環境 配置完不同的信息後,直接在 application.properties 中配置: spring.profiles.active=dev #dev為開發環境,同時可以將dev修改為其它的環境 其中,springProfile 標簽的 name 屬性對應 application.properties 中的 spring.profiles.active 的配置。 即 spring.profiles.active 的值可以看作是日誌配置文件中對應的 springProfile 是否生效的開關。在 src/main/resources 下創建 logback-spring.xml 文件,內容如下 <?xml version="1.0" encoding="UTF-8"?><configuration> <!-- 文件輸出格式 --> <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" /> <!-- test文件路徑 --> <property name="TEST_FILE_PATH" value="d:/test.log" /> <!-- pro文件路徑 --> <property name="PRO_FILE_PATH" value="/opt/test/log" /> <!-- 開發環境 --> <springProfile name="dev"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${PATTERN}</pattern> </encoder> </appender> <logger name="com.light.springboot" level="debug" /> <root level="info"> <appender-ref ref="CONSOLE" /> </root> </springProfile> <!-- 測試環境 --> <springProfile name="test"> <!-- 每天產生一個文件 --> <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件路徑 --> <file>${TEST_FILE_PATH}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 文件名稱 --> <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 文件最大保存歷史數量 --> <MaxHistory>100</MaxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${PATTERN}</pattern> </layout> </appender> <root level="info"> <appender-ref ref="TEST-FILE" /> </root> </springProfile> <!-- 生產環境 --> <springProfile name="prod"> <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${PRO_FILE_PATH}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern> <MaxHistory>100</MaxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>${PATTERN}</pattern> </layout> </appender> <root level="warn"> <appender-ref ref="PROD_FILE" /> </root> </springProfile></configuration>日誌輸出有兩種方式 (log4j以及logback,logback是官方推薦使用的)
- 配置logback
-
配置 log4j2
- 配置 log4j2
- 生成jar包
- 打包成war文件
spring boot 基礎 2018年5月3日