springboot

springboot-自动装配-@EnableAutoConfi

2020-07-26  本文已影响0人  愤怒的奶牛

上一篇文章我们讨论了@ComponentScan 加载Bean,现在我们来使用springboot 给我们提供好的 @EnableAutoConfiguration 来加载外部化的Bean,先看我们该如何使用 @EnableAutoConfiguration

  1. 为了排除springboot自动扫描加载Bean,我们单独创建一个模块study,并将包路径区分开
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
    <groupId>com.boot</groupId>
    <artifactId>study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

package com.boot.study.demo; // 注意包路径

public class TestDemo {

    public TestDemo() {
        System.out.println(" TestDemo init..........");
    }

    public String hello () {
        return "自动装配成功....";
    }

}

===================
package com.boot.study.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类,创建Bean
 * META-INF/spring.factories 配置指向 TestDemoConfig 实现自动装配
 * org.springframework.boot.autoconfigure.EnableAutoConfiguration
 * =com.boot.study.demo.TestDemoConfig
 */
@Configuration
public class TestDemoConfig {

    @Bean
    public TestDemo testDemo () {
        return new TestDemo();
    }
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.boot.study.demo.TestDemoConfig

我们外部模块创建好了,将安装到本地maven 库 mvn install

我们创建好了 study 模块,现在我们要使用了,enable-demo 模块使用study模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>enable-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>enable-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      <!--使用-->
        <dependency>
            <groupId>com.boot</groupId>
            <artifactId>study</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

package com.example.enabledemo;// 扫描根路径

import com.boot.study.demo.TestDemo;
import com.example.enabledemo.helloworld.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class EnableDemoApplication {

    @Autowired
    private TestDemo testDemo;
    public static void main(String[] args) {
        SpringApplication.run(EnableDemoApplication.class, args);
    }

    @GetMapping
    public String hello () {
        return testDemo.hello();
    }
}

运行 访问http://127.0.0.1:8080 可以看到 ' 自动装配成功....'
说明我们成功的加载了外部模块的Bean,并被spring容器管理起来了。注意两个模块的包路径完全不相同,故意排除 被springboot自动扫描加载Bean的情况。

  1. 创建我们自己的Bean --> TestDemo
  2. 创建一个@Configuration配置文件,用来创建Bean --->TestDemoConfig,注意这里的配置文件只是用来配置Bean 的 和xml 中配置<bean>是一样的道理,可以使用条件@Conditionalxxx 实现按需创建Bean
  3. 在resources 路径下创建 META-INF/spring.factories,并配置如下信息:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.boot.study.demo.TestDemoConfig

k= org.springframework.boot.autoconfigure.EnableAutoConfiguration 就是@EnableAutoConfiguration 注解
v = com.boot.study.demo.TestDemoConfig 创建Bean 的配置类

提醒:在这里我们只是简单的演示了如何使用 springboot 内置的自动装配机制,先明白如何使用,我们在慢慢的探索原理,其中 @EnableAutoConfiguration 只是自动装配机制中的一中实现方式。那么如果我们想自己也搞一个 @Enablexxx 注解,来实现某些功能,该如果做呢?这个就是我们接下来要将的另一种自动装配机制了。

上一篇 下一篇

猜你喜欢

热点阅读