[TOC]
从本章开始,总结一下之前看到的关于spring-annotation
的相关知识点,从最基本的入门到后面与mvc
的整合,全部使用注解版来实现,从此告别繁杂的配置文件,该系列总结会为后面理解spring-boot
提供一定的帮助
AnnotationConfigApplicationContext
现在开始创建一个maven
工程,使用main
函数来读取配置类,创建Spring
的容器,基于注解的ApplicationContext
这个类就是AnnotationConfigApplicationContext
,通过指定一个主配置类,来完成IOC
容器的初始化,该项目不用是一个web
工程
1. 项目准备
pom.xml
导入如下jar
包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ddf.spring</groupId>
<artifactId>spring-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
</project>
2. @Configuration配置类
@Configuration
该注解标注在某个class
上,则可以被spring
识别为一个配置类,配置类的作用等同于以前配置文件版的xml.当然具体需要在配置类中配置什么属性或起到什么作用,这就是另外注解的支持了,@Configuration
只是保证你可以在该类中配置的功能能够被解析,如同一个空的xxx.xml
配置文件,这是一切功能型注解生效的前提!
3. @Bean注解注入bean
@Bean
该注解可以标注在配置类的某个方法上,它实现的意义是为IOC
中容器注入bean
,此时bean
的名称为方法名称,bean
的类型为方法返回类型.以下为向容器中注入一个type
为User
的bean
,该bean
的名称为user
,默认都是单实例bean
@Bean
public User user() {
return new User();
}
等同于
<bean id="user" class="xxx.xxx.User"/>
4. 创建AnnotationConfigApplicationContext
- 创建一个
Bean
对象User.java
package com.ddf.spring.annotation.entity;
/**
* @author DDf on 2018/7/19
*/
public class User {
private Integer id;
private String userName;
private String password;
private String tel;
public User() {
System.out.println("User创建完成...............");
}
public User(Integer id, String userName, String password, String tel) {
this.id = id;
this.userName = userName;
this.password = password;
this.tel = tel;
}
// getter and setter
}
- 新建一个配置类
AnnotationConfiguration
package com.ddf.spring.annotation.configuration;
import com.ddf.spring.annotation.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author DDf on 2018/7/19
*/
@Configuration
public class AnnotationConfiguration {
/**
* 注入一个Type为User(方法返回值)的bean,bean的名称为user(方法名)
* @return
*/
@Bean
public User user() {
return new User();
}
}
- 新建主启动类
Application
package com.ddf.spring.annotation;
import com.ddf.spring.annotation.configuration.AnnotationConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author DDf on 2018/7/19
*/
public class Application {
public static void main(String[] args) {
System.out.println("-----------------------IOC容器初始化-------------------------");
// 创建一个基于配置类启动的IOC容器,如果主配置类扫描包的路径下包含其他配置类,则其他配置类可以被自动识别,如果主配置类扫描包的路径下包含其他配置类,则其他配置类可以被自动识别
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
System.out.println("-----------------------IOC容器初始化完成-------------------------");
// 获取当前IOC中所有bean的名称
String[] definitionNames = applicationContext.getBeanDefinitionNames();
// 打印当前IOC中对应名称的bean和bean的类型
for (String name : definitionNames) {
Object bean = applicationContext.getBean(name);
System.out.println("bean name:" + name + ", type: " + bean.getClass());
}
}
}
- 启动
main
,打印如下,可以看到除了最后一行我们注入的User
以外,容器启动后会默认注入一些bean
,但这些bean
默认都是懒加载的,通过打印可以看出来,这是在容器已经初始化完成之后,获取预定义bean
的名称然后调用getBean()
方法之后获取才创建的,懒加载就是针对单实例的而且是在第一次过去才会被创建,而默认情况下都是容器启动后就会完成创建
-----------------------IOC容器初始化-------------------------
User创建完成...............
-----------------------IOC容器初始化完成-------------------------
bean name:org.springframework.context.annotation.internalConfigurationAnnotationProcessor, type: class org.springframework.context.annotation.ConfigurationClassPostProcessor
bean name:org.springframework.context.annotation.internalAutowiredAnnotationProcessor, type: class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
bean name:org.springframework.context.annotation.internalRequiredAnnotationProcessor, type: class org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
bean name:org.springframework.context.annotation.internalCommonAnnotationProcessor, type: class org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
bean name:org.springframework.context.event.internalEventListenerProcessor, type: class org.springframework.context.event.EventListenerMethodProcessor
bean name:org.springframework.context.event.internalEventListenerFactory, type: class org.springframework.context.event.DefaultEventListenerFactory
bean name:annotationConfiguration, type: class com.ddf.spring.annotation.configuration.AnnotationConfiguration$$EnhancerBySpringCGLIB$$1906ac13
bean name:user, type: class com.ddf.spring.annotation.entity.User