读Spring实战(第四版)概括—高级装配

本文深入探讨了Spring的高级装配特性,包括环境与Profile的使用,如何激活Profile,Spring Boot中Profile的应用,条件注解@Conditional的实践,解决自动装配歧义性,Bean作用域详解,以及运行时值注入。文章通过实例详细解释了如何配置和管理不同环境下的Bean,以及如何根据条件创建Bean,同时讨论了不同作用域在Web应用中的应用场景。
摘要由CSDN通过智能技术生成

1.环境与Profile

在开发软件的时候,有时候需要从一个环境迁移到另一个环境。比如在开发阶段我们使用的是dev的环境,在测试阶段使用的是product环境,这时我们就需要不同的配置。Spring同样也提供了类似的解决方案(在Spring3.1中引入了bean profile功能)。如下所示,是一个使用@Profile注解来实现的实例。

首先需要准备两个配置类:

// ProdConfiguration.java
@Configuration
@Profile("prod")
public class ProdConfiguration {
	@Bean
	public ProdE getProdE(){
		ProdE prodE = new ProdE();
		prodE.setContent("产品");
		return prodE;
	}
}
// DevConfiguration.java
@Configuration
@Profile("dev")
public class DevConfiguration {	
	@Bean
	public DevE getDevE() {
		DevE devE = new DevE();
		devE.setContent("开发");
		return devE;
	}
}

再需要在初始化Servlet环境配置类(用于替换web.xml)中设置profile值(下划线)

public class HelloWorldInitializer implements WebApplicationInitializer {
	public void onStartup(ServletContext container) throws ServletException {
		AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
		ctx.register(HelloWorldConfiguration.class);
		ctx.setServletContext(container);
		ctx.getEnvironment().setActiveProfiles("dev");
		ServletRegistration.Dynamic servlet = container.addServlet(
				"dispatcher", new DispatcherServlet(ctx));
		servlet.setLoadOnStartup(1);
		servlet.addMapping("/");
	}
}

最后用一个Controller来测试一下:

@Controller
@RequestMapping("/")
public class HelloWorldController {
	@Autowired(required=false)
	ProdE prodE;
	@Autowired(required=false)
	DevE devE;

	@RequestMapping(method = RequestMethod.GET)
	public String test(ModelMap model) {
		if(null!=prodE){
			System.out.println("******"+prodE.getContent()+"******");
		}else{
			System.out.println("******prodE is null******");
		}
		
		if(null!=devE){
			System.out.println("******"+devE.getContent()+"******");
		}else{
			System.out.println("******devE is null******");
		}
		model.addAttribute("greeting", "Hello World from Spring 4 MVC");
		return "welcome";
	}
}

当然也可以使用XML配置,如下所示:

<beans profile="prod">
	<!-- 在这里配置bean -->
</beans>
<beans profile="dev">
	<!-- 在这里配置bean -->
</beans>

从Spring3.2开始已经支持在方法级别上使用@Profile注解,与@Bean注解一起使用,这样就可以将这两种声明放置在同一个配置类中。

2.激活使用profile

如果不激活使用profile,你配置的profile也是不会被使用的,因为Spring不能确定哪个profile是需要使用的。Spring提供了两个独立的属性来构成一个比较合理的设置:spring.profiles.active和spring.profiles.default。如果设置了spring.profiles.active属性,则Spring会设置本值,如果spring.profiles.active没有设置,则使用spring.profiles.default的默认值。spring.profiles.active对应于注解中的ConfigurableEnvironment.setActiveProfiles(String... profiles);spring.profiles.default对应于注解中的ConfigurableEnvironment.setDefaultProfiles(String... profiles);我们可以有多种方式设置这两种属性:

  • 作为DispatcherServlet的初始化参数
  • 作为Web应用上下文条目参数
  • 作为JNDI条目
  • 作为环境变量
  • 作为JVM的系统变量
  • 在集成测试类上,使用@ActiveProfiles注解设置

XML中的配置如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>test</display-name>
	<!-- - Location of the XML file that defines the root application context. 
		- Applied by ContextLoaderListener. -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/application-config.xml</param-value>
	</context-param>
<!-- 为上下文设置默认的profile -->
	<context-param>
		<param-name>spring.profiles.default</param-name>
		<param-value>dev</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.spr
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值