1. 程式人生 > >Springboot 學習筆記 之 Day 4 筆記部分

Springboot 學習筆記 之 Day 4 筆記部分

spring-boot-starter 核心Spring Boot starter,包括自動配置支援,日誌和YAML
spring-boot-starter-actuator 生產準備的特性,用於幫你監控和 管理應用
spring-boot-starter-web 對全棧web開發的支援,包括Tomcat和 spring-webmvc
spring-boot-starter-aop 對面向切面程式設計的支援,包括 spring-aop 和AspectJ
spring-boot-starter-jdbc 對JDBC資料庫的支援
spring-boot-starter-security 對 spring-security 的支援

 

Spring Boot預設將應用打包成一個可執行的jar包檔案,構建成功後使用java -jar命令即可執行應用。
或者在應用專案的主程式中執行main函式即可,不需要依賴tomcat、jetty等外部的應用伺服器。
其中內建的servlet Container:

此外,你仍然可以部署Spring Boot專案到任何相容Servlet3.0+的容器。

 

開啟devtools特性
devtools的熱部署和自動重啟
要想在Eclipse中使用Devtools的重啟功能,需要將自動編譯功能開啟。
每次儲存檔案並自動編譯後,devtools會檢測到classpath內容的修改,


並觸發應用重啟。重啟時實際只重新載入了一部分類,因此速度會非常快。詳細原理會在後面教程裡介紹。
devtools的livereload
開啟devtools特性的應用在啟動時會啟動一個livereload的server,
瀏覽器(如chrome,Firefox)安裝livereload外掛後,該外掛會監測到livereload server的更新,並自動重新整理頁面。


@SpringBootApplication
申明讓spring boot自動給程式進行必要的配置,這個配置等同於:
@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三個配置


@ResponseBody
該註解修飾的函式,會將結果直接填充到HTTP的響應體中,一般用於構建RESTful的api,該註解一般會配合@RequestMapping一起使用。
示例程式碼:
@RequestMapping("/test")
@ResponseBody
public String test(){
return"ok";
}

@Controller
用於定義控制器類,在spring 專案中由控制器負責將使用者發來的URL請求轉發到對應的服務介面(service層),一般這個註解在類中,通常方法需要配合註解@RequestMapping。

@RestController
@ResponseBody和@Controller的合集。

@EnableAutoConfiguration
Spring Boot自動配置(auto-configuration):
嘗試根據你新增的jar依賴自動配置你的Spring應用。
例如,如果你的classpath下存在HSQLDB,並且你沒有手動配置任何資料庫連線beans,那麼我們將自動配置一個記憶體型(in-memory)資料庫”。
你可以將@EnableAutoConfiguration或者@SpringBootApplication註解新增到一個@Configuration類上來選擇自動配置。
如果發現應用了你不想要的特定自動配置類,你可以使用@EnableAutoConfiguration註解的排除屬性來禁用它們。


@ComponentScan
表示將該類自動發現(掃描)並註冊為Bean,可以自動收集所有的Spring元件,包括@Configuration類。
我們經常使用@ComponentScan註解搜尋beans,並結合@Autowired註解匯入。
如果沒有配置的話,Spring Boot會掃描啟動類所在包下以及子包下的使用了@Service,@Repository等註解的類。


@Configuration
相當於傳統的xml配置檔案,如果有些第三方庫需要用到xml檔案,建議仍然通過@Configuration類作為專案的配置主類——可以使用@ImportResource註解載入xml配置檔案。
@Configuration
@EnableAutoConfiguration
public class RedisConfig {

@Bean(name="jedisPoolConfig")
@ConfigurationProperties(prefix="spring.redis")
public JedisPoolConfig getRedisConfig(){
return new JedisPoolConfig();
}
}

@PropertySource
如果需要有自定義的屬性檔案需要載入,可以使用該註解進行注入,並用@Value配合使用。
@Component
@PropertySource(value = "classpath:config.properties")
public class ConfigUtil {
@Value("${hos.id}")
private String hosId;
@Value("${hos.name}")
private String hosName;
}

@ImportResource
用來載入xml配置檔案。

@Bean
用@Bean標註方法等價於XML中配置的bean。

@Value
注入Spring boot application.properties配置的屬性的值。
@Value(value = "#{message}")
private String message;


Environment 
org.springframework.core.env.Environment,環境類,spring3.1以後開始引入。比如JDK環境,Servlet環境,Spring環境等等;每個環境都有自己的配置資料,如System.getProperties()、System.getenv()等可以拿到JDK環境資料;ServletContext.getInitParameter()可以拿到Servlet環境配置資料等等;也就是說Spring抽象了一個Environment來表示環境配置。
在springBoot中使用直接用@Resource注入,即可獲得系統配置檔案application.properties/yml的屬性值,如果是自定義的配置檔案,則需要預先通過@PropertySource等其他註解注入後,才能獲取。獲取通過getProperty()方法獲取。



1.與MyBatis的整合
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2、與Redis的整合
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
3、Junit進行單元測試
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>



springBoot的釋出
1.將springBoot專案打包成jar
可以使用maven將專案打包成jar檔案,並使用java -jar命令執行主main方法,將專案執行起來。
2.將springBoot專案打包成war
a1.pom檔案的命令將<packaging>jar</packaging>修改為war。
a2.入口類實現SpringBootServletInitializer方法,重寫方法:
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
a3.這裡指定打包的時候不再需要tomcat相關的包
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>


Summarize:
1. SpringBoot讓開發過程變得簡單,開發過程中配置變得容易
2. SpringBoot讓部署過程變得簡單
3. SpringBoot讓運維工作變得簡單

缺點:
-缺少註冊、發現等外圍方案
-缺少外圍監控整合方案
-缺少外圍安全管理方案
-缺少REST落地的URI規劃方案
所以SpringBoot只是一個入門級的微框架

 

 

 

 

==========================================================================================

 

 

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>


<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>

@RequestMapping("/list")
public ModelAndView PageList() {
String sql = "select * from student";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
ModelAndView mav = new ModelAndView("/WEB-INF/views/list.jsp");
mav.addObject("list", list);
return mav;
}

 


nohup /opt/converse_jar_QA/bin/jdk1.8.0_131/bin/java -javaagent:/opt/jacoco/lib/jacocoagent.jar=destfile=/opt/converse_jar_QA/jacoco.exec,includes=com.acxiom.standard.controller -jar /opt/converse_jar_QA/bin/converse.ws.jar spring.config.location=/opt/converse_jar_QA/bin/application.properties 2>&1 &

 

 application.yml 中 配置DB的Sample

注意縮排, 冒號之後的空格

 

spring:
datasource:
name: test
url: jdbc:sqlserver://xxxxxxxx:1433;databaseName=xxx
username: xxxxx
password: P3b#xxx*
# 使用druid資料來源
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20