欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot四大神器之Auto onfiguration的使用

 更新時(shí)間:2021年10月31日 16:16:23   作者:peterwanghao  
本文主要介紹了SpringBoot四大神器之Auto Configuration,springboot auto configuration的本質(zhì)就是自動(dòng)配置spring的各種bean。感興趣的可以了解一下

Spring Boot非常簡(jiǎn)單容易上手,它隱藏了很多內(nèi)容而不需要你去關(guān)心。但對(duì)于一個(gè)好的開發(fā)人員也許希望知道Spring Boot自動(dòng)配置背后到底發(fā)生了什么?

Spring Boot并不屬于一種新的技術(shù),只不過Spring Boot的啟動(dòng)器幫我們配置了若干個(gè)被Spring管理的bean,當(dāng)我們的項(xiàng)目依賴這些jar并啟動(dòng)Spring應(yīng)用時(shí),Spring的Container容器已經(jīng)把jar包下的對(duì)象加以創(chuàng)建及管理了。

簡(jiǎn)而言之,Spring Boot自動(dòng)配置代表了一種基于類路徑上存在的依賴關(guān)系自動(dòng)配置Spring應(yīng)用程序的方法。還可以通過定義消除自動(dòng)配置類中包含的某些bean。這些可以使開發(fā)更快更容易。

springboot auto configuration的本質(zhì)就是自動(dòng)配置spring的各種bean。然后使應(yīng)用可以通過@Autowired等注入方式來直接使用bean。比如自動(dòng)配置redisTemplate,jdbcTemplate等bean。

1. 通過啟動(dòng)類創(chuàng)建Spring Boot應(yīng)用

創(chuàng)建Spring Boot應(yīng)用非常簡(jiǎn)單,只要?jiǎng)?chuàng)建一個(gè)包含main的啟動(dòng)類即可。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
 
@SpringBootApplication
public class App
{
    public static void main(String[] args)
    {
        ApplicationContext ctx = SpringApplication.run(App.class, args);
    }
}

上面這個(gè)類被稱為Spring Boot應(yīng)用的啟動(dòng)類,它通過一個(gè)java的main()方法來引導(dǎo)和啟動(dòng)一個(gè)Spring應(yīng)用。它通常包含了以下內(nèi)容:

  • 創(chuàng)建一個(gè)Spring ApplicationContext實(shí)例。
  • 接收命令行參數(shù)并將其轉(zhuǎn)為Spring屬性。
  • 按照配置加載所有Spring Bean??梢愿鶕?jù)項(xiàng)目需求進(jìn)行其他操作。

2. @SpringBootApplication注解

這個(gè)注解其實(shí)是一個(gè)應(yīng)用了3個(gè)注解的快捷方式。

2.1 @SpringBootConfiguration

@SpringBootConfiguration是在Spring Boot2中出現(xiàn)的一個(gè)新的注解。之前我們都是使用的 @Configuration注解,可以用 @Configuration來替換它,2個(gè)都是實(shí)現(xiàn)同樣的功能。

它表示該類是一個(gè)配置類,應(yīng)該對(duì)其進(jìn)行掃描,以獲得進(jìn)一步的配置和bean定義。

2.2 @EnableAutoConfiguration

此注解用于啟用Spring Application Context的自動(dòng)配置,嘗試猜測(cè)和配置您可能需要的bean。自動(dòng)配置類通?;谀念惵窂揭约澳x的bean來應(yīng)用。

自動(dòng)配置嘗試盡可能智能,并在您定義更多自己的配置時(shí)進(jìn)行后退。您始終可以使用兩種方法來手動(dòng)排除任何您不想應(yīng)用的配置:

  • 使用excludeName()
  • 使用spring.autoconfigure.exclude屬性文件中的屬性。

2.3 @ComponentScan

此注解提供了與Spring XML context:component-scan元素并行的支持。

無(wú)論是basePackageClasses()或basePackages()可以定義特定的軟件包進(jìn)行掃描。如果未定義特定包,則將從聲明此注解的類的包進(jìn)行掃描。

3.自定義自動(dòng)配置

要?jiǎng)?chuàng)建自定義自動(dòng)配置,我們需要?jiǎng)?chuàng)建一個(gè)注釋為@Configuration的類并注冊(cè)它。

讓我們?yōu)镸ySQL數(shù)據(jù)源創(chuàng)建自定義配置:

@Configuration
public class MySQLAutoconfiguration {
  //...
}

下一個(gè)必須的步驟是通過在標(biāo)準(zhǔn)文件資源/ META-INF / spring.factories中的屬性org.springframework.boot.autoconfigure.EnableAutoConfiguration下添加類的名稱,將類注冊(cè)為自動(dòng)配置候選者:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.peterwanghao.samples.springboot.autoconfiguration.MySQLAutoconfiguration

如果我們希望我們的自動(dòng)配置類優(yōu)先于其他自動(dòng)配置候選者,我們可以添加@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)注解。

自動(dòng)配置是使用標(biāo)有@Conditional注解的類和bean設(shè)計(jì)的,以便可以替換自動(dòng)配置或其特定部分。

請(qǐng)注意,只有當(dāng)應(yīng)用程序中未定義自動(dòng)配置的bean時(shí),自動(dòng)配置才有效。如果您定義了bean,那么將覆蓋默認(rèn)值。

3.1 基于類的條件注解

Class conditions允許我們指定使用@ConditionalOnClass注解指定的類,或者使用@ConditionalOnMissingClass注解來指定不存在于 classpath 上的類。

讓我們指定只有存在類DataSource的情況下才會(huì)加載MySQLConfiguration,在這種情況下我們可以假設(shè)應(yīng)用程序?qū)⑹褂脭?shù)據(jù)庫(kù):

@Configuration
@ConditionalOnClass(DataSource.class)
public class MySQLAutoconfiguration {
    //...
}

3.2 基于Bean的條件注解

如果我們只想在指定的bean存在的情況下包含bean,我們可以使用@ConditionalOnBean和@ConditionalOnMissingBean注解。

舉例說明,讓我們將一個(gè)entityManagerFactory bean 添加到我們的配置類中,并指定如果存在一個(gè)名為dataSource的bean 并且尚未定義一個(gè)名為entityManagerFactory的 bean,我們就創(chuàng)建這個(gè)bean :

@Bean
 @ConditionalOnBean(name = "dataSource")
 @ConditionalOnMissingBean
 public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  em.setDataSource(dataSource());
  em.setPackagesToScan("com.peterwanghao.samples.springboot.autoconfiguration.example");
  em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  if (additionalProperties() != null) {
   em.setJpaProperties(additionalProperties());
  }
  return em;
 }

讓我們配置一個(gè)只在尚未定義類型為JpaTransactionManager的bean時(shí)才會(huì)加載的transactionManager bean :

@Bean
 @ConditionalOnMissingBean(type = "JpaTransactionManager")
 JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
  final JpaTransactionManager transactionManager = new JpaTransactionManager();
  transactionManager.setEntityManagerFactory(entityManagerFactory);
  return transactionManager;
 }

3.3 基于屬性的條件注解

@ConditionalOnProperty注解用于指定是否配置將基于Spring環(huán)境屬性的存在和值被加載。

首先,讓我們?yōu)榕渲锰砑右粋€(gè)屬性源文件,以確定從哪里讀取屬性:

@PropertySource("classpath:mysql.properties")
public class MySQLAutoconfiguration {
    //...
}

我們可以配置主DataSource bean,它將用于創(chuàng)建與數(shù)據(jù)庫(kù)的連接,只有在存在名為usemysql的屬性時(shí)才會(huì)加載它。

我們可以使用屬性havingValue來指定必須匹配的usemysql屬性的某些值。

如果usemysql屬性設(shè)置為local,讓我們使用默認(rèn)值定義dataSource bean,該默認(rèn)值連接到名為myDb的本地?cái)?shù)據(jù)庫(kù):

@Bean
 @ConditionalOnProperty(name = "usemysql", havingValue = "local")
 @ConditionalOnMissingBean
 public DataSource dataSource() {
  final DriverManagerDataSource dataSource = new DriverManagerDataSource();

  dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
  dataSource.setUrl("jdbc:mysql://localhost:3306/myDb?createDatabaseIfNotExist=true&&serverTimezone=GMT%2B8");
  dataSource.setUsername("root");
  dataSource.setPassword("123456");

  return dataSource;
 }

如果usemysql屬性設(shè)置為自定義,則數(shù)據(jù)源 bean將使用自定義屬性值的數(shù)據(jù)庫(kù)URL,用戶和密碼進(jìn)行配置:

@Bean(name = "dataSource")
 @ConditionalOnProperty(name = "usemysql", havingValue = "custom")
 @ConditionalOnMissingBean
 public DataSource dataSource2() {
  final DriverManagerDataSource dataSource = new DriverManagerDataSource();

  dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
  dataSource.setUrl(env.getProperty("mysql.url"));
  dataSource.setUsername(env.getProperty("mysql.user") != null ? env.getProperty("mysql.user") : "");
  dataSource.setPassword(env.getProperty("mysql.pass") != null ? env.getProperty("mysql.pass") : "");

  return dataSource;
 }

該mysql.properties文件將包含usemysql屬性:

usemysql=local

如果使用MySQLAutoconfiguration的應(yīng)用程序希望覆蓋默認(rèn)屬性,則它需要做的就是為mysql.properties文件中的mysql.url,mysql.user和mysql.pass屬性添加不同的值以及添加usemysql = custom行。

3.4 基于資源的條件注解

添加@ConditionalOnResource注解意味著僅在存在指定資源時(shí)才加載配置。

讓我們定義一個(gè)名為additionalProperties()的方法,該方法將返回一個(gè)Properties對(duì)象,該對(duì)象包含entityManagerFactory bean 使用的特定于Hibernate的屬性,僅當(dāng)存在資源文件mysql.properties時(shí):

@ConditionalOnResource(resources = "classpath:mysql.properties")
 @Conditional(HibernateCondition.class)
 final Properties additionalProperties() {
  final Properties hibernateProperties = new Properties();

  hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("mysql-hibernate.hbm2ddl.auto"));
  hibernateProperties.setProperty("hibernate.dialect", env.getProperty("mysql-hibernate.dialect"));
  hibernateProperties.setProperty("hibernate.show_sql",
    env.getProperty("mysql-hibernate.show_sql") != null ? env.getProperty("mysql-hibernate.show_sql")
      : "false");

  return hibernateProperties;
 }

我們可以將Hibernate特定的屬性添加到mysql.properties文件中:

mysql-hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
mysql-hibernate.show_sql=true
mysql-hibernate.hbm2ddl.auto=create-drop

3.5 自定義條件

如果我們不想使用Spring Boot中的任何可用條件,我們還可以通過擴(kuò)展SpringBootCondition類并重寫getMatchOutcome()方法來定義自定義條件。

讓我們?yōu)閍dditionalProperties()方法創(chuàng)建一個(gè)名為HibernateCondition的條件,該方法將驗(yàn)證類路徑上是否存在HibernateEntityManager類:

static class HibernateCondition extends SpringBootCondition {

  private static final String[] CLASS_NAMES = { "org.hibernate.ejb.HibernateEntityManager",
    "org.hibernate.jpa.HibernateEntityManager" };

  @Override
  public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
   ConditionMessage.Builder message = ConditionMessage.forCondition("Hibernate");

   return Arrays.stream(CLASS_NAMES)
     .filter(className -> ClassUtils.isPresent(className, context.getClassLoader()))
     .map(className -> ConditionOutcome.match(message.found("class").items(Style.NORMAL, className)))
     .findAny().orElseGet(() -> ConditionOutcome.noMatch(
       message.didNotFind("class", "classes").items(Style.NORMAL, Arrays.asList(CLASS_NAMES))));
  }

 }

然后我們可以將條件添加到additionalProperties()方法:

@Conditional(HibernateCondition.class)
Properties additionalProperties() {
  //...
}

3.6 申請(qǐng)條件

我們還可以通過添加@ConditionalOnWebApplication或@ConditionalOnNotWebApplication注釋來指定只能在Web上下文內(nèi)部/外部加載配置。

4. 測(cè)試自動(dòng)配置

讓我們創(chuàng)建一個(gè)非常簡(jiǎn)單的例子來測(cè)試我們的自動(dòng)配置。我們將使用Spring Data 創(chuàng)建一個(gè)名為MyUser的實(shí)體類和一個(gè)MyUserRepository接口:

@Entity
public class MyUser {
 @Id
 private String email;

 public MyUser() {
 }

 public MyUser(String email) {
  super();
  this.email = email;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }
}

public interface MyUserRepository extends JpaRepository<MyUser, String> {

}

要啟用自動(dòng)配置,我們可以使用@SpringBootApplication或@EnableAutoConfiguration注解:

@SpringBootApplication
public class AutoconfigurationApplication {

 public static void main(String[] args) {
  SpringApplication.run(AutoconfigurationApplication.class, args);
 }
}

接下來,讓我們編寫一個(gè)保存MyUser實(shí)體的JUnit測(cè)試:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AutoconfigurationApplication.class)
@EnableJpaRepositories(basePackages = { "com.peterwanghao.samples.springboot.autoconfiguration.example" })
public class AutoconfigurationLiveTest {

    @Autowired
    private MyUserRepository userRepository;

    @Test
    public void whenSaveUser_thenOk() {
        MyUser user = new MyUser("user@email.com");
        userRepository.save(user);
    }
}

 由于我們尚未定義DataSource配置,因此應(yīng)用程序?qū)⑹褂梦覀儎?chuàng)建的自動(dòng)配置連接到名為myDb的MySQL數(shù)據(jù)庫(kù)。

連接字符串包含createDatabaseIfNotExist = true屬性,因此數(shù)據(jù)庫(kù)不需要存在。但是,需要?jiǎng)?chuàng)建用戶mysqluser或通過mysql.user屬性指定的用戶mysqluser。

我們可以檢查應(yīng)用程序日志,看看是否正在使用MySQL數(shù)據(jù)源:

10:31:47.092 [main] INFO  org.hibernate.Version - HHH000412: Hibernate Core {5.3.7.Final}
10:31:47.094 [main] INFO  org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
10:31:47.227 [main] INFO  o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
10:31:48.039 [main] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
Hibernate: drop table if exists MyUser
Hibernate: create table MyUser (email varchar(255) not null, primary key (email)) engine=InnoDB
10:31:48.655 [main] INFO  o.h.t.s.internal.SchemaCreatorImpl - HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@3a0b6a'
10:31:48.666 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default'
10:31:49.496 [main] INFO  o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
10:31:49.569 [main] WARN  o.s.b.a.o.j.JpaBaseConfiguration$JpaWebConfiguration$JpaWebMvcConfiguration - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
10:31:49.701 [main] WARN  o.s.b.a.t.ThymeleafAutoConfiguration$DefaultTemplateResolverConfiguration - Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
10:31:50.091 [main] INFO  c.p.s.s.a.AutoconfigurationLiveTest - Started AutoconfigurationLiveTest in 4.803 seconds (JVM running for 5.519)
Hibernate: select myuser0_.email as email1_0_0_ from MyUser myuser0_ where myuser0_.email=?
Hibernate: insert into MyUser (email) values (?)
10:31:50.279 [Thread-2] INFO  o.s.s.c.ThreadPoolTaskExecutor - Shutting down ExecutorService 'applicationTaskExecutor'
10:31:50.281 [Thread-2] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
10:31:50.282 [Thread-2] INFO  o.h.t.s.i.SchemaDropperImpl$DelayedDropActionImpl - HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
Hibernate: drop table if exists MyUser

5. 禁用自動(dòng)配置類

如果我們想要從加載中排除自動(dòng)配置,我們可以將帶有exclude或excludeName屬性的@EnableAutoConfiguration注解添加到配置類:

@Configuration
@EnableAutoConfiguration(
  exclude={MySQLAutoconfiguration.class})
public class AutoconfigurationApplication {
    //...
}

禁用特定自動(dòng)配置的另一個(gè)方法是設(shè)置spring.autoconfigure.exclude屬性:

spring.autoconfigure.exclude=com.peterwanghao.samples.springboot.autoconfiguration.MySQLAutoconfiguration

6. 結(jié)論

在本教程中,我們介紹了Spring Boot是如何自動(dòng)加載配置類,以及背后所隱藏的具體實(shí)現(xiàn)。展示了如何創(chuàng)建自定義Spring Boot自動(dòng)配置。

到此這篇關(guān)于SpringBoot四大神器之Auto onfiguration的使用的文章就介紹到這了,更多相關(guān)SpringBoot Auto Configuration內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java求數(shù)組第二大元素示例

    java求數(shù)組第二大元素示例

    這篇文章主要介紹了java求數(shù)組第二大元素示例,需要的朋友可以參考下
    2014-04-04
  • Springboot2.6.x的啟動(dòng)流程與自動(dòng)配置詳解

    Springboot2.6.x的啟動(dòng)流程與自動(dòng)配置詳解

    這篇文章主要給大家介紹了關(guān)于Springboot2.6.x的啟動(dòng)流程與自動(dòng)配置的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • SpringBoot?分模塊開發(fā)的操作方法

    SpringBoot?分模塊開發(fā)的操作方法

    這篇文章主要介紹了SpringBoot?分模塊開發(fā)的操作方法,通過在原項(xiàng)目新增一個(gè)maven模塊,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • mvc架構(gòu)實(shí)現(xiàn)商品的購(gòu)買(二)

    mvc架構(gòu)實(shí)現(xiàn)商品的購(gòu)買(二)

    這篇文章主要為大家詳細(xì)介紹了mvc架構(gòu)實(shí)現(xiàn)商品購(gòu)買功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Mybatis實(shí)現(xiàn)增刪改查

    Mybatis實(shí)現(xiàn)增刪改查

    這篇文章主要介紹了Mybatis實(shí)現(xiàn)增刪改查,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-01-01
  • Java訪問修飾符原理及代碼解析

    Java訪問修飾符原理及代碼解析

    這篇文章主要介紹了Java訪問修飾符原理及代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • JavaWeb項(xiàng)目部署到服務(wù)器詳細(xì)步驟詳解

    JavaWeb項(xiàng)目部署到服務(wù)器詳細(xì)步驟詳解

    這篇文章主要介紹了JavaWeb項(xiàng)目如何部署到服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SpringBoot使用Maven實(shí)現(xiàn)多環(huán)境配置管理

    SpringBoot使用Maven實(shí)現(xiàn)多環(huán)境配置管理

    軟件開發(fā)中經(jīng)常有開發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境,而且一般這些環(huán)境配置會(huì)各不相同,本文主要介紹了SpringBoot使用Maven實(shí)現(xiàn)多環(huán)境配置管理,感興趣的可以了解一下
    2024-01-01
  • springboot實(shí)現(xiàn)單文件和多文件上傳

    springboot實(shí)現(xiàn)單文件和多文件上傳

    這篇文章主要為大家詳細(xì)介紹了springboot實(shí)現(xiàn)單文件和多文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 一文詳解Spring如何控制Bean注入的順序

    一文詳解Spring如何控制Bean注入的順序

    這篇文章主要為大家詳細(xì)介紹Spring如何控制Bean注入的順序,其中續(xù)注意的是在Bean上加@Order(xxx)是無(wú)法控制bean注入的順序的,需要的可以參考一下
    2022-06-06

最新評(píng)論