条件注解,混合配置

条件注解

Profile实际上就是条件注解的一种特殊形式,即条件注解更加灵活,用户可以根据各种不同的条件使用不同的Bean。
条件注解在SpringBoot中使用非常广泛。SpringBoot中提供了许多自动化的配置,例如数据库配置,SpringBoot使用条件注解提前配置好许多常用的类,使用条件注解,在某一个条件满足时,这些配置就会生效
在这里插入图片描述

1.创建接口

package com.sxt.service;

public interface ShowCmd {

	public String show();
}

2.创建接口的实现类

package com.sxt.service.impl;

import com.sxt.service.ShowCmd;

public class WindowsShowCmd implements ShowCmd {

	@Override
	public String show() {
		System.out.println("windows:dir");
		return "dir";
	}
}

package com.sxt.service.impl;

import com.sxt.service.ShowCmd;

public class LinuxShowCmd implements ShowCmd {

	@Override
	public String show() {
		System.out.println("linux:ls");
		return "ls";
	}
}

3.定义条件Condition

package com.sxt.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class WindowsShowCondition implements Condition{

	/**
	 * 匹配条件
	 * @return
	 * true 匹配
	 * false 不匹配
	 */
	@Override
	public boolean matches(ConditionContext cc, AnnotatedTypeMetadata atm) {
		String[] profiles = cc.getEnvironment().getActiveProfiles();
		for (String p : profiles) {
			if (p.contains("windows")) {
				return true;
			}
		}
		return false;
	}
}

package com.sxt.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxShowCondition implements Condition{

	/**
	 * 匹配条件
	 * @return
	 * true 匹配
	 * false 不匹配
	 */
	@Override
	public boolean matches(ConditionContext cc, AnnotatedTypeMetadata atm) {
		String[] profiles = cc.getEnvironment().getActiveProfiles();
		for (String p : profiles) {
			if (p.contains("linux")) {
				return true;
			}
		}
		return false;
	}
}

4.java配置文件

package com.sxt;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import com.sxt.condition.LinuxShowCondition;
import com.sxt.condition.WindowsShowCondition;
import com.sxt.service.ShowCmd;
import com.sxt.service.impl.LinuxShowCmd;
import com.sxt.service.impl.WindowsShowCmd;

@Configuration
public class JavaConfig {

	@Bean
	@Conditional(WindowsShowCondition.class)
	public ShowCmd windowsShowCmd(){
		return new WindowsShowCmd();
	}
	
	@Bean
	@Conditional(LinuxShowCondition.class)
	public ShowCmd linuxShowCmd(){
		return new LinuxShowCmd();
	}
}

5.测试调用

package com.sxt.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.sxt.JavaConfig;
import com.sxt.service.ShowCmd;

public class Test {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
		ac.getEnvironment().setActiveProfiles("linux");
		
		ac.register(JavaConfig.class);
		ac.refresh();
		
		ShowCmd bean = ac.getBean(ShowCmd.class);
		System.out.println(bean.show());
	}
}

在这里插入图片描述

Bean的作用域

在这里插入图片描述
在spring的配置中,默认情况下,bean都是单例的(singleton)。无论获取多少次,获取到的都是同一个bean
在这里插入图片描述
User类

package com.sxt.pojo;

public class User {

	public void run(){
		System.out.println("run方法");
	}
	public User() {
		System.out.println("无参构造方法");
	}
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 	<!-- 注册user对象 scope:默认singleton-->
 	<bean class="com.sxt.pojo.User" scope="prototype" />
 </beans>

测试

package com.sxt.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxt.pojo.User;

public class IocTest {

	@Test
	public void test(){
		//加载spring容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//从spring容器中获取User对象
		User user = ac.getBean(User.class);
		System.out.println(user);
		user = ac.getBean(User.class);
		System.out.println(user);
	}
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

混合配置

开发中可能既有配置文件存在,也在使用java配置的方式,这时候可以使用@ImportResource来实现

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<bean class="com.dpb.javabean.UserBean" ></bean>
</beans>

java配置文件

@Configuration 
@ImportResource("classpath:application.xml")
public class AppJavaConfig {

	@Bean
	Book book(){
		return new Book();
	}
}

版权声明:本文为qq_42539533原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。