成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

springboot(二)——springboot自動(dòng)配置解析

張率功 / 3634人閱讀

摘要:前言用過(guò)的肯定很熟悉,它其中有個(gè)重要的特性,就是自動(dòng)配置平時(shí)習(xí)慣的一些設(shè)置的配置作為默認(rèn)配置。提倡無(wú)配置文件的理念,使用生成的應(yīng)用完全不會(huì)生成任何配置代碼與配置文件。

前言

用過(guò)springboot的肯定很熟悉,它其中有個(gè)重要的特性,就是自動(dòng)配置(平時(shí)習(xí)慣的一些設(shè)置的配置作為默認(rèn)配置)。springboot提倡無(wú)XML配置文件的理念,使用springboot生成的應(yīng)用完全不會(huì)生成任何配置代碼與XML配置文件。下面先看一個(gè)springboot集成mybatis的例子。
第一步: 引入pom文件

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
        
            mysql
            mysql-connector-java
            5.1.47
        

第二步: 因?yàn)槲沂褂玫膞ml配置文件去使用mybatis,在application.properties文件加入如下配置:

#指定mapper文件位置
mybatis.mapper-locations=classpath:mapper/*.xml

#數(shù)據(jù)源信息
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zplxjj?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

第三步: 加入實(shí)體類、dao、mapper文件

第四步:啟動(dòng)類上面加入注解

@SpringBootApplication
@MapperScan("com.stone.zplxjj.dao")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

第五步:至此,配置完成,只需要寫個(gè)單側(cè),springboot已經(jīng)完美集成mybatis

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
    @Autowired
    UserMapper userMapper;

    @Test
    public void testMybatis() {
        System.out.println(userMapper.selectByPrimaryKey(1L));
    }
}
@EnableAutoConfiguration

通過(guò)上面的例子,我們發(fā)現(xiàn)集成mybatis特別簡(jiǎn)單,那些繁瑣的類的注入都沒(méi)有寫,只需要加入數(shù)據(jù)庫(kù)的一些配置即可,那這其中@EnableAutoConfiguration功不可沒(méi)。@EnableAutoConfiguration 注解已經(jīng)在@SpringBootApplication里面了

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    Class[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class[] scanBasePackageClasses() default {};
}

我們看到@EnableAutoConfiguration結(jié)構(gòu)如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class[] exclude() default {};

    String[] excludeName() default {};
}

這其中起作用的一個(gè)重要注解@Import,這個(gè)Spring提供的一個(gè)注解,可以導(dǎo)入配置類或者Bean到當(dāng)前類中,我們進(jìn)入到AutoConfigurationImportSelector類中查看,方法太長(zhǎng),截取核心的兩個(gè)方法:

    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        } else {
            AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
            AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);
            return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
        }
    }

    protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {
        if (!this.isEnabled(annotationMetadata)) {
            return EMPTY_ENTRY;
        } else {
            AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
            List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
            configurations = this.removeDuplicates(configurations);
            Set exclusions = this.getExclusions(annotationMetadata, attributes);
            this.checkExcludedClasses(configurations, exclusions);
            configurations.removeAll(exclusions);
            configurations = this.filter(configurations, autoConfigurationMetadata);
            this.fireAutoConfigurationImportEvents(configurations, exclusions);
            return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
        }
    }

通過(guò)項(xiàng)目啟動(dòng)后,打上注解,可以看到MybatisAutoConfiguration引入了進(jìn)來(lái)

而MybatisAutoConfiguration能引入進(jìn)來(lái),其實(shí)是在mybatis-spring-boot-autoconfigure-2.0.1.jar包里面的spring.factories指定的,通過(guò)調(diào)用SpringFactoriesLoader.loadFactoryNames()來(lái)掃描加載含有META-INF/spring.factories文件的jar包,從而標(biāo)識(shí)哪些自動(dòng)配置的類

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
@Conditional

@Conditional 的作用,可以根據(jù)條件去加載特定的bean,原理這邊不做探討,springboot基于此實(shí)現(xiàn)了幾個(gè)注解,比較方便的實(shí)現(xiàn)條件加載類
@ConditionalOnBean:Spring容器中是否存在對(duì)應(yīng)的實(shí)例
@ConditionalOnMissingBean:Spring容器中是否缺少對(duì)應(yīng)的實(shí)例
通過(guò)查看MybatisAutoConfiguration中的SqlSessionFactory的寫法

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }

        this.applyConfiguration(factory);
        if (this.properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
        }

        if (!ObjectUtils.isEmpty(this.interceptors)) {
            factory.setPlugins(this.interceptors);
        }

        if (this.databaseIdProvider != null) {
            factory.setDatabaseIdProvider(this.databaseIdProvider);
        }

        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }

        if (this.properties.getTypeAliasesSuperType() != null) {
            factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
        }

        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }

        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            factory.setMapperLocations(this.properties.resolveMapperLocations());
        }

        return factory.getObject();
    }
結(jié)語(yǔ)

通過(guò)上面分析mybatis如何集成springboot,知道了springboot入口在哪里以及如何實(shí)現(xiàn)的自動(dòng)配置,這里只是簡(jiǎn)單的做了介紹,其中的一些源碼和細(xì)節(jié)就沒(méi)有分析了,我相信,入口知道了,接下來(lái)就好摳細(xì)節(jié)了。

本人也開(kāi)通了微信公眾號(hào):stonezplxjj和個(gè)人博客:http://www.zplxjj.com,更多文章歡迎關(guān)注公眾號(hào):

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/74253.html

相關(guān)文章

  • SpringBoot關(guān)于JSON交互問(wèn)題

    摘要:關(guān)于交互問(wèn)題一交互的優(yōu)勢(shì)本來(lái)就是里的內(nèi)容客戶端可以很容易對(duì)數(shù)據(jù)解析數(shù)據(jù)格式簡(jiǎn)單易于讀寫帶寬占用小不錯(cuò)的可讀性可表示各類復(fù)雜性的數(shù)據(jù)。注解相當(dāng)于合在一起的作用。從上面返回結(jié)果可以發(fā)現(xiàn)兩個(gè)問(wèn)題,第一許多為的字段也輸出。 SpringBoot關(guān)于JSON交互問(wèn)題 一、Json交互的優(yōu)勢(shì) 1.JSON本來(lái)就是javascript里的內(nèi)容,客戶端可以很容易對(duì)JSON數(shù)據(jù)解析. 2.數(shù)據(jù)格式簡(jiǎn)單...

    Dogee 評(píng)論0 收藏0
  • SpringBoot就是這么簡(jiǎn)單

    摘要:熱加載代表的是我們不需要重啟服務(wù)器,就能夠類檢測(cè)得到,重新生成類的字節(jié)碼文件無(wú)論是熱部署或者是熱加載都是基于類加載器來(lái)完成的。驗(yàn)證階段字節(jié)碼文件不會(huì)對(duì)造成危害準(zhǔn)備階段是會(huì)賦初始值,并不是程序中的值。 一、SpringBoot入門 今天在慕課網(wǎng)中看見(jiàn)了Spring Boot這么一個(gè)教程,這個(gè)Spring Boot作為JavaWeb的學(xué)習(xí)者肯定至少會(huì)聽(tīng)過(guò),但我是不知道他是什么玩意。 只是大...

    whinc 評(píng)論0 收藏0
  • springboot學(xué)習(xí)()——springmvc配置使用

    摘要:中添加攔截器配置如下攔截所有請(qǐng)求,也就是,只攔截開(kāi)頭的請(qǐng)求。在中并沒(méi)有提供配置文件的方式來(lái)配置攔截器,因此需要使用的代碼式配置,配置如下這個(gè)屬性通常并不需要手動(dòng)配置,高版本的會(huì)自動(dòng)檢測(cè)第四點(diǎn)講下靜態(tài)資源映射。 以下內(nèi)容,如有問(wèn)題,煩請(qǐng)指出,謝謝 上一篇講解了springboot的helloworld部分,這一篇開(kāi)始講解如何使用springboot進(jìn)行實(shí)際的應(yīng)用開(kāi)發(fā),基本上尋著sprin...

    hiyayiji 評(píng)論0 收藏0
  • 基于SpringBoot的后臺(tái)管理系統(tǒng)(啟動(dòng)類解析,開(kāi)源的世界真好)(一)

    摘要:目前只是一個(gè)后臺(tái)模塊,希望自己技能增強(qiáng)到一定時(shí),可以把的融合進(jìn)來(lái)。目錄第一站,分析了啟動(dòng)類??匆?jiàn)沒(méi),這個(gè)也是配置類,它聲明了視圖解析器地域解析器以及靜態(tài)資源的位置,想起來(lái)沒(méi),就是前置,后置。程序啟動(dòng)類我們點(diǎn)擊源碼看看。 Guns基于SpringBoot,致力于做更簡(jiǎn)潔的后臺(tái)管理系統(tǒng),完美整合springmvc + shiro + 分頁(yè)插件PageHelper + 通用Mapper + ...

    SwordFly 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<