Spring注釈解析の@ImportResource
4037 ワード
一、Import Resource
1.1パッケージとクラスを定義する
まず、
このパッケージにクラスを作成します。
リソースディレクトリにプロファイルを追加する
開始クラスフラットディレクトリまたはサブディレクトリにjava configクラス
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が自分でスキャンして登録するのではなく、構成類を修正し、
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コメントの内容は以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。
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コメントの内容は以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。