一、改造为Spring纯xml项目
https://www.yuque.com/wuxiangwuwo/dfvedv/yzx2z4
使用之前的hua-transfer项目,改造为hua-transfer-ioc-xml项目。可作为普通maven项目改造为Spring项目的参考。
1 复制之前的项目
2 修改如下地方
3 导入模块
4 引入Spring IoC容器功能依赖
<!--引入Spring Ioc 容器功能--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.2.RELEASE</version></dependency>
5 改造beans.xml
- 配置文件可以是任意名,但一般默认为applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">根据实际需要,将所有https改为http。
6 JavaSE下IoC容器启动测试
@Test
public void testIoC() throws Exception {
// 通过读取classpath下的xml文件来启动容器(xml模式SE应用下推荐)
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// 不推荐使用
//ApplicationContext applicationContext1 =
// new FileSystemXmlApplicationContext("文件系统的绝对路径");
// 第一次getBean该对象
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
System.out.println("accountDao:" + accountDao);
applicationContext.close();
}
7 Web下启动IoC容器
7.1 导入spring-web依赖
<!--引入spring web功能-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
7.2 配置web.xml
<!--配置Spring ioc容器的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--使用监听器启动Spring的IOC容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- ContextLoaderListener的全限定名可以通过双击shift键,查询到。
7.3 修改Servlet的代码
Servlet类获取Web容器对象,删除自定义的BeanFactroy类
// 1. 实例化service层对象
// private ProxyFactory proxyFactory = (ProxyFactory) BeanFactory.getBean("proxyFactory");
// private AccountService accountService = (AccountService) proxyFactory.getProxy(BeanFactory.getBean("accountService"));
private AccountService accountService = null;
@Override
public void init() throws ServletException {
WebApplicationContext webApplicationContext = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
ProxyFactory proxyFactory = (ProxyFactory) webApplicationContext
.getBean("proxyFactory");
accountService = (AccountService) proxyFactory
.getProxy(webApplicationContext.getBean("accountService"));
}
7.4 tomcat插件启动
二、纯xml方式使用AOP
需求:横切逻辑代码是打印⽇志,希望把打印⽇志的逻辑织⼊到⽬标⽅法的特定位置(service层transfer⽅法)
1 导入AOP相关依赖
<!--spring aop的jar包支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<!--第三方的aop框架aspectj的jar-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
2 创建切面类LogUtils
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class LogUtils {
/**
* 业务逻辑开始之前执行
*/
public void beforeMethod(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
System.out.println(arg);
}
System.out.println("业务逻辑开始执行之前执行.......");
}
/**
* 业务逻辑结束时执行(无论异常与否)
*/
public void afterMethod() {
System.out.println("业务逻辑结束时执行,无论异常与否都执行.......");
}
/**
* 异常时时执行
*/
public void exceptionMethod() {
System.out.println("异常时执行.......");
}
/**
* 业务逻辑正常时执行
*/
public void successMethod(Object retVal) {
System.out.println("业务逻辑正常时执行.......");
}
/**
* 环绕通知
*
*/
public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) {
System.out.println("环绕通知中的beforemethod....");
Object result = null;
try{
// 控制原有业务逻辑是否执行
result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
}catch(Exception e) {
System.out.println("环绕通知中的exceptionmethod....");
}finally {
System.out.println("环绕通知中的after method....");
}
return result;
}
}
3 添加配置
applicationContext.xml中增加AOP的约束头
<!--
Spring基于XML的AOP配置前期准备: 在spring的配置⽂件中加⼊aop的约束
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
Spring基于XML的AOP配置步骤:
第⼀步:把通知Bean交给Spring管理
第⼆步:使⽤aop:config开始aop的配置
第三步:使⽤aop:aspect配置切⾯
第四步:使⽤对应的标签配置通知的类型
⼊⻔案例采⽤前置通知,标签为aop:before
-->
具体配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/beans/spring-aop.xsd
">
<!--省略无关配置-->
<!--进行aop相关的xml配置,配置aop的过程其实就是把aop相关术语落地-->
<!--横切逻辑bean-->
<bean id="logUtils" class="com.hua.utils.LogUtils"></bean>
<!--使用config标签表明开始aop配置,在内部配置切面aspect-->
<!--aspect = 切入点(锁定方法) + 方位点(锁定方法中的特殊时机)+ 横切逻辑 -->
<aop:config>
<aop:aspect id="logAspect" ref="logUtils">
<!--切入点锁定我们感兴趣的方法,使用aspectj语法表达式-->
<!--<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>-->
<aop:pointcut id="pt1" expression=
"execution(public void com.hua.service.impl.AccountServiceImpl.
transfer(java.lang.String,java.lang.String,int))"/>
<!--方位信息,pointcut-ref关联切入点-->
<!--aop:before前置通知/增强-->
<!-- <aop:before method="beforeMethod" pointcut-ref="pt1"/>-->
<!--aop:after,最终通知,无论如何都执行-->
<!--aop:after-returnning,正常执行通知-->
<!-- <aop:after-returning method="successMethod" returning="retValue"/>-->
<!--aop:after-throwing,异常通知-->
<aop:around method="arroundMethod" pointcut-ref="pt1"/>
</aop:aspect>
</aop:config>
</beans>
4 测试
/**
* 测试xml aop
*/
@Test
public void testXmlAop() throws Exception {
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
accountService.transfer("6029621011000","6029621011001",100);
}
三、纯xml方式使用AOP方式二
1 新增依赖
<!--spring aop的jar包支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<!--第三方的aop框架aspectj的jar-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
2 配置切面类
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.println("开启事务"); //额外功能 【前置通知】
//注意:procee方法的返回值就是你目标类方法的返回值
Object o = mi.proceed(); //放行, 继续执行目标类中的方法
System.out.println("提交事务"); //额外功能 【后置通知】
return o;
}
}
3 xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/beans/spring-aop.xsd
">
<!-- 配置额外功能,代理类也交给工厂 -->
<bean id="myAdvice" class="com.hua.aspect.MyAdvice"/>
<!-- 配置AOP -->
<aop:config>
<!-- 3.配置切入点
com.hua.service.impl下所有的类中的所有方法(最前边的*代表返回值随意,小括号里面..代表参数随意)
-->
<!--<aop:pointcut id="pc"
expression="execution(* com.hua.service.impl.*.*(..))"/>-->
<!-- annocation切入点-->
<aop:pointcut id="pc"
expression="@annotation(com.hua.annotation.A)"/>
<!-- 4.在切入点织入额外功能
advice-ref="指向你的额外功能"
pointcut-ref="指向你的切入点"
-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
4 代码测试
/**
* 测试xml aop
*/
@Test
public void testXmlAop() throws Exception {
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
accountService.transfer("6029621011000","6029621011001",100);
}





