10.Spring配置方式

 

Spring配置

第一阶段:xml配置

Spring 1.x

第二阶段:注解配置

Spring 2.x,基本配置(如数据库配置)用xml,业务配置用注解

第三阶段:Java配置

Spring 3.x,Spring Boot

无论是xml配置,注解配置还是Java配置,都被称为配置元数据,元数据即描述数据的数据。元数据本身不具备任何可执行的能力,只能通过外界代码来对这些元数据进行解析后,才能进行一些有意义的操作。

Java配置

Java配置是通过@Configuration@Bean来实现的。

  • @Configuration声明当前类是一个配置类,相当于一个Spring配置的xml文件

  • @Bean注解在方法上,声明当前方法的返回值为一个Bean

全局配置使用Java配置(如数据库相关配置、MVC相关配置),业务Bean的配置使用注解配置(@Service,@Component,@Repository,@Controller)

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class FunctionService {

public String sayHello(String word){
return "Hello "+word+" !";
}
}

public class UseFunctionService {
FunctionService functionService;

public void setFunctionService(FunctionService functionService) {
this.functionService = functionService;
}

public String sayHello(String word){
return functionService.sayHello(word);
}
}

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

@Configuration
public class JavaConfig {

@Bean
public FunctionService functionService() {
return new FunctionService();
}

@Bean
public UseFunctionService useFunctionService() {
UseFunctionService useFunctionService = new UseFunctionService();
useFunctionService.setFunctionService(functionService());
return useFunctionService;
}


}

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

public static void main(String[] args){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(JavaConfig.class);

UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);

System.out.println(useFunctionService.sayHello("java config"));

context.close();
}
}

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
09:34:41.367 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e81f4dc
09:34:41.473 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
09:34:41.999 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
09:34:42.001 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
09:34:42.003 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
09:34:42.005 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
09:34:42.085 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'javaConfig'
09:34:42.092 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'functionService'
09:34:42.112 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'useFunctionService'
Hello java config !
09:34:42.246 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1e81f4dc, started on Thu Mar 26 09:34:41 CST 2020

Process finished with exit code 0

Spring框架的四大原则

  1. 使用POJO进行轻量级和最小侵入式开发。

  2. 通过依赖注入和基于接口编程实现松耦合。

  3. 通过AOP和默认习惯进行声明式编程。

  4. 使用AOP和模板(template)减少模式化代码。