Spring 嵌套类@Configuration注解使用示例

x33g5p2x  于2022-09-24 转载在 Spring  
字(2.9k)|赞(0)|评价(0)|浏览(420)

Spring @Configuration 在类级别进行了注解,表示一个类声明了一个或多个 @Bean 方法。 @Configuration 类可以相互嵌套。我们只需要针对应用程序上下文注册外部配置类,这也将引导嵌套的配置类。使用嵌套的配置类,我们可以避免 @Import 注解。在此页面上,我们将提供创建嵌套配置并根据应用程序上下文引导它们的示例。

创建嵌套配置类

在我们的示例中,我们有两个相互嵌套的配置类。
OuterConfig.java

package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.concretepage")
public class OuterConfig {
    @Bean
    public OuterTask getOuterTask() {
		return new OuterTask("Outer Task");
    }
    
    @Configuration
    public static class InnerConfig {
        @Bean
        public InnerTask getInnerTask() {
		return new InnerTask("Inner Task");
        }
    }
}

InnerTask.java

package com.concretepage;
public class InnerTask {
	private String name;
	public InnerTask(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
}

OuterTask.java

package com.concretepage;
public class OuterTask {
	private String name;
	public OuterTask(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
}

注册外部配置类

当我们根据应用程序上下文注册外部配置类时,它也会引导外部和内部配置类。
MySpringApp.java

package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MySpringApp {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(OuterConfig.class);
		OuterTask outerTask = ctx.getBean(OuterTask.class);
		System.out.println(outerTask.getName());// will work
		InnerTask innerTask = ctx.getBean(InnerTask.class);
		System.out.println(innerTask.getName()); // will work
		
		ctx.registerShutdownHook();
		ctx.close();
	}
}

输出

Outer Task
Inner Task

要在外部配置类中访问内部配置类的bean,我们可以在外部配置类中注入内部配置类的bean。
OuterConfig.java

package com.concretepage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.concretepage")
public class OuterConfig {
	@Autowired
	private InnerTask innerTask;

	@Bean
	public OuterTask getOuterTask() {
		System.out.println(innerTask.getName());
		return new OuterTask("Outer Task");
	}

	@Configuration
	public static class InnerConfig {
		@Bean
		public InnerTask getInnerTask() {
			return new InnerTask("Inner Task");
		}
	}
}

注册内部配置类

当我们针对应用程序上下文注册内部配置类时,它将仅引导内部配置类,而不是它所嵌套的外部配置类。
MySpringApp.java

package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.OuterConfig.InnerConfig;
public class MySpringApp {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(InnerConfig.class);
		InnerTask innerTask = ctx.getBean(InnerTask.class);
		System.out.println(innerTask.getName()); //will work
		OuterTask outerTask = ctx.getBean(OuterTask.class);
		System.out.println(outerTask.getName()); //will throw exception		
		
		ctx.registerShutdownHook();
		ctx.close();
	}
}

相关文章

微信公众号

最新文章

更多