1. 程式人生 > >Spring中重要的註解

Spring中重要的註解

現在大部分的Spring專案都會用到註解。使用註解來替換xml,一行簡單的註解就可以解決很多事情。但是你真的懂其中的原理嗎。

本文翻譯於 https://docs.spring.io/spring-framework/docs and https://docs.spring.io/spring-framework/docs

在面試的時候 多少會問道 你瞭解過Spring註解嗎。

先來談談@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.4.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,希望它被容器載入和管理。

  • 新建一個 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 的方式來啟動

  • 可以通過使用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 (掃包範圍)

@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.4.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註解,需要配置與變數名一致即可。

@Inject
@Named("mongo")
private Mongo mongo;

@Autowired: @Autowired是Spring提供的註解,通過AutowiredAnnotationBeanPostProcessor類實現的依賴注入,與@inject二者具有可互換性。位於org.springframework.beans.factory.annotation 包內,是Spring 中的註解

@Autowired預設是按照byType進行注入的,但是當byType方式找到了多個符合的bean,又是怎麼處理的?Autowired預設先按byType,如果發現找到多個bean,則又按照byName方式比對,如果還有多個,則報出異常。

public class TestServiceImpl {
// 下面兩種@Autowired只要使用一種即可
@Autowired
private UserDao userDao; // 用於欄位上
 
@Autowired
public void setUserDao(UserDao userDao) { // 用於屬性的方法上
this.userDao = userDao;
}
}

@Autowired 註解是按照型別(byType)裝配依賴物件,預設情況下它要求依賴物件必須存在,如果允許null值,可以設定它的required屬性為false。如果我們想使用按照名稱(byName)來裝配,可以結合@Qualifier註解一起使用。如下:

public class TestServiceImpl {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
}

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

@Resource預設按照ByName自動注入,由J2EE提供,需要匯入包javax.annotation.Resource。@Resource有兩個重要的屬性:name和type,而Spring將@Resource註解的name屬性解析為bean的名字,而type屬性則解析為bean的型別。所以,如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不制定name也不制定type屬性,這時將通過反射機制使用byName自動注入策略。

public class TestServiceImpl {
// 下面兩種@Resource只要使用一種即可
@Resource(name="userDao")
private UserDao userDao; // 用於欄位上
 
@Resource(name="userDao")
public void setUserDao(UserDao userDao) { // 用於屬性的setter方法上
this.userDao = userDao;
}
}

注:

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

@Autowired@Inject預設autowired by type,可以通過@Qualifier顯式指定autowired by qualifier name。

@Resource預設autowired by field name,如果autowired by field name失敗,會退化為autowired by type,可以通過@Qualifier顯式指定autowired by qualifier name,如果autowired by qualifier name失敗,會退化為autowired by field name。但是這時候如果autowired by field name失敗,就不會再退化為autowired by type。

@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 + '\'' +
                '}';
    }
}

通過@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 方法可能不會反過來建立更多配置類