springboot?? application.yml? ?? ?? ??
Jun 03, 2023 pm 06:43 PM??????? ?? ?? ??? ??? ??? ????. ? ??? ??? ??? ???? ???? ??? ?? ? ??? ?? ???? ??? ?? ???? ? ?? ????. ??? ??? ??? ????? ? ????. ?? ?? ? ??? application.yml
??? ? ? ????. application.yml
文件中。
那么,怎么在代碼里獲取或者使用這個(gè)地址呢?有2個(gè)方法。
方法一:
我們可以通過@Value 注解的 ${key} 即可獲取配置文件(application.yml)中和 key 對(duì)應(yīng)的 value 值,這個(gè)方法適用于微服務(wù)比較少的情形
方法二:
在實(shí)際項(xiàng)目中,遇到業(yè)務(wù)繁瑣,邏輯復(fù)雜的情況,需要考慮封裝一個(gè)或多個(gè)配置類。比如,如果一個(gè)業(yè)務(wù)需要同時(shí)使用微服務(wù)1、微服務(wù)2和微服務(wù)3,那么這些微服務(wù)將被調(diào)用。
如果這樣一個(gè)個(gè)去使用 @Value 注解引入相應(yīng)的微服務(wù)地址的話,太過于繁瑣。
也許實(shí)際業(yè)務(wù)中,遠(yuǎn)遠(yuǎn)不止這三個(gè)微服務(wù),甚至十幾個(gè)都有可能。對(duì)于這種情況,我們可以先定義一個(gè) MicroServiceUrl

??? , ??? ???? ? ??? ??? ?????? 2?? ??? ????.
?? 1:
@Value ??? ?? ${key}? ?? ?? ??(application.yml)? ?? ???? ?? ?? ? ????. ? ??? ????? ?? ?? ??? ?????. ??
?? 2:

MicroServiceUrl
???? ???? ???????? URL? ????? ??? ? ??????import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "url") public class MicroServiceUrl { private String orderUrl; private String userUrl; private String shoppingUrl; public String getOrderUrl() { return orderUrl; } public void setOrderUrl(String orderUrl) { this.orderUrl = orderUrl; } public String getUserUrl() { return userUrl; } public void setUserUrl(String userUrl) { this.userUrl = userUrl; } public String getShoppingUrl() { return shoppingUrl; } public void setShoppingUrl(String shoppingUrl) { this.shoppingUrl = shoppingUrl; } }??????? ??:????
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>???? ??? ???? ?? ????. ??? ???? ???? ???. ??? ???????? URL? ??? ???? @Resource ??? ?? ?? ??? ?? ???? ?? ???? ?????. ??? ??? ???????. ????????
import com.example.test1.config.MicroServiceUrl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * 獲取配置文件(application.yml)中和 key 對(duì)應(yīng)的 value 值 * 2種方法 */ @RestController @RequestMapping("/test") public class ConfigController { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class); @Value("${url.orderUrl}") private String orderUrl; @Resource private MicroServiceUrl microServiceUrl; @RequestMapping("/config") public String testConfig() { LOGGER.info("獲取的地址為:{}", orderUrl); LOGGER.info("微服務(wù)1地址為:{}", microServiceUrl.getOrderUrl()); LOGGER.info("微服務(wù)2地址為:{}", microServiceUrl.getUserUrl()); LOGGER.info("微服務(wù)3地址為:{}", microServiceUrl.getShoppingUrl()); return "success"; } }
? ??? springboot?? application.yml? ?? ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Jasypt ?? Jasypt? ???? ???? ???? ????? ?? ??? ??? ??? ? ?? ??? ??? ?? ??? ?? ?? ??? ???? ?? ??? ? ??? ???? ?? ?? ??? ?????. ?? ?? ??? ??. ????, ???, ??, ???? ???... Spring ?? ??????, ??? API?? ??? ???? ?? JCE ???? ?? ??? ? ????... ?? ???? ?????: com.github.ulisesbocchiojasypt-spring-boot-starter2. Jasypt? ??? ??? ?????? ??? ??? ??? ? ?? ??? ??? ?????.

1. Redis? ?? ?? ??? ?? ??? ??? ??? ?????. ?? ??? ?? ????? ?? ?? ??? ??? ??? ???? ???. ?? ??? ??? ??? ?????. ?? ??? ????? ??? ? ?? ??? ??? ???? ???? ??? ??? ???? ?? ????? ??? ???? ?? ??? ?? ?????. ?? ??? ?? ??? ??? ????? ????. ??? ?? ???? ???? ?? ?? ????? ?? ?? ??? ?? ???? ? ????? ???? ???? ??????? ????? ??????? ???????. ?? ??? ??????? ?? ????? ????? ????? ???. ?? ????? MySQL?? ??? ??? ?? ???? ?? ?? ??? ???? ?? ???? ?? ???? ????? ??? ?????. ?? ??? ???? ?? ????. ???? ?? ??

1. RedisAPI ?? ??? ????? RedisTemplate1.1? ??? ?????. API ?? Redis ?? ??? ??? ?? ??? RedisTemplate ???? ?????. ??? RedisTemplate ???? ?? ???? ?? ?? ??? ???. ? ??, ?? ??? ??? ??, ?? ?? ?? ?? @NullableprivateRedisSe

Springboot? ??? ??? jar ???? ???? ? ?? ??? ???? ? ????. springboot? ??? jar ???? ???? ? ??? ?? ? ?? ??? ?????. ? ??? ??? ? ??? ?? ?? ?????. ???? ??? ??? ???? ???? ? ????. ??? ??? publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input ??? ????.

?? ???? 1. ??? ????? ?????? 30? ??? ??? ????? ?????. ?? ??? ???? ??? ???? ???????. 2. ??? ?????? ?? ? 7? ?? ??? ???? ?????. ?? ??? ???? ???? ??? ???? ????? ??? ??? ?????. 3. ???? 5? ?? ??? ?? ??? ??? ?????. ?? ??? ??? ?????... ??? ?? ??? ??? ?? ????? ?? ?? ??? ???? ???? ?? ??? ??? ? ????. ?: xxl-job ??? ??? ???????.

Springboot+Mybatis-plus? ?? ??? ?? ??? ???? ?? SQL ?? ???? ?? ? ?? ??? ??? ??? ???? ??? ??????? ?????. ?? ??? ?? BrandDTO ??? ???? ???? ?? ?? ??? ????????. Mybatis-plus?? ?? ??? ??? ???? ?? ?? ???? ?? Mybatis-plus-join? ?? ??? ???? ??? ?? Mapper.xml ??? ???? ???? ? ResultMap? ???? ???. ?? SQL ?? ?????. ? ??? ???? ???? ?? ???? ??? ??? ? ????.

SpringBoot? SpringMVC? ?? Java ???? ????? ???? ???????? ? ???? ? ?? ??? ???? ????. ? ????? ? ? ?????? ??? ??? ???? ???? ??? ????. ?? SpringBoot? ?? ?????. SpringBoot? Spring ?????? ???? ?? ??????? ?? ? ??? ????? ?? Pivotal ??? ???????. ?? ??? ?? ??? ???? ??? ??? ??? ?????.

1. @Import? ?? ???? ?????. @Import? ?? ???? ???? ?? ???? Bean?? ???? ? ??? ???. @SpringBootApplication(?? ???), @Configuration(?? ???), @Component(???? ???)? ???? ???? @Import? ??? ? ????. ??: @RestController, @Service ? @Repository? ?? @Import ??? ?? @Component@SpringBootApplication@Import(ImportBean.class)//ImportBean? ????.
