最近在研究spring的功能实例,通过一个个实例来体会spring的强大之处。当然体会之后就需要将体会总结出来,正所谓积沙成塔,到使用的时候也方便查询,接下来的几篇文章我们就一起来通过几个实例来学习spring的一些功能。
1. 对于spring首先想到的概念就IOC,spring生成容器的方式可以分为两种,一种是通过ClassPathXmlApplicationContext加载xml配置文件,进行扫描定义类名的方式,然后使用ClassUtils.forName((String) value, null)
反射的方式生成新类。
2. 另外一种是在类中使用AnnotationConfigApplicationContext加载注释类,然后扫描类中注释,将类名放到Map中,然后根据类名使用ClassUtils.forName方法实例化对象。
public void registerBean(Class<?> annotatedClass, String name,
@SuppressWarnings("unchecked") Class<? extends Annotation>... qualifiers) {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
return;
}
//spring 默认生成不是单例,如果需要生成单例需要使用@Scope注释或在xml中使用<aop:scoped-proxy/>元素注释
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
abd.setScope(scopeMetadata.getScopeName());
String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
if (qualifiers != null) {
for (Class<? extends Annotation> qualifier : qualifiers) {
if (Primary.class == qualifier) {
abd.setPrimary(true);
}
else if (Lazy.class == qualifier) {
abd.setLazyInit(true);
}
else {
abd.addQualifier(new AutowireCandidateQualifier(qualifier));
}
}
}
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
实例:
首先生成接口类:
package hello;
public interface MessageService {
String getMessage();
}
之后将接口类注入到使用类内部:
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
final private MessageService service;
@Autowired
public MessagePrinter(MessageService service){
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
最后我们通过AnnotationConfigApplicationContext加载类:
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class Application {
@Bean
MessageService mockMessageService(){
return new MessageService() {
public String getMessage() {
return "Hello World";
}
};
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
运行一下,我们可以看到打印信息Hello World
,我们可以看到整个过程没有new的使用,我们通过context.getBean(MessagePrinter.class)获取MessagePrinter的实例,而实例内的MessageService则由spring代理帮我们自动实现了。
附录pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org</groupId>
<artifactId>bus-spring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>bus.spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.6.RELEASE</spring.version>
<reactor.version>1.1.0.M3</reactor.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>1.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- security start -->
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId>
<version>1.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId> <version>4.1.0.RELEASE</version>
</dependency> -->
<!-- <dependency> <groupId>org.apache.directory.server</groupId> <artifactId>apacheds-server-jndi</artifactId>
<version>1.5.5</version> </dependency> -->
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId>
</dependency> -->
<!-- security start -->
<!-- redis start -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
<!-- redis end -->
<!-- rebbitmq start -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
<version>1.5.4.RELEASE </version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId> spring-rabbit</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<!-- rebbitmq end -->
<!-- http://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<!-- <dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.4.RELEASE</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<!-- Reactor -->
<dependency>
<groupId>org.projectreactor</groupId>
<artifactId>reactor-net</artifactId>
<version>${reactor.version}</version>
</dependency>
<dependency>
<groupId>org.projectreactor.spring</groupId>
<artifactId>reactor-spring-context</artifactId>
<version>${reactor.version}</version>
</dependency>
<!-- GraphicsMagick -->
<dependency>
<groupId>com.sharneng</groupId>
<artifactId>gm4java</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>bus-spring</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>