AOP的初识

1.AOP中的几个概念

1.1概念

1.连接点:类里面那些方法可以被增强,这些方法成为连接点

2.切入点:实际被增强的方法,称为切入点

3.通知:实际增强的逻辑部分称为通知(增强)

        类型:前置通知(@Befor)、后置通知(@After)、环绕通知(@Around)、异常通知(@AfterThrowing)、最终通知(@AfterReturning) 

4.切面:把通知应用到切入点过程

1.2引入依赖

<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.5</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.5</version>
</dependency>

<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.4.1</version>
<scope>compile</scope>
</dependency>

 代理的本质:

i:业务层

package com.openlab.Service;

public interface CarService {
    public int togo(int pric);
}

 ii:实现层

package com.openlab.Service.Impl;

import com.openlab.Service.CarService;

public class CarServiceImpl implements com.openlab.Service.CarService {
    @Override
    public int togo(int pric) {
        System.out.println("car的实现方法执行了。。。");
        return 0;
    }
}

 iii:代理层(秘书)

package com.openlab.MyProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyProxy implements InvocationHandler {
    private Object obj;
    public MyProxy(Object obj){
        this.obj=obj;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法执行前。。。");
        Object ret=method.invoke(obj,args);
        System.out.println("方法执行后。。。。");
        return ret;
    }
}

 代理层需要实现InvocationHandler接口,应为在最后对象执行方法时。本质上是调用了该接口的invoke方法

测试

@org.junit.Test
public void Test05(){
    CarServiceImpl carimpl=new CarServiceImpl();
    Class[] classes=carimpl.getClass().getInterfaces();
    CarService carService=(CarService) Proxy.newProxyInstance(Test.class.getClassLoader(),classes, new MyProxy(carimpl));
    int ss=carService.togo(500);
    System.out.println(ss);
}

 最终结果:

1.3xml文件

xml文件中,我们可以使用注解来减少代码量,也可在xml文件中进行bean

注解方法:

<context:component-scan base-package="com.openlab.aoptest"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

bean:

<bean id="proxy" class="com.openlab.aoptest.Proxy.Myproxy"></bean>
<aop:config>
    <aop:pointcut id="poid" expression="execution(* com.openlab.aoptest.service.impl.UserServiceImpl.*(..))"/>
    <aop:aspect ref="proxy">
        <aop:before method="Befor" pointcut-ref="poid"></aop:before>
    </aop:aspect>
</aop:config>

1.4代理类(秘书):(代理类中我们也可以使用@Pointcut(value="exectuion(返回值类型 需要增强类的完全地址.方法名/*.参数类型/*(..))     )

package com.openlab.aoptest.Proxy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Myproxy {
//    @Pointcut(value = "execution(* com.openlab.aoptest.service.impl.*.*(..))")
    @Before(value = "execution(* com.openlab.aoptest.service.impl.*.*(..))")
    public void Befor(){
        System.out.println("方法执行前。。。");
    }
    @After(value = "execution(* com.openlab.aoptest.service.impl.*.*(..))")
    public void After(){
        System.out.println("方法执行后。。。");

    }
    @AfterReturning(value = "execution(* com.openlab.aoptest.service.impl.*.*(..))")
    public void Aferturn(){
        System.out.println("方法执行最终返回。。。");
    }
    @AfterThrowing(value = "execution(* com.openlab.aoptest.service.impl.*.*(..))")
    public void thr(){
        System.out.println("异常抛出执行、、、、");
    }

    @Around(value = "execution(* com.openlab.aoptest.service.impl.*.*(..))")
    public void round(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕执行前、、、");
        joinPoint.proceed();
        System.out.println("环绕执行后。。。。。");
    }

}

1.5被代理的类(老板):

package com.openlab.aoptest.service.impl;

import lombok.Data;
import org.springframework.stereotype.Component;

@Data
@Component
public class UserServiceImpl{
    public String show() {
        System.out.println("show方法执行了、、、");
        return null;
    }
}

测试类

@org.junit.Test
public void Test06(){
    ApplicationContext context= new ClassPathXmlApplicationContext("aop.xml");
    UserServiceImpl userService=context.getBean(UserServiceImpl.class);
    userService.show();
}

最终结果

2.jdbcTemplate的使用

2.1引入依赖

  <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-jdbc</artifactId>

            <version>5.3.5</version>

        </dependency>

        <!--Spring事物依赖 -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-tx</artifactId>

            <version>5.3.5</version> 

        </dependency>

 2.2sml文件的配置

<?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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


    <context:property-placeholder location="classpath:druid.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="password" value="${password}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${name}"></property>
    </bean>
<!--以上为连接数据库-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <context:component-scan base-package="com.openlab"></context:component-scan>
</beans>

2.3.创建实体类

package com.openlab.pojo;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer roleId;
    private String iconURL;
}

2.4dao层的编写

package com.openlab.Dao;

import com.openlab.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class Templet {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void insert(User user){
        jdbcTemplate.update("INSERT into user VALUES(?,?,?,?,?)",user.getId(),user.getUsername(),user.getPassword(),user.getRoleId(),user.getIconURL());

    }
}

测试类:

import com.openlab.Dao.Templet;
import com.openlab.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JdbcTemplet {
    @Test
    public void jdbctest(){
        User user=new User(00012,"小尚","666666",1,"null");
        ApplicationContext context=new ClassPathXmlApplicationContext("Beans01.xml");
        Templet templet=context.getBean(Templet.class);
        templet.insert(user);
    }
}

 最终结果:查看数据库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值