springAOP
静态代理:
代理模式:可以使真实角色的操作更加纯粹。不用关注公共业务,公共业务交给代理角色,实现业务的分工,公共业务发生扩展时,方便集中管理,
缺点:一个真实角色就会产生一个代理角色,
接口
public interface Rent { public void rent(); }
代理
public class Proxy implements Rent{ private Host host; public Proxy(Host host) { this.host = host; } public Proxy() { } @Override public void rent() { host.rent(); } public void seehouse(){ System.out.println("中间带你看房"); } public void hetong(){ System.out.println("签合同"); } public void fare(){ System.out.println("收中介费"); } }
真实房东
public class Host implements Rent{ @Override public void rent() { System.out.println("房主出租房子"); } }
动态代理:
实现了同一个借口哦即可
补充了静态代理的缺点,底层为反射
分为两大类:基于接口的动态代理(jdk动态代理),基于类的动态代理(cglib),java字节码实现:javasist
需要了解两个类:Proxy ,InvcoationHanle
public class ProxyHandle01 implements InvocationHandler { //被代理的接口 private Object object; public Object getObject() { return object; } public void setObject(Object object) { this.object = object; } //生成代理类 public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(), this .getClass().getInterfaces(),this); } //处理代理类,并返回结果 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = method.invoke(object, args); return result; } }
public class Client { public static void main(String[] args) { //真实角色 Host host=new Host(); //代理角色,现在没有 ProxyHanle ph = new ProxyHanle(); //通过调用程序处理角色来处理我们要调用的接口对象 ph.setRent(host); Rent proxy = (Rent) ph.getProxy(); proxy.rent(); } }
Aop
使用springim
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency>
方式1
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册bean --> <bean id="UserService" class="com.serivce.UserServiceImpl"></bean> <bean id="log" class="com.log.log"></bean> <bean id="afterlog" class="com.log.AfterLog"></bean> <!--方式1,使用原生的spring api接口--> <!--配置aop 需要导入aop的约束--> <aop:config> <!--切入点 express表达式 excution(要执行的位置 ! * * * * *)--> <aop:pointcut id="poinycut" expression="execution(* com.serivce.UserServiceImpl.*(..))"/> <!--执行环绕增加--> <aop:advisor advice-ref="log" pointcut-ref="poinycut"></aop:advisor> <aop:advisor advice-ref="afterlog" pointcut-ref="poinycut"></aop:advisor> </aop:config> </beans>
接口
public interface UserService { public void add(); public void delete(); public void update(); public void query(); }
实现类
public class UserServiceImpl implements UserService{ @Override public void add() { System.out.println("增加了一个用户"); } @Override public void delete() { System.out.println("删除了一个用户"); } @Override public void update() { System.out.println("修改了一个用户"); } @Override public void query() { System.out.println("查询了一个用户"); } }
前后日志
public class AfterLog implements AfterReturningAdvice { //returnValue 返回值 @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue); } }
public class log implements MethodBeforeAdvice { //method:要执行的目标对象的方法 //args: 参数 //target:目标对象 @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了"); } }
测试
public class Mytest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //动态代理的是接口 UserService userService =(UserService) context.getBean("UserService"); userService.add(); } }
方式2
使用自定义切入类
<bean id="UserService" class="com.serivce.UserServiceImpl"></bean> <bean id="log" class="com.log.log"></bean> <bean id="afterlog" class="com.log.AfterLog"></bean> <bean id="diy" class="com.diy.DiyPointCut"></bean> <aop:config> <!--自定义切面,ref要引用的类--> <aop:aspect ref="diy"> <!--切入点--> <aop:pointcut id="point" expression="execution(* com.serivce.UserServiceImpl.*(..))"/> <aop:before method="Before" pointcut-ref="point"></aop:before> <aop:after method="after" pointcut-ref="point"></aop:after> </aop:aspect> </aop:config>
public class DiyPointCut { public void Before(){ System.out.println("=====方法执行前====="); } public void after(){ System.out.println("------方法执行后-----"); } }
方式3
注解的方式
//使用注解 @Aspect//标注这个类是一个切面 public class AutoDiy { @Before("execution(* com.serivce.UserServiceImpl.*(..))") public void before(){ System.out.println("----方法执行前!----"); } }
<bean id="UserService" class="com.serivce.UserServiceImpl"></bean> <bean id="log" class="com.log.log"></bean> <bean id="afterlog" class="com.log.AfterLog"></bean> <bean id="AutoDiy" class="com.diy.AutoDiy"></bean> <!--开启注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
整合Mybatis
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!--spring 操作数据库--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> </dependencies>
回忆mybatis
1.编写实体类
2.编写核心配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 对实体类进行扫描--> <typeAliases> <package name="com.guo.pojo"/> </typeAliases> <!-- environments:配置数据库连接环境信息。可以配置多个environment,通过default属性切换不同的 environment --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--数据库连接信息--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///db2?useSSL=false"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.mapper.UserMapper"></mapper> </mappers> </configuration>
3.编写接口
4.编写Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mapper.UserMapper"> <select id="seleterUser" resultType="com.pojo.account"> select * from db2.account; </select> </mapper>
5.测试