花10天学习SSM能学多少?
文章目录
前言
今天是学习SSM的第三天,今天课比较多所以学习内容就少了很多,喜欢这个系列的同学就多多关注吧!
一、什么是基于代理类的AOP的实现
想法:是将切面中的某个通知插入到目标类的目标方法的适当位置;
期望:用户CRUD的功能:本身CRUD逻辑业务前添加日志类中的开启日志和事务类中的开启事务功能;并且在其后添加事务类中的提交事务和日志类中的关闭日志功能;
准备工作:🍔
1.用户类UserDao(目标类),方法CURD业务逻辑(目标方法)要做完
2.日志类-切面(开启日志和保存日志-通知)
事务类-切面(开启事务和提交事务-通知)
通过基于代理类的AOP的实现将上述功能整合
代理类:目标类+切面
代理方法:目标方法+多个通知
用户增加==》(开启日志+开启事务)+用户增加+(提交事务+关闭日志)
用户删除==》(开启日志+开启事务)+用户删除+(提交事务+关闭日志)
用户修改==》(开启日志+开启事务)+用户修改+(提交事务+关闭日志)
用户查找==》(开启日志+开启事务)+用户查找+(提交事务+关闭日志)
1准备切面和通知;(外来)-引用其它工程的业务逻辑
2准备目标类和目标方法;(自己)-自己写的业务逻辑
3找到适当的位置;(目标方法的前后等位置)-切入点表达式
UserDAOUserDAOProxy
实现了这个做成我们就叫做基于代理类的AOP的实现
二、保姆级别开发教程
0.创建如下图目录:
注意🧨🧨
1.如果是小白,jar包就全部导入
2.注意day1012proxy.xml文件的所在的位置是src包而非day1012
3.在编程的开始时候,先导入jar包
jar包下载地址:
链接:https://pan.baidu.com/s/16_5Br3OTVcODXiMiXy8YGw
提取码:wr2y
1.准备接口
代码如下(示例):
package day1012;
public interface UserDao {
void insert();
void delect();
void update();
void find();
}
2.对应的实现类
代码如下(示例):
package day1012;
public class UserDaolmp implements UserDao{
@Override
public void insert() {
// TODO Auto-generated method stub
System.out.println("可以插入自己的业务");
}
@Override
public void delect() {
// TODO Auto-generated method stub
}
@Override
public void update() {
// TODO Auto-generated method stub
}
@Override
public void find() {
// TODO Auto-generated method stub
}
}
3.AuthorityAspect组装
package day1012;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class AuthorityAspect implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
startLog();
Object obj = arg0.proceed();
saveLog();
return null;
}
public void startLog() {
System.out.println("模拟开始日志");
}
public void saveLog() {
System.out.println("模拟保存日志");
}
}
4.TranactionAspect组装
注意:🧨🧨🧨
//spring-aop aopalliance-aop联盟接口规范包
Maven工程需准备
package day1012;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactoryBean;
public class TranactionAspect implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
beginTranaction();
Object obj = arg0.proceed();
commitTranaction();
// ProxyFactoryBean
return obj;
}
public void beginTranaction() {
System.out.println("模拟开启事务T");
}
public void commitTranaction() {
System.out.println("模拟提交事务T");
}
}
5.配置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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "userDao" class = "day1012.UserDaolmp">
</bean>
<bean id = "tranactionAspect" class = "day1012.TranactionAspect">
</bean>
<bean id = "userDaoProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
<property name = "proxyInterfaces" value = "day1012.UserDao"></property>
<property name = "target" ref = "userDao"></property>
<property name = "interceptorNames" value="tranactionAspect"></property>
<property name = "proxyTargetClass" value = "true"></property>
</bean>
</beans>
6.生成测试类
package day1012;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestProxy {
public static void main(String[] args) {
String file = "day1012procy.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(file);
UserDao user =ac.getBean("userDaoProxy",UserDao.class);
user.insert();
}
}
7.实验结果+分析
总结
今天学习了基于代理类的AOP的实现,同时做了一个小实验。
在这三天的SSM学习,我发现Spring的内容还是很多的,加油吧。今天的学习就到这里啦
END