最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot3外部化配置與aop實現(xiàn)方案

 更新時間:2026年01月21日 09:08:22   作者:alineverstop  
這篇文章給大家介紹SpringBoot3外部化配置與aop實現(xiàn)方案,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

POM文件中為何要以繼承的方式引入SpringBoot?

繼承父工程的優(yōu)勢

  1. 依賴管理:在父工程中定義依賴的版本,子模塊直接引用而不必指定版本號
  2. 插件管理:在父工程中配置插件,子模塊直接使用
  3. 屬性設(shè)置:在父工程中定義一些通用屬性,如項目編碼、java版本等
  4. 統(tǒng)一配置:可以統(tǒng)一多個子模塊的構(gòu)建配置,確保一致性。

繼承了父工程,那么引入依賴的時候不需要指定版本號,因為在父工程中,各種依賴的版本號已經(jīng)預(yù)設(shè)好了。

SpringBoot核心注解

@SpringBootApplication

被此標注表示該類是SpringBoot項目的入口類。

此注解被以下三個注解標注,說明@SpringBootApplication同時有以下三個注解的功能

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

@SpringBootConfiguration

@SpringBootConfiguration被@Configuration標注說明項目的主入口類同時是一個配置類,因此在主入口類中使用@Bean注解方法的話,該方法返回值對象會被納入Ioc容器管理。

@EnableAutoConfiguration

啟用自動配置,SpringBoot默認情況下啟用自動配置。

自動配置有什么用?

自動配置只要啟動,SpringBoot就會去類路徑中查找Class。根據(jù)類路徑中有某些類來自動管理Bean,不需要程序員手動配置。

比如SpringBoot檢測到SqlSessionFactory,或者在application.properties中配置了數(shù)據(jù)源,SpringBoot會認為項目中含有MyBatis框架,會將MyBatis相關(guān)的Bean初始化,然后放到Ioc容器中管理起來。

@ComponentScan

@ComponentScan負責(zé)組件掃描。會掃描此包及此包下所有子包或子包的子包等的路徑。

外部化配置

外部化配置是指將配置信息存儲 在應(yīng)用程序代碼之外的地方。這樣配置信息獨立于 代碼進行管理。方便配置修改。修改后不需要重新編譯,也不需要重新部署。

springboot默認先找外部化配置

Application.properites

  • Application.properites配置文件是SpringBoot默認的額配置文件
  • Application.properites不是必須的,SpringBoot提供了默認配置,如果需要修改默認配置,就在Application.properites中進行配置。
  • Application.properites可以放在類路徑中,也可以放在項目之外,因此成為外部化配置

SpringBoot在啟動時會從以下位置按順序加載Application.properites:

  1. file:./config/: 首先在SpringBoot當前工作目錄下的config文件夾中查找(如果沒找到Application.properites,會繼續(xù)查找Application.yml,2個都沒找到,才會進入下一個位置查找,以此類推)
  2. file:./: 這里找不到會繼續(xù)查找下一個位置
  3. classpath:/config/:
  4. classpath:/

如果在多個位置有相同屬性的定義,那么最先檢查的位置中的屬性值先使用。

如果要指定配置文件位置,可以通過--spring.config.location=進行指定,比如:

java -jar xxxx.jar --spring.config.location=file:///E:\a\b\application.properties

注意:以上的--spring.config.location=file:///E:\a\b\application.properties屬于命令行參數(shù),會被傳遞到main方法的(String[] args)參數(shù)上。

讀取配置

// 讀取配置文件中myapp.path的值,
// 如果這個key不存在,并且沒有指定默認值,那么會報錯
    // ${myapp.path:50}指定myapp.path的默認值是50
    @Value("${myapp.path:50}")
    private String appPath;

YAML語法規(guī)則

數(shù)據(jù)結(jié)構(gòu)

  • 支持多種數(shù)據(jù)結(jié)構(gòu),包括:字符串、數(shù)字、布爾值、數(shù)組、List集合、Map鍵值對等
  • yaml使用一個空格來分割屬性名和屬性值,比如:
name: jack
  1. yaml使用換行+空格表示層級關(guān)系,注意不能使用tab 必須是空格,空格數(shù)量無要求,建議2個或4個,比如:
myapp: 
  name: mall
  1. 同級元素左側(cè)對其
  2. 大小寫敏感
  3. 使用# 進行注釋
  4. 在一個映射中,鍵必須唯一
  5. 普通文本可以使用單引號,也可以使用雙引號,也可以什么都不用(單引號中的\n表示普通文本,雙引號中的\n表示換行)
  6. 保留文本原格式使用 | 比如:
username: |
  aaaa
  bbb
  ccc
  1. 文檔切割: --- 這個符號下面的配置認為是一個獨立的yaml文件,便于大文件的閱讀。

配置文件合并

#properties文件合并
# 對于數(shù)組來說,使用逗號進行分隔開
spring.config.import=classpath:/application-mysql.properties,classpath:/application-redis.properties

yml文件合并的第一種寫法

spring: 
  config: 
    import: [classpath:/application-mysql.yml,classpath:/application-redis.yml]

yml文件合并的第二種寫法

spring: 
  config: 
    import: 
      - classpath:/application-mysql.yml
      - classpath:/application-redis.yml

多環(huán)境切換

開發(fā)環(huán)境配置文件:application-dev.properties

測試環(huán)境配置文件:application-test.properties

預(yù)生產(chǎn)環(huán)境配置文件:application-preprod.properties

生產(chǎn)環(huán)境配置文件:application-prod.properties

如果啟用生產(chǎn)環(huán)境配置,可以有以下兩種操作方式:

  1. 在application.properties添加配置:spring.profiles.active=prod
  2. 在命令行參數(shù)上添加: --spring.profiles.active=prod

將配置綁定到簡單Bean

package com.ali.bindtobean.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
// 納入Ioc容器
@Component
// 將配置文件一次性綁定到Bean對象上
@ConfigurationProperties(prefix = "myapp")
public class AppConfig {
    // 要實現(xiàn)一次性綁定,配置文件中的屬性名 必須和Bean對象的屬性名要一致
    // 底層在給對象屬性賦值時,調(diào)用了setter方法,因此每個屬性必須有setter方法
  private String name;
  private Integer age;
  private String password;
  private Boolean gender;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Boolean getGender() {
        return gender;
    }
    public void setGender(Boolean gender) {
        this.gender = gender;
    }
    @Override
    public String toString(){
        return "AppConfig [name=" + name + ", age=" + age + ", gender=" + gender + "]";
    }
}
spring.application.name=bindtobean
myapp.name=jack
myapp.age=12
myapp.password=123
myapp.gender=true

綁定嵌套Bean

在一個Bean的屬性中,有一個其他Bean類型。這樣就是嵌套Bean。

package com.ali.bindtobean.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app.xyz")
public class User {
    private String name;
    private Address address;
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", address=" + address.toString() + "]";
    }
}
package com.ali.bindtobean.bean;
public class Address {
    private String city;
    private String street;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    @Override
    public String toString() {
        return "Address [city=" + city + ", street=" + street + "]";
    }
}
app.xyz.name=lucy
app.xyz.address.city=xj
app.xyz.address.street=dayang

其他方式綁定Bean

// 在主入口程序添加以下注解,啟用將配置信息綁定到User這個Bean
@EnableConfigurationProperties({User.class, Address.class})

另一種方式:

// 在主入口程序添加以下注解,掃面指定包。將配置信息綁定到這個包下的類
@ConfigurationPropertiesScan(basePackages = "com.ali.bindtobean.bean")

復(fù)雜的屬性結(jié)構(gòu)綁定Bean

綁定數(shù)組、集合、Map到Bean

app2.abc.names[0]=jack
app2.abc.names[1]=lucy
app2.abc.names[2]=tom
app2.abc.addresses[0].city=bj
app2.abc.addresses[0].street=chaoyang
app2.abc.addresses[1].city=tj
app2.abc.addresses[1].street=nankai
app2.abc.addressList[0].city=bj_list
app2.abc.addressList[0].street=chaoyang_list
app2.abc.addressList[1].city=tj_list
app2.abc.addressList[1].street=nankai_list
# addr1 和addr2 都是key
app2.abc.addressMap.addr1.city=bj_map
app2.abc.addressMap.addr1.street=chaoyang_map
app2.abc.addressMap.addr2.city=tj_map
app2.abc.addressMap.addr2.street=nankai_map

yaml文件配置方式如下:

app2:
    abc:
        names:
          - tom
          - smith
        addresses:
            - city: beijing
              street: chaoyang
            - city: tianjin
              street: nankai
       # addressList 可以寫成 address-list
        addressList:
            - city: beijing
              street: chaoyang
            - city: tianjin
              street: nankai
        addressMap:
            addr1:
                city: beijing
                street: chaoyang
            addr2:
                city: tianjin
                street: nankai
package com.ali.bindtobean.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Map;
@ConfigurationProperties(prefix = "app2.abc")
public class AppBean {
    // 數(shù)組中元素是簡單類型
    private String[] names;
    // 數(shù)組中元素是Bean
    private Address[] addresses;
    //List集合。List中元素是Bean
    private  List<Address> addressList;
    //Map集合: String,Bean
    private Map<String,Address> addressMap;
    public void setNames(String[] names) {
        this.names = names;
    }
    public void setAddresses(Address[] addresses) {
        this.addresses = addresses;
    }
    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }
    public void setAddressMap(Map<String, Address> addressMap) {
        this.addressMap = addressMap;
    }
    @Override
    public String toString() {
        return "";
    }
}

將配置綁定到第三方對象

other:
  abc:
    city: beijing
    street: daxing
package com.ali.bindtobean.config;
import com.ali.bindtobean.bean.Address;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig2 {
    //  address 是第三方類,使用以下方式完成配置到屬性的綁定
    @Bean
    @ConfigurationProperties(prefix = "other.abc")
    public Address address() {
        return new Address();
    }
}

指定配置數(shù)據(jù)來源

@Component
@ConfigurationProperties(prefix = "app2.abc")
// 指定配置數(shù)據(jù)來自/a/b/group-info.properties路徑的配置文件
@PropertySource("classpath:/a/b/group-info.properties")
public class AppBean {
...
}

@ImportResource注解

當SpringBoot項目中出現(xiàn)ApplicationContext.xml文件。并且文件中配置了Bean。要把這個Bean注入到容器中。

package com.ali.bindtobean.bean;
public class Person {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return super.toString();
    }
}

配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="person" class="com.ali.bindtobean.bean.Person">
        <property name="name" value="jack"/>
        <property name="age" value="20"/>
    </bean>
</beans>
// 在主入口程序添加以下注解讓applocationContext.xml文件生效
@ImportResource("classpath:/applocationContext.xml")

Environment

spring提供的一個接口。SpringBoot啟動的時候會把環(huán)境、系統(tǒng)信息封裝到Environment對象中,需要獲取這些信息,可使用Environment接口的方法。

Environment對象主要包括

  • 當前激活的配置文件 active-profiles
  • 系統(tǒng)屬性,如系統(tǒng)名字 、java版本
  • 環(huán)境變量
  • 應(yīng)用程序啟動時傳給主方法的命令行參數(shù)
@Autowired
private Environment environment;
public void  doSomething() {
    // 獲取當前激活的配置文件
    String[] activeProfiles = environment.getActiveProfiles();
    for (String activeProfile : activeProfiles) {
        System.out.println(activeProfile);
    }
    //  獲取配置信息
    String property = environment.getProperty("app.xyz.address.city");
    System.out.println(property);
}

SpringBoot aop實現(xiàn)

添加依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>3.3.5</version>
</dependency>

編寫切面類

package com.ali.springaop.component;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import java.util.Arrays;
// 指定切面類
@Aspect
@Component
public class LoggingAspect {
    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
    // 定義切入點,匹配所有以“service”結(jié)尾的包下的所有方法
    @Pointcut("execution(* com.ali.springaop.service..*(..))")
    public void ServiceMethods(){
    }
    // 前置通知,切入點的方法執(zhí)行前執(zhí)行此代碼
    @Before("ServiceMethods()")
    public void before(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.info("Method [{}] with parameters [{}] is called", methodName, Arrays.toString(args));
    }
}

到此這篇關(guān)于SpringBoot3外部化配置與aop實現(xiàn)方案的文章就介紹到這了,更多相關(guān)SpringBoot外部化配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Tomcat 實現(xiàn)WebSocket詳細介紹

    Tomcat 實現(xiàn)WebSocket詳細介紹

    這篇文章主要介紹了Tomcat 如何實現(xiàn)WebSocket的相關(guān)資料,對WebSocket協(xié)議通信的過程進行了詳細介紹,需要的朋友可以參考下
    2016-12-12
  • 淺談Java常見的排序算法

    淺談Java常見的排序算法

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著Java常見的排序算法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 編程入門:掌握Java運算符技巧

    編程入門:掌握Java運算符技巧

    掌握Java運算符技巧,能讓你的編程之旅輕松許多,本指南將帶你深入了解如何巧妙地使用這些強大的工具,讓代碼不僅高效,還充滿樂趣,跟著我們一起,讓你的Java代碼在運算符的魔法下煥發(fā)新生!
    2023-12-12
  • Java中indexOf函數(shù)示例詳解

    Java中indexOf函數(shù)示例詳解

    Java String 類的 indexOf() 方法返回指定字符串中指定字符或字符串第一次出現(xiàn)的位置,這篇文章主要介紹了Java中indexOf函數(shù)詳解,需要的朋友可以參考下
    2024-01-01
  • Java線程池內(nèi)部任務(wù)出異常后發(fā)現(xiàn)異常的3種方法

    Java線程池內(nèi)部任務(wù)出異常后發(fā)現(xiàn)異常的3種方法

    Java線程池是一種并發(fā)編程工具,它允許開發(fā)者以線程池的形式重用線程,從而避免頻繁創(chuàng)建和銷毀線程所帶來的開銷,這篇文章主要給大家介紹了關(guān)于Java線程池內(nèi)部任務(wù)出異常后發(fā)現(xiàn)異常的3種方法,需要的朋友可以參考下
    2025-07-07
  • springboot集成flyway全過程

    springboot集成flyway全過程

    這篇文章主要介紹了springboot集成flyway全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • 基于JDOM生成解析XML過程解析

    基于JDOM生成解析XML過程解析

    這篇文章主要介紹了基于JDOM生成解析XML過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Spring Security  整體架構(gòu)操作流程

    Spring Security  整體架構(gòu)操作流程

    這篇文章主要介紹了Spring Security  整體架構(gòu)操作流程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-07-07
  • Ribbon和Feign的區(qū)別及說明

    Ribbon和Feign的區(qū)別及說明

    本文介紹了Spring Cloud Netflix中的兩個負載均衡組件:Ribbon和Feign,Ribbon是一個基于HTTP和TCP客戶端的負載均衡工具,使用起來較為繁瑣,而Feign是一個使用接口方式的HTTP客戶端,采用類似MyBatis的@Mapper注解方式,使得編寫客戶端變得非常容易
    2024-11-11
  • 使用nacos實現(xiàn)自定義文本配置的實時刷新

    使用nacos實現(xiàn)自定義文本配置的實時刷新

    我們都知道,使用Nacos時,如果將Bean使用@RefreshScope標注之后,這個Bean中的配置就會做到實時刷新,本文給大家介紹了如何使用nacos實現(xiàn)自定義文本配置的實時刷新,需要的朋友可以參考下
    2024-05-05

最新評論

临汾市| 台湾省| 阿合奇县| 潮州市| 台南市| 武乡县| 南乐县| 醴陵市| 库车县| 嘉峪关市| 依兰县| 晋江市| 贞丰县| 丰都县| 普宁市| 江孜县| 南岸区| 金湖县| 河津市| 日土县| 莱西市| 吉水县| 遵化市| 林周县| 金华市| 桃园市| 冷水江市| 象州县| 盐山县| 乐至县| 商河县| 阜城县| 呼和浩特市| 泸水县| 德清县| 姚安县| 余江县| 罗江县| 务川| 丰城市| 武隆县|