<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spring &#8211; 有意与无意之间</title>
	<atom:link href="https://zhangxihai.cn/archives/tag/spring/feed" rel="self" type="application/rss+xml" />
	<link>https://zhangxihai.cn</link>
	<description>千淘万漉虽辛苦 吹尽狂沙始到金 - 生命不息 编程不止</description>
	<lastBuildDate>Thu, 16 May 2024 04:46:20 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.11</generator>
	<item>
		<title>Spring Boot: ImportSelector接口</title>
		<link>https://zhangxihai.cn/archives/342</link>
					<comments>https://zhangxihai.cn/archives/342#respond</comments>
		
		<dc:creator><![CDATA[胖爷]]></dc:creator>
		<pubDate>Sun, 08 Apr 2018 03:50:44 +0000</pubDate>
				<category><![CDATA[编码]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring boot]]></category>
		<guid isPermaLink="false">https://zhangxihai.cn/?p=342</guid>

					<description><![CDATA[在Spring Boot中，ImportSelector是一个允许动态选择和导入配置类的接口，它的主要作用是根据某些条件动态决定哪些配资类需要被导入Spring应用的上下文中。 接口定义 public interface ImportSelector { String[] selectImports(AnnotationMetadata importingC...]]></description>
										<content:encoded><![CDATA[<p>在Spring Boot中，<code>ImportSelector</code>是一个允许动态选择和导入配置类的接口，它的主要作用是根据某些条件动态决定哪些配资类需要被导入Spring应用的上下文中。</p>
<h3>接口定义</h3>
<pre><code>public interface ImportSelector {
    String[] selectImports(AnnotationMetadata importingClassMetadata);
}</code></pre>
<p><strong>参数AnnotationMetadata</strong>：表示使用了<code>@Import</code>注解的类的元数据，方法返回一个字符串数组，每个字符串对应一个需要导入的配置类的全限定名。</p>
<p><strong>作用过程</strong> 当Spring boot启动时，它会扫描到使用了<code>@Import</code>注解的类，<code>ImportSelector</code>会根据传入的元数据决定需要导入的配置类，并将这些类注册到Spring上下文中。</p>
<p><strong>应用场景</strong> </p>
<ol>
<li>当基于条件(如环境变量、JVM系统属性等)动态导入配置类时；</li>
<li>在创建自定义的启动注解（如<code>@EnableSomeFuture</code>）时，通过<code>ImportSelector</code>来导入现骨干的配置类；</li>
<li>Spring框架本身在多个地方使用<code>ImportorSelector</code>来简化客户端的配置负担，如<code>@EnableAsync</code>、<code>@EnableScheduling</code>。</li>
</ol>
<p>示例</p>
<pre><code>import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        // 基于条件选择配置类
        String prop = System.getProperty(&quot;myProp&quot;);
        if (&quot;someValue&quot;.equals(prop)) {
            return new String[]{MyConfig1.class.getName()};
        } else {
            return new String[]{MyConfig2.class.getName()};
        }
    }
}

@Configuration
public class MyConfig1 {
    @Bean
    public AppBean appBean() {
        return new AppBean(&quot;from config 1&quot;);
    }
}

@Configuration
public class MyConfig2 {
    @Bean
    public AppBean appBean() {
        return new AppBean(&quot;from config 2&quot;);
    }
}

public class AppBean {
    private String message;
    public AppBean(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

@Configuration
@Import(MyImportSelector.class)
public class MainConfig {
    // 主配置类
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zhangxihai.cn/archives/342/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
