Spring 自定义条件装配
定义、:Bean装配前的前置判断
举例:@Profile,@Conditional
实现:注解方式,编程方式
注解方式
创建一个计算合的service
package com.imooc.diveinspringboot.service;
import org.springframework.stereotype.Service;
@Service
public interface CalculateService {
Integer sum(Integer... values);
}
创建两个条件实现类
java7 for循环实现
package com.imooc.diveinspringboot.service;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
/*
* @author:ty
* @create:2019-04-23 21:35
* */
@Profile("Java7")
@Service
public class Java7CalculateService implements CalculateService{
@Override
public Integer sum(Integer... values) {
System.out.println("使用java7 for循环实现");
int sum=0;
for (int i=0;i<values.length;i++)
{
sum+=values[i];
}
return sum;
}
}
Java 0 lamada实现
package com.imooc.diveinspringboot.service;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import java.util.stream.Stream;
/*
* @author:ty
* @create:2019-04-23 21:35
* */
@Profile("Java8")
@Service
public class Java8CalculateService implements CalculateService{
@Override
public Integer sum(Integer... values) {
int sum= Stream.of(values).reduce(0,Integer::sum);
return sum;
}
}
然后再bootstrap
package com.imooc.diveinspringboot.bootstrap;
import com.imooc.diveinspringboot.service.CalculateService;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
/*
* @author:ty
* @create:2019-04-23 21:40
* */
@SpringBootApplication(scanBasePackages = "com.imooc.diveinspringboot.service")
public class CalculateServiceBootStrap {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(CalculateServiceBootStrap.class)
.web(WebApplicationType.NONE)
.profiles("Java7") 重点**
.run(args);
CalculateService calculateService=context.getBean(CalculateService.class);
System.out.println(calculateService.sum(1,2,3,4,5,6,7,8,9,10));
context.close();
}
}
使用@Conditional
先创建一个自定义的注解@ ConditionalOnSystemProperty
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
@Documented
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionalOnSystemProperty {
/*
* java 系统属性名称
* @return
* */
String name();
/*
* java系统属性值
* @return
* */
String value();
}
然后创建OnSystemPropertyCondition类,它继承于conditon类,在里面写一些约束条件。
@Component
public class OnSystemPropertyCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取注解的原信息
Map<String,Object> attributes = annotatedTypeMetadata.getAnnotationAttributes
(ConditionalOnSystemProperty.class.getName());
String propertyName = String.valueOf(attributes.get("name"));
String propertyValue = String.valueOf(attributes.get("value"));
String javaPropertyValue = System.getProperty(propertyName);
return propertyValue.equals(javaPropertyValue);
}
}
其中值得注意的是AnnotatedTypeMetadata ,这里面有注解的原信息,很重要。
然后再进行,bootstrap的测试,省略。