2018年6月6日
18:52
Spring 核心包
maven
<!--导入jar包 core jcl-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!--导入jar包 beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<!--context aop expression-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
Spring ApplicationContext 容器
三种加载方式
- FileSystemXmlApplicationContext:需要提供完成路径
- ClassPathXmlApplicationContext:在类加载的路径寻找
- WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
- AnnotationConfigApplicationContext:加载带有注解的类到spring容器中 ,通过regist方法可以注册多个带有Configuration的类
xml配置
bean头
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
Bean
属性 |
描述 |
class |
这个属性是强制性的,并且指定用来创建 bean 的 bean 类。 |
name |
这个属性指定唯一的 bean 标识符。在基于 XML 的配置元数据中,你可以使用 ID 和/或 name 属性来指定 bean 标识符。 |
scope |
单例模式,或者多例模式等方式的创建 |
autowire |
自动注入可以通过byName byType constructor方式自动注入 |
Lazy-init |
延迟初始化的 bean 告诉 IoC 容器在它第一次被请求时,而不是在启动时去创建一个 bean 实例。 |
Init-method 方法 |
初始化回调函数 |
destroy-method 方法 |
销毁时候回调函数 |
Partent |
声明当前bean的父类 |
Abstract |
声明当前类是抽象的 |
子节点标签 |
描述 |
Constructor-arg |
通过构造方法注入 |
properties |
参数注入 |
Bean 后置处理器 通过实现BeanPostProcessor的方法可以在初始化bean之前或者时候进行操作
注解配置
Beans 头
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
</beans>
<context:annotation-config/>
通过注解设置
在bean属性上的注解
注解 |
描述 |
@Required |
应用于bean的setter方法 |
@Autowried |
应用于bean属性的setter方法,非setter方法,构造函数和属性,默认为bytype的注入 |
@Qualifier |
通过指定确切的将被连线的 bean,@Autowired 和 @Qualifier 注解可以用来删除混乱 |
Spring JSR-250注释 |
@Resource 默认按照名称注入,应用于与Autowried同样的位置 @PostConstruct 用于制定 init-method 方法 @PostDestroy 用于销毁的回调方法 |
创建bean 的注解
注解 |
描述 |
@Configuration |
注解类上用于声明这个类可以使用Spring Ioc 容器作为bean定义的来源 |
@Bean |
注解方法名称作为bean的Id 创建并返回实际的bean |
@import |
注解允许从另一个配置类中加载 @Bean 定义。从而加载一个类就可以获得另一个配置类的bean |
@Scop |
生命周期 |
通过 AnnotationConfigApplicationContext(Class class); 方法加载带有注解的类到容器中
事件处理
接口 |
描述 |
ApplicationListener |
监听上下文事件 onApplicationEvent() |
类 |
事件 |
ApplicationEvent 通过继承这个类可以 自定义监听事件 |
监听事件 ContextRefreshedEvent 初始化或刷新时 ContextStartedEvent 使用 ConfigurableApplicationContext中的start方法启动applicationcontext时 ContextClosedEvent当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时 RequestHandledEvent |
AOP
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
项 |
描述 |
Aspect |
切面,添加功能的类 |
Join point |
程序和面交汇的点叫做连接点。它是一个抽象的概念,在实现AOP时,并不需要去定义一个join point。 |
Advice |
添加到需要添加功能方法的前面后面或者异常处理地方等 |
Pointcut |
需要添加功能的类或者方法 |
Target object |
被代理的对象 |
Weaving |
提供3个织入点: 编译期:在Java编译期,采用特殊的编译器,将切面织入到Java类中。 类加载期:通过特殊的类加载器,在类字节码加载到JVM时,织入切面。 运行期:采用CGLIB或JDK动态代理进行切面的织入 |
通知 |
描述 |
前置通知 |
在一个方法执行之前,执行通知。 |
后置通知 |
在一个方法执行之后,不考虑其结果,执行通知。 |
返回后通知 |
在一个方法执行之后,只有在方法成功完成时,才能执行通知。 |
抛出异常后通知 |
在一个方法执行之后,只有在方法退出抛出异常时,才能执行通知。 |
环绕通知 |
在建议方法调用之前和之后,执行通知。 |
Xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
//声明一个aop
<aop:config>
//声明切面
<aop:aspect id="log" ref="logging">
//切点
<aop:pointcut id="selectAll"
expression="execution(* com.abc.spring.*.*(..))"/>
//前置通知
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
//返回后通知()
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
//后置通知
<aop:after-returning pointcut-ref="selectAll"
returning="retVal"
method="afterReturningAdvice"/>
//获取异常
<aop:after-throwing pointcut-ref="selectAll"
throwing="ex"
method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config>
<bean id="student" class="com.abc.spring.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<bean id="logging" class="com.abc.spring.Logging"/>
</beans>
注解配置
xml
<aop:aspectj-autoproxy/>
注解 |
解释 |
@Aspect |
声明一个切面 |
@Pointcut |
声明一个切点 |
@Before |
前置通知 |
@After |
后置通知 不考虑结果相当于在finally 里执行 |
@AfterReturning |
返回通知 |
@AfterThrowing |
异常通知 |
@Around |
环绕通知,可以代替上面所有通知 |
已使用 Microsoft OneNote 2016 创建。