Spring入门

配置

添加 maven 依赖

1
2
3
4
5
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>

Ioc容器

Spring 的 ApplicationContext 容器

最常被使用的 ApplicationContext 接口实现:

  • FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径
  • ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。
  • WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。

Bean

Bean的属性

属性 描述
class 这个属性是强制性的,并且指定用来创建 bean 的 bean 类。
name 这个属性指定唯一的 bean 标识符。在基于 XML 的配置元数据中,你可以使用 ID 和/或 name 属性来指定 bean 标识符。
scope singleton / prototype
constructor-arg 注入依赖关系
properties 注入依赖关系
autowiring mode byType / byName
lazy-initialization mode 延迟初始化的 bean 告诉 IoC 容器在它第一次被请求时,而不是在启动时去创建一个 bean 实例。
initialization 方法 在 bean 的所有必需的属性被容器设置之后,调用回调方法
destruction 方法 当包含该 bean 的容器被销毁时,使用回调方法

BeanPostProcessor

实现 BeanPostProcessor 接口

调用顺序:

  1. BeanPostProcessor.postProcessBeforeInitialization
  2. init-method
  3. BeanPostProcessor.postProcessAfterInitialization

依赖注入

构造函数

强制

setter函数

非强制

配置方法:注解

xml 文件中添加

1
<context:annotation-config/>

@Required

  • 修饰setter方法
    表示该属性必须设置

@Autowired

  • 修饰setter方法

    执行 byType 自动连接

  • 修饰属性

    省略该属性的setter函数,执行 byType 自动连接

  • 修饰构造函数

    执行 constructor 自动连接

默认 @Autowired 包括 @Required ,可以使用 @Autowired (required = false) 取消

@Qualifier

  • 修饰属性

    在有多个相同类型的 Bean 的情况下,指定装配 id 的那个

@PostConstruct @PreDestroy

  • 修饰方法

    声明 init-method 和 destroy-method 方法

配置方法:Java

@Configuration

  • 修饰类

    声明该类是配置类

@Bean

  • 修饰方法

    表明该方法返回一个Bean

Java 配置方法

1
2
3
4
5
6
7
8
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean // 方法名就是Bean的id
public HelloWorld helloWorld(){
return new HelloWorld();
}
}

等价于对应的 xml 配置

1
2
3
<beans>
<bean id="helloWorld" class="HelloWorld" />
</beans>

进行调用

1
2
3
4
5
6
7
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}

@Import

  • 修饰配置类

    导入所需的其他Bean的配置类

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
// ConfigA.java
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
// ConfigB.java
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B a() {
return new A();
}
}
// App.java
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}

@Scope

  • 修饰返回Bean的方法

    和 xml 中作用相同

事件

通过 ApplicationEvent 类和 ApplicationListener 接口来提供在 ApplicationContext 中处理事件。

序号 Spring 内置事件 & 描述
1 ContextRefreshedEventApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext接口中使用 refresh() 方法来发生。
2 ContextStartedEvent当使用 ConfigurableApplicationContext 接口中的 start() 方法启动 ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序。
3 ContextStoppedEvent当使用 ConfigurableApplicationContext 接口中的 stop() 方法停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作。
4 ContextClosedEvent当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。
5 RequestHandledEvent这是一个 web-specific 事件,告诉所有 bean HTTP 请求已经被服务。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// MainApp.java
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");

context.start();

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();

context.stop();
}
}
// CStartEventHandler
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}

参考资料

Core Technologies 这是官网的官方文档,找了好久才找到ORZ:https://spring.io -> projects -> spring framework -> features -> core technologies,看来官方已经默认没有noob会从官网开始学习spring了

Spring教程_极客学院Wiki

Spring Tutorial