面向对象编程中很重要的一步就是进行对象设计和定义(比如类或接口等),而下一步就要考虑如何使用这些对象,一般是先通过实例化对象来生产实例,然后调用实例的方法来执行逻辑。其中实例化有多种方法,传统的new,Class.forName()和反射等,当项目较大,对象和实例将会非常多,依赖关系也会较为复杂,一般传统的方式会较难控制。Spring提供的IoC或DI,使用配置的方式来完成实例容器、实例化、实例间的依赖关系(依赖注入)和生命周期管理等。
auto-detect
autowire
实例容器
BeanFactory
BeanFactoryAware
ApplicationContext
常用的有:
ClassPathXmlApplicationContext
AnnotationConfigApplicationContext
AnnotationConfigWebApplicationContext:适用于web项目
BeanFactoryPostProcessor
实例化bean的方法:实例工厂、静态工厂和构造器
ApplicationContextAware
BeanPostProcessor
1 | |
BeanFactoryPostProcessor
1 | |
基于XML
<import/>
<context:component-scan/>
<context:annotation-config/>
基于注解
Bean定义注解
以下注解被扫描后实现bean定义
@Component
会自动constructor注入
1 | |
不会自动setter注入
1 | |
@Controller
@Service
@Repository
@Entity
依赖注入注解
@Autowired
Spring4新增泛型注入支持,例如:
1 | |
@PostConstruct
@PreDestroy
基于Java Config
@Configuration
定义@bean的载体,好比一个定义<bean>的xml文件,可以被实例容器加载
@Import
引入其他的@Configuration,有如下三种方式
-
直接引入
直接引入另外一个配置类,最常用的方式,简单明了,例如:
@Import(App2Config.class),效果类似<import/> -
条件引入
通过org.springframework.context.annotation.ImportSelector实现,实例参见实践Spring Enable系列之EnableAsync
-
动态注册
通过org.springframework.context.annotation.ImportBeanDefinitionRegistrar实现,实例参见实践Spring-Enable系列之EnableAspectJAutoProxy
@Bean
bean定义
@Scope
默认是单例(singleton),也可以指定为原型(prototype),
1 | |
@Primary
首选bean
@Qulifier
指定注入那个bean
依赖注入
-
引用方法注入
-
方法参数注入
默认按照
类型注入,当有多个同类型实例时按照名字注入,如果注入失败则报存在多个同类型实例错误。- 构造方法参数注入
- @Bean方法参数注入
详情参见下面样例
生命周期
1 | |
备注:需要实例容器ctx.registerShutdownHook(),否则destoryMethod不会触发
样例
SaveVideoInfoDao.java
1 | |
VideoService.java
1 | |
App1Config.java
1 | |
App2Config.java
1 | |
App3Config.java
1 | |
SampleMain.java
1 | |
样例源码请移步github之sample-spring-javaconfig
@ComponentScan
Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML’s <context:component-scan/> element.
One of basePackageClasses(), basePackages() or its alias value() may be specified to define specific packages to scan. If specific packages are not defined scanning will occur from the package of the class with this annotation.
Note that the <context:component-scan> element has an annotation-config attribute, however this annotation does not. This is because in almost all cases when using @ComponentScan, default annotation config processing (e.g. processing @Autowired and friends) is assumed. Furthermore, when using AnnotationConfigApplicationContext, annotation config processors are always registered, meaning that any attempt to disable them at the @ComponentScan level would be ignored.
@ImportResource
1 | |