原創 | 我被面試官給虐懵了,竟然是因為我不懂Spring中的@Configuration


GitHub 3.7k Star 的Java工程師成神之路 ,不來了解一下嗎?

GitHub 3.7k Star 的Java工程師成神之路 ,真的不來了解一下嗎?

GitHub 3.7k Star 的Java工程師成神之路 ,真的確定不來了解一下嗎?

現在大部分的Spring項目都采用了基於注解的配置,采用了@Configuration 替換標簽的做法。一行簡單的注解就可以解決很多事情。但是,其實每一個注解背后都有很多值得學習和思考的內容。這些思考的點也是很多大廠面試官喜歡問的內容。

在一次關於Spring注解的面試中,可能會經歷面試官的一段奪命連環問:

@Configuration有什么用?
@Configuration和XML有什么區別?哪種好?
Spring是如何基於來獲取Bean的定義的?
@Autowired 、 @Inject、@Resource 之間有什么區別?
@Value、@PropertySource 和 @Configuration?
Spring如何處理帶@Configuration @Import的類?
@Profile有什么用?
@Configuration 如何嵌套?
Spring如何對Bean進行延遲初始化?
Spring項目怎么進行單元測試?
@Configuration 使用上有哪些約束?

本文就來嘗試回答下以上問題。簡單介紹下@Configuration 注解,並且你看一下他的基本用法以及和其他注解產生化學反應。文章內容較長,建議收藏。

@Configuration 基本說明

定義:指示一個類聲明一個或者多個@Bean 聲明的方法並且由Spring容器統一管理,以便在運行時為這些bean生成bean的定義和服務請求的類。例如:


@Configuration
public class AppConfig {
  
  @Bean
  public MyBean myBean(){
    return new MyBean();
  }
}

上述AppConfig 加入@Configuration 注解,表明這就是一個配置類。有一個myBean()的方法,返回一個MyBean()的實例,並用@Bean 進行注釋,表明這個方法是需要被Spring進行管理的bean。@Bean 如果不指定名稱的話,默認使用myBean名稱,也就是小寫的名稱。

通過注解啟動:

通過啟動一個AnnotationConfigApplicationContext 來引導這個@Configuration 注解的類,比如:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();

在web項目中,也可以使用AnnotationContextWebApplicationContext 或者其他變體來啟動。

新建一個SpringBoot項目(別問我為什么,因為這樣創建項目比較快)。

  • pom.xml 文件如下:
<?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 http://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.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring.configuration</groupId>
    <artifactId>spring-configuration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-configuration</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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
    </dependencies>

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

</project>

  • 在config 包下新建一個MyConfiguration環境配置,和上面的示例代碼相似,完整的代碼如下:
@Configuration
public class MyConfiguration {

    @Bean
    public MyBean myBean(){
        System.out.println("myBean Initialized");
        return new MyBean();
    }
}

說明MyConfiguration 是一個配置類,能夠在此類下面聲明管理多個Bean,我們聲明了一個MyBean 的bean,希望它被容器加載和管理。

  • 在pojo包下新建一個MyBean的類,具體代碼如下
public class MyBean {

    public MyBean(){
        System.out.println("generate MyBean Instance");
    }

    public void init(){
        System.out.println("MyBean Resources Initialized");
    }
}

  • 新建一個SpringConfigurationApplication類,用來測試MyConfiguration類,具體代碼如下:
public class SpringConfigurationApplication {

    public static void main(String[] args) {
        
//        AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class)
      	// 因為我們加載的@Configuration 是基於注解形式的,所以需要創建AnnotationConfigApplicationContext
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注冊MyConfiguration 類並刷新bean 容器。
        context.register(MyConfiguration.class);
        context.refresh();

    }

}

輸出:

myBean Initialized
generate MyBean Instance

從輸出的結果可以看到,默認名稱為myBean 的bean隨着容器的加載而加載,因為myBean方法返回一個myBean的構造方法,所以myBean被初始化了。

通過XML 的方式來啟動

  • 可以通過使用XML方式定義的<context:annotation-config />開啟基於注解的啟動,然后再定義一個MyConfiguration的bean,在/resources 目錄下新建 application-context.xml 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>

    <!-- 相當於基於注解的啟動類 AnnotationConfigApplicationContext-->
    <context:annotation-config />

    <bean class="com.spring.configuration.config.MyConfiguration"/>

</beans>
  • 需要引入applicationContext.xml ,在SpringConfigurationApplication 需要進行引入,修改后的SpringConfigurationApplication如下:
public class SpringConfigurationApplication {

    public static void main(String[] args) {

        ApplicationContext context = 
          new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

輸出:

myBean Initialized
generate MyBean Instance

基於ComponentScan() 來獲取Bean的定義

@Configuration 使用@Component 進行原注解,因此@Configuration 類也可以被組件掃描到(特別是使用XML context:component-scan 元素)。

在這里認識幾個注解: @Controller, @Service, @Repository, @Component

  • @Controller: 表明一個注解的類是一個"Controller",也就是控制器,可以把它理解為MVC 模式的Controller 這個角色。這個注解是一個特殊的@Component,允許實現類通過類路徑的掃描掃描到。它通常與@RequestMapping 注解一起使用。

  • @Service: 表明這個帶注解的類是一個"Service",也就是服務層,可以把它理解為MVC 模式中的Service層這個角色,這個注解也是一個特殊的@Component,允許實現類通過類路徑的掃描掃描到

  • @Repository: 表明這個注解的類是一個"Repository",團隊實現了JavaEE 模式中像是作為"Data Access Object" 可能作為DAO來使用,當與 PersistenceExceptionTranslationPostProcessor 結合使用時,這樣注釋的類有資格獲得Spring轉換的目的。這個注解也是@Component 的一個特殊實現,允許實現類能夠被自動掃描到

  • @Component: 表明這個注釋的類是一個組件,當使用基於注釋的配置和類路徑掃描時,這些類被視為自動檢測的候選者。

也就是說,上面四個注解標記的類都能夠通過@ComponentScan 掃描到,上面四個注解最大的區別就是使用的場景和語義不一樣,比如你定義一個Service類想要被Spring進行管理,你應該把它定義為@Service 而不是@Controller因為我們從語義上講,@Service更像是一個服務的類,而不是一個控制器的類,@Component通常被稱作組件,它可以標注任何你沒有嚴格予以說明的類,比如說是一個配置類,它不屬於MVC模式的任何一層,這個時候你更習慣於把它定義為 @Component。@Controller,@Service,@Repository 的注解上都有@Component,所以這三個注解都可以用@Component進行替換。

來看一下代碼進行理解:

  • 定義五個類,類上分別用@Controller, @Service, @Repository, @Component, @Configuration 進行標注,分別如下
@Component
public class UserBean {}

@Configuration
public class UserConfiguration {}

@Controller
public class UserController {}

@Repository
public class UserDao {}

@Service
public class UserService {}
  • MyConfiguration上加上@ComponentScan 注解,掃描上面5個類所在的包位置。代碼如下:
@Configuration
@ComponentScan(basePackages = "com.spring.configuration.pojo")
public class MyConfiguration {

    @Bean
    public MyBean myBean(){
        System.out.println("myBean Initialized");
        return new MyBean();
    }
}
  • 修改 SpringConfigurationApplication 中的代碼,如下:
public class SpringConfigurationApplication {

    public static void main(String[] args) {

//        AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class)
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(MyConfiguration.class);
        context.refresh();

        // 獲取啟動過程中的bean 定義的名稱
        for(String str : context.getBeanDefinitionNames()){
            System.out.println("str = " + str);
        }
        context.close();

    }
}

輸出:

myBean Initialized
generate MyBean Instance
str = org.springframework.context.annotation.internalConfigurationAnnotationProcessor
str = org.springframework.context.annotation.internalAutowiredAnnotationProcessor
str = org.springframework.context.annotation.internalRequiredAnnotationProcessor
str = org.springframework.context.annotation.internalCommonAnnotationProcessor
str = org.springframework.context.event.internalEventListenerProcessor
str = org.springframework.context.event.internalEventListenerFactory
str = myConfiguration
str = userBean
str = userConfiguration
str = userController
str = userDao
str = userService
str = myBean

由輸出可以清楚的看到,上述定義的五個類成功被@ComponentScan 掃描到,並在程序啟動的時候進行加載。

@Configuration 和 Environment

​ @Configuration 通常和Environment 一起使用,通過@Environment 解析的屬性駐留在一個或多個"屬性源"對象中,@Configuration類可以使用@PropertySource,像Environment 對象提供屬性源

  • 為了便於測試,我們引入junit4和spring-test 的依賴,完整的配置文件如下
<?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 http://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.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring.configuration</groupId>
    <artifactId>spring-configuration</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-configuration</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring.version>5.0.6.RELEASE</spring.version>
        <spring.test.version>4.3.13.RELEASE</spring.test.version>
        <junit.version>4.12</junit.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.test.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
    </dependencies>

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

</project>
  • 在config 包下定義一個 EnvironmentConfig 類,注入Environment 屬性,完整代碼如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EnvironmentConfig.class)
@Configuration
@PropertySource("classpath:beanName.properties")
public class EnvironmentConfig {

    @Autowired
    Environment env;

    @Test
    public void testReadProperty(){
        // 獲取bean.name.controller 的屬性
        System.out.println(env.getProperty("bean.name.controller"));
        // 判斷是否包含bean.name.component
        System.out.println(env.containsProperty("bean.name.component"));
        // 返回與給定鍵關聯的屬性值
        System.out.println(env.getRequiredProperty("bean.name.service"));
    }
}
  • 在/resources 目錄下新建beanName.properties 文件,如下:
bean.name.configuration=beanNameConfiguration
bean.name.controller=beanNameController
bean.name.service=beanNameService
bean.name.component=beanNameComponent
bean.name.repository=beanNameRepository

啟動並進行Junit測試,輸出如下:

…..…...

beanNameController

true

beanNameService

…..…...

@Autowired 、 @Inject、@Resource 的區別

@Inject: 這是jsr330 的規范,通過AutowiredAnnotationBeanPostProcessor 類實現的依賴注入。位於javax.inject包內,是Java自帶的注解。

@Inject
@Named("environment")
Environment env;

不加@Named注解,需要配置與變量名一致即可。

@Autowired: @Autowired 是Spring提供的注解,通過AutowiredAnnotationBeanPostProessor 類實現注入。位於org.springframework.beans.factory.annotation 包內,是Spring 中的注解

@Autowired
Environment env;

默認是通過byType 實現注入

@Resource: @Resource 是jsr250規范的實現,@Resource通過CommonAnnotationBeanPostProcessor 類實現注入。@Resource 一般會指定一個name屬性,如下:

@Resource(name = "environment")
Environment env;

默認是通過byName 實現注入

區別:

@Autowired和@Inject基本是一樣的,因為兩者都是使用AutowiredAnnotationBeanPostProcessor來處理依賴注入。但是@Resource是個例外,它使用的是CommonAnnotationBeanPostProcessor來處理依賴注入。當然,兩者都是BeanPostProcessor。

在介紹完上述三者的區別之后,可以對Environment的屬性以上述注入方式進行改造

@Value、@PropertySource 和 @Configuration

@Configuration 可以和@Value 和@PropertySource 一起使用讀取外部配置文件,具體用法如下:

  • 在config 包下新建一個ReadValueFromPropertySource類,代碼如下
@PropertySource("classpath:beanName.properties")
@Configuration
public class ReadValueFromPropertySource {

    @Value("bean.name.component")
    String beanName;

    @Bean("myTestBean")
    public MyBean myBean(){
        return new MyBean(beanName);
    }

}

通過@PropertySource引入的配置文件,使@Value 能夠獲取到屬性值,在給myBean()方法指定了一個名稱叫做myTestBean。

  • 修改MyBean類,增加一個name屬性和一個構造器,再生成其toString() 方法
public class MyBean {

    String name;

    public MyBean(String name) {
        this.name = name;
    }

    public MyBean(){
        System.out.println("generate MyBean Instance");
    }

    public void init(){
        System.out.println("MyBean Resources Initialized");
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "name='" + name + '\'' +
                '}';
    }
}
  • 在SpringConfigurationApplication中進行測試,如下
public class SpringConfigurationApplication {

    public static void main(String[] args) {

      // 為了展示配置文件的完整性,之前的代碼沒有刪除。
//        AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class)
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.register(MyConfiguration.class);
//        context.refresh();
//
//        // 獲取啟動過程中的bean 定義的名稱
//        for(String str : context.getBeanDefinitionNames()){
//            System.out.println("str = " + str);
//        }
//        context.close();

        ApplicationContext context =
                new AnnotationConfigApplicationContext(ReadValueFromPropertySource.class);
        MyBean myBean = (MyBean) context.getBean("myTestBean");
        System.out.println("myBean = " + myBean);

    }
}

使用Applicatio@InConntext 就能夠獲取myTestBean 這個bean,再生成myBean的實例。

輸出:myBean = MyBean{name='bean.name.component'}

@Import 和 @Configuration

@Import的定義(來自於JavaDoc):表明一個或者多個配置類需要導入,提供與Spring XML中 相等的功能,允許導入@Configuration 、@ImportSelector、@ImportBeanDefinitionRegistar的實現,以及常規組件類似於AnnotationConfigApplicationContext。可能用於類級別或者是原注解。如果XML或者其他非@Configuration標記的Bean資源需要被導入的話,使用@ImportResource。下面是一個示例代碼:

  • 在pojo 包下新建兩個配置類,分別是CustomerBo, SchedualBo
@Configuration
public class CustomerBo {

    public void printMsg(String msg){
        System.out.println("CustomerBo : " + msg);
    }

    @Bean
    public CustomerBo testCustomerBo(){
        return new CustomerBo();
    }
}

@Configuration
public class SchedulerBo {

    public void printMsg(String msg){
        System.out.println("SchedulerBo : " + msg);
    }

    @Bean
    public SchedulerBo testSchedulerBo(){
        return new SchedulerBo();
    }
}
  • 在config 包下新建一個AppConfig,導入CustomerBo 和 SchedulerBo 。
@Configuration
@Import(value = {CustomerBo.class,SchedulerBo.class})
public class AppConfig {}
  • 在config 包下新建一個ImportWithConfiguration ,用於測試@Import 和 @Configuration 的使用
public class ImportWithConfiguration {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        CustomerBo customerBo = (CustomerBo) context.getBean("testCustomerBo");
        customerBo.printMsg("System out println('get from customerBo')");

        SchedulerBo schedulerBo = (SchedulerBo) context.getBean("testSchedulerBo");
        schedulerBo.printMsg("System out println('get from schedulerBo')");
    }
}

輸出:

CustomerBo : System out println('get from customerBo')
SchedulerBo : System out println('get from schedulerBo')

@Profile

@Profile: 表示當一個或多個@Value 指定的配置文件處於可用狀態時,組件符合注冊條件,可以進行注冊。

三種設置方式:

  • 可以通過ConfigurableEnvironment.setActiveProfiles()以編程的方式激活

  • 可以通過AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME (spring.profiles.active )屬性設置為

    JVM屬性

  • 作為環境變量,或作為web.xml 應用程序的Servlet 上下文參數。也可以通過@ActiveProfiles 注解在集成測試中以聲明方式激活配置文件。

作用域

  • 作為類級別的注釋在任意類或者直接與@Component 進行關聯,包括@Configuration 類

  • 作為原注解,可以自定義注解

  • 作為方法的注解作用在任何方法

注意:

​ 如果一個配置類使用了Profile 標簽或者@Profile 作用在任何類中都必須進行啟用才會生效,如果@Profile({"p1","!p2"}) 標識兩個屬性,那么p1 是啟用狀態 而p2 是非啟用狀態的。

@ImportResource 和 @Configuration

@ImportResource: 這個注解提供了與@Import 功能相似作用,通常與@Configuration 一起使用,通過AnnotationConfigApplicationContext 進行啟動,下面以一個示例來看一下具體用法:

  • 在config下新建TestService 類,聲明一個構造函數,類初始化時調用
public class TestService {

    public TestService(){
        System.out.println("test @importResource success");
    }
}
  • 在/resources 目錄下新建 importResources.xml ,為了導入TestService
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>

    <bean id = "testService" class="com.spring.configuration.config.TestService" />

</beans>
  • 然后在config 下新建一個ImportResourceWithConfiguration, 用於讀取配置文件
@Configuration
@ImportResource("classpath:importResources.xml")
public class ImportResourceWithConfiguration {

    @Autowired
    private TestService service;

    public void getImportResource(){
        new TestService();
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(ImportResourceWithConfiguration.class);
        context.getBean("testService");

    }
}

輸出:test @importResource success

@Configuration 嵌套

@Configuration注解作用在類上,就和普通類一樣能夠進行相互嵌套,定義內部類。

// 來自JavaDoc
@Configuration
public class AppConfig{
  
  @Inject
  DataSource dataSource;
  
  @Bean
  public MyBean myBean(){
    return new MyBean(dataSource);
  }
  
  @Configuration
  static class DataConfig(){
    @Bean
    DataSource dataSource(){
      return new EmbeddedDatabaseBuilder().build()
    }
  }
}

在上述代碼中,只需要在應用程序的上下文中注冊 AppConfig 。由於是嵌套的@Configuration 類,DatabaseConfig 將自動注冊。當AppConfig 、DatabaseConfig 之間的關系已經隱含清楚時,這就避免了使用@Import 注解的需要。

@Lazy 延遲初始化

@Lazy : 表明一個bean 是否延遲加載,可以作用在方法上,表示這個方法被延遲加載;可以作用在@Component (或者由@Component 作為原注解) 注釋的類上,表明這個類中所有的bean 都被延遲加載。如果沒有@Lazy注釋,或者@Lazy 被設置為false,那么該bean 就會急切渴望被加載;除了上面兩種作用域,@Lazy 還可以作用在@Autowired和@Inject注釋的屬性上,在這種情況下,它將為該字段創建一個惰性代理,作為使用ObjectFactory或Provider的默認方法。下面來演示一下:

  • 修改MyConfiguration類,在該類上添加@Lazy 注解,新增一個IfLazyInit()方法,檢驗是否被初始化。
@Lazy
@Configuration
@ComponentScan(basePackages = "com.spring.configuration.pojo")
public class MyConfiguration {

    @Bean
    public MyBean myBean(){
        System.out.println("myBean Initialized");
        return new MyBean();
    }

    @Bean
    public MyBean IfLazyInit(){
        System.out.println("initialized");
        return new MyBean();
    }
}
  • 修改SpringConfigurationApplication 啟動類,放開之前MyConfiguration 的啟動類
public class SpringConfigurationApplication {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//        context.register(MyConfiguration.class);
//        context.refresh();
//
//        // 獲取啟動過程中的bean 定義的名稱
        for(String str : context.getBeanDefinitionNames()){
            System.out.println("str = " + str);
        }
//        context.close();

//        ApplicationContext context =
//                new AnnotationConfigApplicationContext(ReadValueFromPropertySource.class);
//        MyBean myBean = (MyBean) context.getBean("myTestBean");
//        System.out.println("myBean = " + myBean);

    }
}

輸出你會發現沒有關於bean的定義信息,但是當吧@Lazy 注釋拿掉,你會發現輸出了關於bean的初始化信息:

myBean Initialized
generate MyBean Instance
initialized
generate MyBean Instance

@RunWith 和 @ContextConfiguration

Junit4 測試類,用於注解在類上表示通過Junit4 進行測試,可以省略編寫啟動類代碼,是ApplicationContext 等啟動類的替換。一般用@RunWith 和 @Configuration 進行單元測試,這是軟件開發過程中非常必要而且具有專業性的一部分,上面EnvironmentConfig 類證實了這一點:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EnvironmentConfig.class)
@Configuration
@PropertySource("classpath:beanName.properties")
public class EnvironmentConfig {

//    @Autowired
//    Environment env;

    @Inject
    Environment env;

    @Test
    public void testReadProperty(){
        // 獲取bean.name.controller 的屬性
        System.out.println(env.getProperty("bean.name.controller"));
        // 判斷是否包含bean.name.component
        System.out.println(env.containsProperty("bean.name.component"));
        // 返回與給定鍵關聯的屬性值
        System.out.println(env.getRequiredProperty("bean.name.service"));
    }
}

@Enable 啟動Spring內置功能

詳情查閱@EnableAsync,@EnableScheduling,@EnableTransactionManagement,@EnableAspectJAutoProxy,@EnableWebMvc官方文檔

@Configuration 使用約束

  • 必須以類的方式提供(即不是從工廠方法返回的實例)
  • @Configuration 注解的類必須是非final的
  • 配置類必須是非本地的(即可能不在方法中聲明),native 標注的方法
  • 任何嵌套的@Configuration 都必須是static 的。
  • @Bean 方法可能不會反過來創建更多配置類

參考資料:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

https://blog.csdn.net/u012734441/article/details/51706504


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM