最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot關(guān)于@Configuration的用法解讀

 更新時間:2026年04月02日 08:50:46   作者:白話機(jī)器學(xué)習(xí)  
本文總結(jié)了Spring框架中@configuration注解的用法,包括基本說明、使用場景、與@Component、@Profile等注解的結(jié)合使用、@import與@importresource的區(qū)別、@lazy的作用等,通過示例代碼詳細(xì)介紹了如何使用@Configuration進(jìn)行基于注解的配置

@Configuration 全部用法

現(xiàn)在大部分的項(xiàng)目都采用了基于注解的配置,采用了@Configuration 替換 標(biāo)簽的做法。但是最近在翻看Spring 官方文檔時,發(fā)現(xiàn)@Configuration 聲明為基礎(chǔ)標(biāo)簽之外,還和大量的其他注解產(chǎn)生化學(xué)反應(yīng)。

@Configuration 基本說明

? **定義:指示一個類聲明一個或者多個@Bean 聲明的方法并且由Spring容器統(tǒng)一管理,以便在運(yùn)行時為這些bean生成bean的定義和服務(wù)請求的類。**例如:

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

上述AppConfig 加入@Configuration 注解,表明這就是一個配置類。

有一個myBean()的方法,返回一個MyBean()的實(shí)例,并用@Bean 進(jìn)行注釋,表明這個方法是需要被Spring進(jìn)行管理的bean。

@Bean 如果不指定名稱的話,默認(rèn)使用myBean名稱,也就是小寫的名稱。

通過注解啟動

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

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

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

新建一個SpringBoot項(xiàng)目(別問我為什么,因?yàn)檫@樣創(chuàng)建項(xiàng)目比較快)。

  • 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環(huán)境配置,和上面的示例代碼相似,完整的代碼如下:
@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)
      	// 因?yàn)槲覀兗虞d的@Configuration 是基于注解形式的,所以需要創(chuàng)建AnnotationConfigApplicationContext
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注冊MyConfiguration 類并刷新bean 容器。
        context.register(MyConfiguration.class);
        context.refresh();

    }

}

輸出:

myBean Initialized
generate MyBean Instance

從輸出的結(jié)果可以看到,默認(rèn)名稱為myBean 的bean隨著容器的加載而加載,因?yàn)?strong>myBean方法返回一個myBean的構(gòu)造方法,所以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"
>
    <!-- 相當(dāng)于基于注解的啟動類 AnnotationConfigApplicationContext-->
    <context:annotation-config />
    <bean class="com.spring.configuration.config.MyConfiguration"/>
</beans>
  • 需要引入applicationContext.xml ,在SpringConfigurationApplication 需要進(jìn)行引入,修改后的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 進(jìn)行原注解,因此@Configuration 類也可以被組件掃描到(特別是使用XML context:component-scan 元素)。

在這里認(rèn)識幾個注解: @Controller, @Service, @Repository, @Component

  • @Controller: 表明一個注解的類是一個"Controller",也就是控制器,可以把它理解為MVC 模式的Controller 這個角色。這個注解是一個特殊的@Component,允許實(shí)現(xiàn)類通過類路徑的掃描掃描到。它通常與@RequestMapping 注解一起使用。
  • @Service: 表明這個帶注解的類是一個"Service",也就是服務(wù)層,可以把它理解為MVC 模式中的Service層這個角色,這個注解也是一個特殊的@Component,允許實(shí)現(xiàn)類通過類路徑的掃描掃描到
  • @Repository: 表明這個注解的類是一個"Repository",團(tuán)隊(duì)實(shí)現(xiàn)了JavaEE 模式中像是作為"Data Access Object" 可能作為DAO來使用,當(dāng)與 PersistenceExceptionTranslationPostProcessor 結(jié)合使用時,這樣注釋的類有資格獲得Spring轉(zhuǎn)換的目的。這個注解也是@Component 的一個特殊實(shí)現(xiàn),允許實(shí)現(xiàn)類能夠被自動掃描到
  • @Component: 表明這個注釋的類是一個組件,當(dāng)使用基于注釋的配置和類路徑掃描時,這些類被視為自動檢測的候選者。

也就是說,上面四個注解標(biāo)記的類都能夠通過@ComponentScan 掃描到,上面四個注解最大的區(qū)別就是使用的場景和語義不一樣,比如你定義一個Service類想要被Spring進(jìn)行管理,你應(yīng)該把它定義為@Service 而不是@Controller因?yàn)槲覀儚恼Z義上講,@Service更像是一個服務(wù)的類,而不是一個控制器的類,@Component通常被稱作組件,它可以標(biāo)注任何你沒有嚴(yán)格予以說明的類,比如說是一個配置類,它不屬于MVC模式的任何一層,這個時候你更習(xí)慣于把它定義為 @Component。@Controller,@Service,@Repository 的注解上都有@Component,所以這三個注解都可以用@Component進(jìn)行替換。

來看一下代碼進(jìn)行理解:

  • 定義五個類,類上分別用@Controller, @Service, @Repository, @Component, @Configuration 進(jìn)行標(biāo)注,分別如下
@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 掃描到,并在程序啟動的時候進(jìn)行加載。

@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"));
        // 返回與給定鍵關(guān)聯(lián)的屬性值
        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

啟動并進(jìn)行Junit測試,輸出如下:

…………

beanNameController

true

beanNameService

…………

@Autowired 、 @Inject、@Resource 的區(qū)別

@Inject: 這是jsr330 的規(guī)范,通過AutowiredAnnotationBeanPostProcessor 類實(shí)現(xiàn)的依賴注入。位于javax.inject包內(nèi),是Java自帶的注解。

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

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

@Autowired: @Autowired 是Spring提供的注解,通過AutowiredAnnotationBeanPostProessor 類實(shí)現(xiàn)注入。位于org.springframework.beans.factory.annotation 包內(nèi),是Spring 中的注解

@Autowired
Environment env;

默認(rèn)是通過byType 實(shí)現(xiàn)注入

@Resource: @Resource 是jsr250規(guī)范的實(shí)現(xiàn),@Resource通過CommonAnnotationBeanPostProcessor 類實(shí)現(xiàn)注入。@Resource 一般會指定一個name屬性,如下:

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

默認(rèn)是通過byName 實(shí)現(xiàn)注入

區(qū)別:

@Autowired和@Inject基本是一樣的,因?yàn)閮烧叨际鞘褂肁utowiredAnnotationBeanPostProcessor來處理依賴注入。但是@Resource是個例外,它使用的是CommonAnnotationBeanPostProcessor來處理依賴注入。當(dāng)然,兩者都是BeanPostProcessor。

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

@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屬性和一個構(gòu)造器,再生成其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中進(jìn)行測試,如下
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的實(shí)例。

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

@Import 和 @Configuration

@Import的定義(來自于JavaDoc):表明一個或者多個配置類需要導(dǎo)入,提供與Spring XML中相等的功能,允許導(dǎo)入@Configuration 、@ImportSelector、@ImportBeanDefinitionRegistar的實(shí)現(xiàn),以及常規(guī)組件類似于AnnotationConfigApplicationContext??赡苡糜陬惣墑e或者是原注解。如果XML或者其他非@Configuration標(biāo)記的Bean資源需要被導(dǎo)入的話,使用@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,導(dǎo)入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: 表示當(dāng)一個或多個@Value 指定的配置文件處于可用狀態(tài)時,組件符合注冊條件,可以進(jìn)行注冊。

三種設(shè)置方式:

  • 可以通過ConfigurableEnvironment.setActiveProfiles()以編程的方式激活
  • 可以通過AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME (spring.profiles.active )屬性設(shè)置為
  • JVM屬性
  • 作為環(huán)境變量,或作為web.xml 應(yīng)用程序的Servlet 上下文參數(shù)。也可以通過@ActiveProfiles 注解在集成測試中以聲明方式激活配置文件。

作用域

  • 作為類級別的注釋在任意類或者直接與@Component 進(jìn)行關(guān)聯(lián),包括@Configuration 類
  • 作為原注解,可以自定義注解
  • 作為方法的注解作用在任何方法

注意

? 如果一個配置類使用了Profile 標(biāo)簽或者@Profile 作用在任何類中都必須進(jìn)行啟用才會生效,如果@Profile({“p1”,“!p2”}) 標(biāo)識兩個屬性,那么p1 是啟用狀態(tài) 而p2 是非啟用狀態(tài)的。

@ImportResource 和 @Configuration

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

  • 在config下新建TestService 類,聲明一個構(gòu)造函數(shù),類初始化時調(diào)用
public class TestService {

    public TestService(){
        System.out.println("test @importResource success");
    }
}
  • 在/resources 目錄下新建 importResources.xml ,為了導(dǎo)入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注解作用在類上,就和普通類一樣能夠進(jìn)行相互嵌套,定義內(nèi)部類。

// 來自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()
    }
  }
}

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

@Lazy 延遲初始化

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

  • 修改MyConfiguration類,在該類上添加@Lazy 注解,新增一個IfLazyInit()方法,檢驗(yàn)是否被初始化。
@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);

    }
}

輸出你會發(fā)現(xiàn)沒有關(guān)于bean的定義信息,但是當(dāng)吧@Lazy 注釋拿掉,你會發(fā)現(xiàn)輸出了關(guān)于bean的初始化信息:

myBean Initialized
generate MyBean Instance
initialized
generate MyBean Instance

@RunWith 和 @ContextConfiguration

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

@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"));
        // 返回與給定鍵關(guān)聯(lián)的屬性值
        System.out.println(env.getRequiredProperty("bean.name.service"));
    }
}

@Enable 啟動Spring內(nèi)置功能

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

@Configuration 使用約束

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

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring jpa和mybatis整合遇到的問題解析

    Spring jpa和mybatis整合遇到的問題解析

    有朋友說jpa相比mybatis太難用,多表聯(lián)合的查詢寫起來也比較費(fèi)勁,所以便加入了mybatis的支持,在配置jpa時遇到各種問題,需要修改相關(guān)配置文件,下面小編給大家分享下修改配置文件的思路,感興趣的朋友參考下
    2016-10-10
  • SpringMVC中的DispatcherServlet初始化流程詳解

    SpringMVC中的DispatcherServlet初始化流程詳解

    這篇文章主要介紹了SpringMVC中的DispatcherServlet初始化流程詳解,DispatcherServlet這個前端控制器是一個Servlet,所以生命周期和普通的Servlet是差不多的,在一個Servlet初始化的時候都會調(diào)用該Servlet的init()方法,需要的朋友可以參考下
    2023-12-12
  • SpringBoot如何正確配置并運(yùn)行Kafka

    SpringBoot如何正確配置并運(yùn)行Kafka

    這篇文章主要介紹了SpringBoot如何正確配置并運(yùn)行Kafka問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 利用Sharding-Jdbc組件實(shí)現(xiàn)分表

    利用Sharding-Jdbc組件實(shí)現(xiàn)分表

    這篇文章主要為大家詳細(xì)介紹了利用Sharding-Jdbc組件實(shí)現(xiàn)分表,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Java用list儲存,遍歷,查詢指定信息過程詳解

    Java用list儲存,遍歷,查詢指定信息過程詳解

    這篇文章主要介紹了Java用list儲存,遍歷,查詢指定信息過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • 淺談Java中的hashcode方法(推薦)

    淺談Java中的hashcode方法(推薦)

    本篇文章主要介紹了Java中的hashcode方法,詳細(xì)的介紹了hashCode方法的作用,具有一定的參考價值,有需要的可以了解一下。
    2016-12-12
  • Spring Boot的單例模式實(shí)現(xiàn)代碼

    Spring Boot的單例模式實(shí)現(xiàn)代碼

    Spring Boot里的單例不是GoF意義上的全JVM 唯一對象 + 私有構(gòu)造器,而是由 Spring 容器管理的每個 ApplicationContext 僅一個實(shí)例,本文介紹Spring Boot的單例模式是如何實(shí)現(xiàn)的,感興趣的朋友跟隨小編一起看看吧
    2026-03-03
  • Spring Cloud 微服務(wù)全棧實(shí)踐指南

    Spring Cloud 微服務(wù)全棧實(shí)踐指南

    本文詳細(xì)介紹了SpringCloud在微服務(wù)架構(gòu)中的核心能力,包括服務(wù)注冊與發(fā)現(xiàn)、配置中心、服務(wù)通信與網(wǎng)關(guān)、穩(wěn)定性工程、可觀測性、數(shù)據(jù)一致性與工程落地,以及云原生的進(jìn)階演進(jìn),適合已有SpringBoot基礎(chǔ)的工程師進(jìn)行參考和實(shí)踐,感興趣的朋友跟隨小編一起看看吧
    2025-11-11
  • java中String.intern()方法功能介紹

    java中String.intern()方法功能介紹

    這篇文章主要介紹了java中String.intern()方法具有什么功能,主要包括String.intern原理,JDK6中String.intern()的相關(guān)知識,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • MybatisPlus使用聚合函數(shù)的示例代碼

    MybatisPlus使用聚合函數(shù)的示例代碼

    本文主要介紹了MybatisPlus使用聚合函數(shù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03

最新評論

屏南县| 常山县| 淮阳县| 平原县| 德惠市| 济源市| 措美县| 盱眙县| 安阳县| 武穴市| 荣昌县| 和平区| 海门市| 镇江市| 宣城市| 日喀则市| 南召县| 肇源县| 绍兴县| 阿城市| 陇川县| 封开县| 临江市| 祁门县| 桦甸市| 大姚县| 长顺县| 明水县| 五原县| 西宁市| 江永县| 沽源县| 广东省| 宁武县| 镇平县| 綦江县| 临洮县| 东台市| 漠河县| 乐陵市| 英超|