0%

基于配置方式实现自定义条件装配

本文主要讲通过配置方式来实现自定义条件装配 —— @Profile

我们尝试使用Profile实现两个计算服务,分别是Java7的for循环以及Java8的lambda表达式


首先我们先新建一个计算服务接口

CalculateService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package xyz.suiwo.diveinspringboot.service;

/**
* @author suiwo
* @title: CalculateService
* @date 2019-06-06 13:55
*/
public interface CalculateService {

/**
* sum求和
* @param values 多个整数
* @return
*/
Integer sum(Integer... values);
}

添加两种方式的实现类

Java7CalculateService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package xyz.suiwo.diveinspringboot.service;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

/**
* @author suiwo
* @title: Java7CalculateService
* @date 2019-06-06 13:56
*/
@Service
@Profile("Java7")
public class Java7CalculateService implements CalculateService{

@Override
public Integer sum(Integer... values) {
System.out.println("Java7实现");
int sum = 0;
for(int i : values){
sum += i;
}
return sum;
}

public static void main(String[] args) {
CalculateService calculateService = new Java7CalculateService();
System.out.println(calculateService.sum(1,2,3,4,5,6,7,8,9,10));
}
}

Java8CalculateService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package xyz.suiwo.diveinspringboot.service;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.stream.Stream;

/**
* @author suiwo
* @title: Java8CalculateService
* @date 2019-06-06 13:58
*/
@Service
@Profile("Java8")
public class Java8CalculateService implements CalculateService {
@Override
public Integer sum(Integer... values) {
System.out.println("Java8实现");
int sum = Stream.of(values).reduce(0,Integer::sum);
return sum;
}

public static void main(String[] args) {
CalculateService calculateService = new Java7CalculateService();
System.out.println(calculateService.sum(1,2,3,4,5,6,7,8,9,10));
}
}

下面我们来添加启动类

CalculateServiceBootstrap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package xyz.suiwo.diveinspringboot.bootstrap;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import xyz.suiwo.diveinspringboot.service.CalculateService;

/**
* @author suiwo
* @title: CalculateServiceBootstrap
* @date 2019-06-06 14:01
*/
@SpringBootApplication(scanBasePackages = "xyz.suiwo.diveinspringboot.service")
public class CalculateServiceBootstrap {

public static void main(String[] args) {
ConfigurableApplicationContext context =
new SpringApplicationBuilder(CalculateServiceBootstrap.class)
.web(WebApplicationType.NONE)
.profiles("Java8")
.run(args);

CalculateService calculateService = context
.getBean(CalculateService.class);

System.out.println("sum:" + calculateService.sum(1,2,3,4,5,6,7,8,9,10));

//关闭上下文
context.close();
}
}

此时终端打印结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.5.RELEASE)

2019-06-14 11:09:17.221 INFO 30420 --- [ main] x.s.d.b.CalculateServiceBootstrap : Starting CalculateServiceBootstrap on XXX with PID 30434 (/Users/dive-in-spring-boot/target/classes started by XXX in /Users/dive-in-spring-boot)
2019-06-14 11:09:17.228 INFO 30420 --- [ main] x.s.d.b.CalculateServiceBootstrap : The following profiles are active: Java8
2019-06-14 11:09:20.062 INFO 30420 --- [ main] x.s.d.b.CalculateServiceBootstrap : Started CalculateServiceBootstrap in 3.416 seconds (JVM running for 4.569)
Java8实现
sum:55

Process finished with exit code 0

若将profile改成Java7,则终端结果为

1
2
3
4
5
6
7
8
9
10
11
12
13
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.5.RELEASE)

2019-06-14 11:10:17.847 INFO 30434 --- [ main] x.s.d.b.CalculateServiceBootstrap : Starting CalculateServiceBootstrap on XXX with PID 30434 (/Users/dive-in-spring-boot/target/classes started by XXX in /Users/dive-in-spring-boot)
2019-06-14 11:10:17.851 INFO 30434 --- [ main] x.s.d.b.CalculateServiceBootstrap : The following profiles are active: Java7
2019-06-14 11:10:20.149 INFO 30434 --- [ main] x.s.d.b.CalculateServiceBootstrap : Started CalculateServiceBootstrap in 2.822 seconds (JVM running for 3.95)
Java7实现
sum:55