Spring註解解析之@ImportResource

一、ImportResource

1.1 定義包和類

首先定義一個不會被ComponentScan掃描到的包outpackage,如下:

在這裡插入圖片描述

在該包內創建一個類:

package outpackage;

import org.springframework.stereotype.Service;

@Service
public class HelloService1 {
    public void method1() {
        System.out.println("class:HelloService1__method:method1");
    }
}

1.2 定義配置文件

在資源目錄添加配置文件applicationContext.xml

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 開啟註解模式 -->
    <context:annotation-config/>

    <!-- 基於註解自動註冊spring bean -->
    <context:component-scan base-package="outpackage"/>
</beans>

1.3 定義Java Config類

在啟動類平級目錄或者是子目錄添加java config類保證能夠被springboot掃描到,引入xml配置,如下:

package dongshi.daddy;

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

@Configuration
@ImportResource({"classpath:applicationContext.xml"})
public class OuterPackageConfiguration {
}

1.4 測試代碼

@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) throws URISyntaxException, IOException {
        ConfigurableApplicationContext run = SpringApplication.run(HelloWorldMainApplication.class, args);
        // 獲取通過配置文件定義而被掃描到的類
        HelloService1 bean = run.getBean(HelloService1.class);
        System.out.println(bean);
    }
}

二、運行

2021-05-19 17:52:52.896  INFO 16232 — [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path ”
…snip…
outpackage.HelloService1@1929425f

為瞭證明確實是xml配置文件在起作用,而不是springboot自己掃描註冊,修改配置類,註釋掉@ImportResource({"classpath:applicationContext.xml"}),如下:

package dongshi.daddy;

import org.springframework.context.annotation.Configuration;

@Configuration
//@ImportResource({"classpath:applicationContext.xml"})
public class OuterPackageConfiguration {
}

然後運行:

2021-05-19 18:01:10.522  INFO 18260 — [           main] dongshi.daddy.HelloWorldMainApplication  : Started HelloWorldMainApplication in 0.944 seconds (JVM running for 1.355)
Exception in thread “main” org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘outpackage.HelloService1’ available
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
 at dongshi.daddy.HelloWorldMainApplication.main(HelloWorldMainApplication.java:16)

可以看到就找不到對應的bean瞭。

到此這篇關於Spring註解解析之@ImportResource的文章就介紹到這瞭,更多相關@ImportResource註解內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: