1,基本概念
1)Gradle构建
compile 'org.springframework:spring-context:4.3.12.RELEASE'
2)在应用spring时,有两个地方可以声明一个bean。一个是在spring的xml配置文件中配置<bean>(或者@Configuration中定义bean方法);一个是在代码中通过Component等标注声明。(Component、Repository、Service以及Controller)
3)BeanFactory接口,用于访问Spring的bean容器。The root interface for accessing a Spring bean container.
image.png
singletonObjects用户缓存singleton bean
image.png
ApplicationContext是BeanFactory的子接口Central interface to provide configuration for an application.
,它extends ListableBeanFactory。image.png
AnnotationConfigApplicationContext标准的应用上下文(@since 3.0)Standalone application context, accepting annotated classes as input
,继承GenericApplicationContext(拥有属性DefaultListableBeanFactory
)
DefaultListableBeanFactory它有一个beanDefinitionMap,来存放bean的定义。private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);
image.png
4)容器通过读取元配置
,来初始化bean容器,然后指导对象的初始化、配置和组装。The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.
元配置可以通过xml、Java注解以及Java代码表示。configuration metadata is represented in XML, Java annotations, or Java code.
XML元配置:XML-based configuration metadata shows these beans configured as <bean/> elements inside a top-level <beans/> element.
Java-Based配置:Java configuration typically uses @Bean annotated methods within a @Configuration class.
2,主要注解和jar包。
1)spring-context包中的主要注解。
@Configuration通过@Configuration注解类,里面包含beans方法。declares bean methods, processed by the Spring container
@Bean注解在方法上, 可以产生一个由spring容器管理的bean对象。Indicates that a method produces a bean to be managed by the Spring container.
@Component注解类上,该类可以被spring自动检测,成为spring容器的bean。可以处理@Autowired等注解。when using annotation-based configuration and classpath scanning. this class can be auto-detection
@ComponentScan(value = "com.hzq.ioc")配置spring组件的扫描路径。Configures component scanning directives. default is this package
2)spring-context依赖引入几个spring jar包。
spring-core,spring的核心工具类,是其他组件的核心基础。eg: org.springframework.util.Assert断言类,org.springframework.util.StringUtils字符串工具类,org.springframework.util.DigestUtils处理MD5信息
spring-expression,spring的表达式语言。
spring-beans,含访问配置文件、创建和管理bean、以及进行IOC操作。如果应用只需基本的IoC/DI 支持,引入spring-core.jar 及spring-beans.jar 文件就可以了。
spring-aop,使用基于AOP 的Spring特性。如声明型事务管理(Declarative Transaction Management)需要aop的支持。
spring-context,为Spring 核心提供了大量扩展。提供ApplicationContext,从注解的类中获取bean的定义,并自动刷新spring的context。deriving bean definitions from the given annotated classes and automatically refreshing the context
3)面向接口编程。
@Bean注解bean方法和@Service / @Component等注解实现类。都可以将生成的bean对象,交给spring container管理。
正常情况,当一个bean交由spring container管理, 在其他地方注入该bean, 都是同一个对象。(单例,无状态的类。)
4)例子
@Bean//Indicates that a method produces a bean to be managed by the Spring container.
public MessageService messageService() {
return new MessageServiceImpl();
}
//@Service,This annotation serves as a specialization of @Component,
allowing for implementation classes to be autodetected through classpath scanning.
@Component // 或者@Service
public class MessageServiceImpl implements MessageService{
@Override
public void printMessage() {
System.out.println(this.getMessage());
}
@Override
public String getMessage() {
return "I am message";
}
}
package com.hzq.ioc.msg;
/**
* Created by hzq on 2017/10/14.
*/
public interface MessageService {
/**
* 打印msg的信息
*/
void printMessage();
/**
* 获取message信息
* @return
*/
String getMessage();
}
package com.hzq.ioc.msg;
/**
* Created by hzq on 2017/10/14.
*/
public class MessageServiceImpl implements MessageService{
@Override
public void printMessage() {
System.out.println(this.getMessage());
}
@Override
public String getMessage() {
return "I am message";
}
}
package com.hzq.ioc.msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by hzq on 2017/10/14.
*/
@Component
public class InvokeMessage {
@Autowired//成员变量注入
private MessageService messageService;
private MessageService messageService2;
@Autowired//成员方法注入
public void injectionMessage(MessageService messageService){
System.out.println("这里注入 messageService2");
this.messageService2 = messageService;
}
public void testInjectionMessage(){
// 注入的是同一个对象
System.out.println("注入的两个对象相等吗?" + (messageService == messageService2));
this.messageService.printMessage();
messageService2.printMessage();
}
}
package com.hzq.ioc;
import com.hzq.ioc.msg.InvokeMessage;
import com.hzq.ioc.msg.MessageService;
import com.hzq.ioc.msg.MessageServiceImpl;
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;
import org.springframework.stereotype.Component;
/**
* Created by hzq on 2017/10/14.
*/
@Configuration//declares bean methods, processed by the Spring container
@ComponentScan(value = "com.hzq.ioc")//Configures component scanning directives. default this package
@Component//when using annotation-based configuration and classpath scanning. this class auto-detection
public class Application {
@Bean//Indicates that a method produces a bean to be managed by the Spring container.
public MessageService messageService() {
return new MessageServiceImpl();
}
public static void main(String[] args) {
// deriving bean definitions from the given annotated classes
// and automatically refreshing the context.
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
//获取bean的两种方式
//1,<bean>xml配置、@Bean方法
//2,使用@Component等注解标识
MessageService messageService = context.getBean(MessageService.class);
messageService.printMessage();
InvokeMessage invokeMessage = context.getBean(InvokeMessage.class);//自动检测到,交由spring容器管理
invokeMessage.testInjectionMessage();
}
}