SpringBoot--自動載入
1 單執行main方法後執行的事情如下:
在org.springframework.boot.autoconfigure.spring.factories中部分配置如下:
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
從上可知,Redis是自動配置的。
2 Redis配置修改
在RedisAutoConfiguration中:
@Configuration
@ConditionalOnClass({ JedisConnection.class, RedisOperations.class, Jedis.class })
//在專案中發現了JedisConnection.class, RedisOperations.class, Jedis.class會初始化redis配置。
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {
通過查閱全域性配置文件說明,可配置Redis伺服器相關資訊:
# REDIS (RedisProperties)
spring.redis.cluster.max-redirects= # Maximum number of redirects to follow when executing commands across the cluster.
spring.redis.cluster.nodes= # Comma-separated list of "host:port" pairs to bootstrap from.
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL, will override host, port and password (user will be ignored), e.g. redis://user:password@example.com:6379
spring.redis.host=localhost # Redis server host.
spring.redis.password= # Login password of the redis server.
spring.redis.ssl=false # Enable SSL support.
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of Redis server.
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.
spring.redis.timeout=0 # Connection timeout in milliseconds.
在src/main/respurces下建立的application.properties檔案,
spring.redis.url= 127.0.0.1
當執行SpringApplication初始化時,會自動初始化Redis相關的資訊。
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
/**
* Database index used by the connection factory.
*/
private int database = 0;
/**
* Redis url, which will overrule host, port and password if set.
*/
private String url;
/**
* Redis server host.
*/
private String host = "localhost";
如上,將會在application.properties中找spring.redis字首的配置資訊,例如spring.redis.url的值對映到String url中。
部分摘自某智播客。
3 建立一個自定義載入的模組,類似於啟動的時候自動載入spring.factory中的內容。
application.properties:
hello.msg=hello
配置檔案對映對應的類:
@ConfigurationProperties(prefix="hello")
public class HelloProperties {
private final String MSG="WORLD";
private String msg=MSG;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
被自動建立的服務:
public class HelloService {
private String msg;
public String sayHello(){
return "hello"+this.msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
自動建立中心:
@Configuration
@EnableConfigurationProperties(HelloProperties.class)//開啟宣告注入,將配置檔案中的值對映到變數中
@ConditionalOnClass(HelloService.class)//判斷HelloService在類路徑中是否存在,
@ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing=true)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
@ConditionalOnMissingBean(HelloService.class)//沒有的情況下自動建立
public HelloService helloService(){
HelloService helloService=new HelloService();
helloService.setMsg(helloProperties.getMsg());
return helloService;
}
}
在src/main/resources/下建立META-INF資料夾後在META-INF裝中建立spring.factories,
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.text.springboot.autoconfigure.hello.HelloServiceAutoConfiguration
Maven clean一下專案後install,在另外一個專案中依賴。啟動
<dependencies>
<dependency>
<groupId>text</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
</dependencies>
開啟debug模式:
application.properties
debug=true
啟動:
@RestController
@SpringBootApplication
public class TextApplication {
@Autowired
private HelloService helloService;
@RequestMapping("/")
public String index(){
return helloService.sayHello();
}
public static void main(String[] args) {
SpringApplication.run(TextApplication.class, args);
}
}
執行結果:
在
Positive matches:
中檢視到HelloService已自動載入。
訪問根目錄:
相關文章
- 自動載入
- SpringBoot--入門Spring Boot
- Composer 自動載入
- php 自動類載入類 composer.json 實現自動載入PHPJSON
- composer自動載入配置
- (十三)自動載入新模組
- PHP自動載入機制PHP
- 自動載入的iframe高度自適應
- ThinkPHP6 的自動載入PHP
- PHP 類自動載入機制PHP
- AIX啟動自動載入檔案系統AI
- 再看 Composer 自動載入原始碼原始碼
- Composer 自動載入原始碼解析原始碼
- 初學 PHP 類的自動載入PHP
- PHP 自動載入功能原理解析PHP
- composer應用(一)自動載入
- 2、Ktor學習-自動重新載入;
- sweetalert載入彈窗完成自動關閉
- 徹底搞懂Composer自動載入原理
- ZendFramework自動載入類的實現方法Framework
- 封裝ListView,實現自動載入更多封裝View
- PLSQL8.0自動載入器原始碼SQL原始碼
- 拖動滾動條實現內容自動載入效果
- 讓AutoCAD啟動時自動載入應用程式
- Laravel 原始碼筆記 Composer 自動載入Laravel原始碼筆記
- PHP 物件導向 (八)類的自動載入PHP物件
- PHP檔案的自動載入(autoloading)PHP
- DFL自動虛擬載入的使用說明
- ASM無法自動載入磁碟組問題ASM
- WINDOWS能夠自動載入程式的位置(轉)Windows
- tp5.0 的 模組配置自動載入問題
- PHP 的自動載入(持續學習更新中)PHP
- PHP名稱空間與自動載入簡述PHP
- PHP AutoLoad 自動載入機制分析實踐PHP
- PHP名稱空間及自動載入淺析PHP
- PHP自動載入__autoload的工作機制PHP
- express框架路由配置及controller自動載入Express框架路由Controller
- ListView下拉重新整理,上拉自動載入更多View