Spring

Spring入门阶段一——IOC

什么是Bean?

Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象。

Spring 容器会自动完成@bean对象的实例化。

创建应用对象之间的协作关系的行为称为:装配(wiring),这就是依赖注入的本质。

Bean的实例化过程

aware 装配

如果bean实现了BeanNameAware接口,spring将bean的id传给setBeanName()方法;

什么是反转控制(IOC-Inversion of Control)依赖注入(DI-Dependency Injection)

IOC容器中Bean的生命周期方法

  • Spring IOC容器可以管理Bean的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务.
  • Spring IOC 容器对 Bean 的生命周期进行管理的过程:
    • 通过构造器或工厂方法创建 Bean 实例
    • 为 Bean 的属性设置值和对其他 Bean 的引用
    • 调用 Bean 的初始化方法
    • Bean 可以使用
    • 当容器关闭时, 调用 Bean 的销毁方法
  • 在 Bean 的声明里设置 init-method 和 destroy-method 属性, 为 Bean 指定初始化和销毁方法.

后置处理器BeanPostProcessor

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.test.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* bean后置处理器
* @author zss
*/
public class PostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
if ("narCodeService".equals(beanName)) {//过滤掉bean实例ID为narCodeService
return bean;
}
System.out.println("后置处理器处理bean=【"+beanName+"】开始");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
if ("narCodeService".equals(beanName)) {
return bean;
}
System.out.println("后置处理器处理bean=【"+beanName+"】完毕!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return bean;
}
}
//注意:接口中两个方法不能返回null,如果返回null那么在后续初始化方法将报空指针异常或者通过getBean()方法获取不到bena实例对象 因为后置处理器从Spring IoC容器中取出bean实例对象没有再次放回IoC容器中

// *.xml文件中加入以下信息即可
<!-- Spring后置处理器 -->
<bean id="postProcessor" class="com.test.spring.PostProcessor"/>

执行顺序:最先执行的是postProcessBeforeInitialization,然后是afterPropertiesSet,然后是init-method,然后是postProcessAfterInitialization

Spring中Bean的作用域

  • singleton:单例模式
  • prototype:每次调用新建一个实例
  • request:每次Http请求创建一个新的Bean
  • session:同一个HttpSession共享同一个Bean

Bean的配置三种xml配置方式(全类名反射、工厂方法[静态工厂&实例工厂]、FactoryBean)

Bean基于注解的配置方式

组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件。

对于扫描到的组件,Spring有默认的命名策略:

  • 使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称

特定组件包括:

  • @Component: 基本注解, 标识了一个受 Spring 管理的组件
  • @Respository: 标识持久层组件,持久层一般就是DAO
  • @Service: 标识服务层(业务层)组件
  • @Controller: 标识表现层组件

当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明\context:component-scan\

利用注解建立bean与bean的引用关系(组件装配)@AutoWired @Resource @Inject

\context:component-scan\ 元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.

  • @AutoWired:自动装配具有兼容类型的单个 Bean属性(ByType)
    • 说人话:在IOC容器中找是否用与被注解类型相兼容的Bean,如果有就引用过来。
    • 可用在构造器,普通字段(即使不是public),一切具有参数的方法都可以使用
    • @AutoWired(required=false):false:没有该对象则不报错,不引用
    • 若兼容类型的bean不唯一,则需要在@Component(‘这里的名字’)和需要传参的属性的名字一致;或者在装配时指定bean:@Qualifier(“userRepositoryImpl”)注解
  • @Resource:默认按照ByName自动注入,其余与@AutoWired类似@Resource(name=“helloWorld”)

Spring依赖注入

  • 接口注入(\<property name=”userDao” ref=”userDaoMyBatis”>\</property>)
  • 构造器注入(\<constructor-arg name=”userDao” ref=”userDaoJdbc”>\</constructor-arg>)
  • 基于注解注入(@Autowired)

Spring入门阶段二——AOP

面向切面编程(Aspects Oriented Progromming)

AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充。AOP 的主要编程对象是切面(aspect),而切面模块化横切关注点。在应用 AOP 编程时,仍然需要定义公共功能, 但可以明确的定义这个功能在哪里,以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里。
AOP 的好处:
每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级业务模块更简洁, 只包含核心业务代码

Spring入门阶段三——事务管理

事务简介(实际上和sql的事务一样):

  • 事务管理是企业级应用程序开发中必不可少的技术, 用来确保数据的完整性和一致性。
  • 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用
  • 事务的四个关键属性(ACID)
Ty.Wings wechat
欢迎您订阅我的公众号,并在GitHub上为我Star!