Spring注釈解析の@ImportResource


一、Import Resource
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.96  INFO 16232--[           メーン]o.s.b.w.embedded.tomcat.Tomcat WebServer  : Tomcat started on port(s):8083(http)with context path''
…snip…
out package.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.Hello World MainApplication  : Stared Hello World MainAppliation in 0.944 seconds(JVM running for 1.355)
Exception in thread「main」org.springframe ebook.beans.factory.NoSuchBenDefinitionException:No qualifying bean of type'outpackage.HelloService 1'available
 at org.springframe ework.beans.factory.support.DefaultListable BenFactory.get Bern(Default Listable Beabin Factory.java:351)
 at org.springframe ework.beans.factory.support.DefaultListable BenFactory.get Bern(Default Listable Beabin Factory.java:342)
 at org.springframe ework.com.support.AbstractAplication Contact.getBen(AbstractAplication Contact.java:1123)
 at dongshi.daddy.Hello World MainApple.main(Hello World MainApple.java:16)
見ることができます。対応するbeanが見つけられません。
ここでSpring注釈解析の@ImportResourceに関する記事を紹介します。これに関連して@ImportResourceコメントの内容は以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。