AOP 操作的准备
1,Spring框架一般都是基于AspectJ实现AOP操作
什么是AspectJ?
AspectJ 不是Spring组成部分,独立Aop框架,一般把AspectJ 和Spring 框架一起使用,进行AOP操作
基于AspectJ实现AOP操作
1,基于xml配置文件实现
2,基于注解方式实现
3,在项目工程里引入AOP相关依赖
4,切入点表达式
1,切入点表达式作用?
知道对哪个类里的哪个方法进行增强
2,语法结构?
execution([权限修饰符][返回类型][类全路径][方法名称]([参数列表]))
进行通知的配置
1,在SPring 配置文件中,开启扫描注解
<?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"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启扫描注解-->
<context:component-scan base-package="test2"></context:component-scan>
<!-- 开启AspectJ 生抽代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
2,使用注解创建User 和UserProxy对象
3,在增强类上面添加注解@Aspect
package test2;
import org.springframework.stereotype.Component;
@Component
public class User {
public void add(){
System.out.println("原方法");
int a = 10/0;
}
}
=========================================
package test2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect //生成代理对象
public class UserProxy {
//添加通知类型注解
@Before(value = "execution(* test2.User.add(..))")
public void before(){
System.out.println("原方法之前的功能 before");
}
// 不管有没有异常都执行
@After(value = "execution(* test2.User.add(..))")
public void after(){
System.out.println("原方法后的功能 after");
}
//在方法返回值之后执行
// 有异常就不执行
@AfterReturning(value = "execution(* test2.User.add(..))")
public void AfterReturning(){
System.out.println("AfterReturning");
}
//异常通知
@AfterThrowing(value = "execution(* test2.User.add(..))")
public void AfterThrowing(){
System.out.println("原方法出现异常时的功能 AfterThrowing");
}
//在原方法之前之后都执行
@Around(value = "execution(* test2.User.add(..))")
public void Around(ProceedingJoinPoint prceedingJoinPoint) throws Throwable {
System.out.println("环绕之前 Around");
prceedingJoinPoint.proceed();
System.out.println("环绕之后 Around");
}
}
======================================================
4, 在 Spring 配置文件中开启生成代理对象
package test2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
// @Test
// public void test1(){
// ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
// User user = context.getBean("user",User.class);
// user.add();
// }
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
User user = context.getBean("user",User.class);
user.add();
}
}
/*
* 无异常情况
环绕之前 Around
原方法之前的功能 before
原方法
环绕之后 Around
原方法后的功能 after
AfterReturning
*/
/*
* 有异常情况
环绕之前 Around
原方法之前的功能 before
原方法
原方法后的功能 after
原方法出现异常时的功能 AfterThrowing
*/
相同的切入点抽取
@Pointcut(value = "execution(* test2.User.add(..))")
public void pointdemo(){}
@Before(value = "pointdemo()")
public void before(){
System.out.println("原方法之前的功能 before");
}
有多个增强类对同一个方法进行增强,设置增强类优先级
在增强类上面添加注解@order(数字类型值) ,数字类型值越小优先级越高
@Component
@Aspect
@Order(1)
public class User2Proxy {
@Before(value = "execution(* test2.User.add(..))")
public void before(){
System.out.println("order=1 before");
}
}
========================
@Component
@Aspect //生成代理对象
@Order(3)
public class UserProxy {
@Pointcut(value = "execution(* test2.User.add(..))")
public void pointdemo(){}
@Before(value = "pointdemo()")
public void before(){
System.out.println("原方法之前的功能 before");
}
基于配置方式:
<?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"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="test3.User"></bean>
<bean id="userProxy" class="test3.UserProxy"></bean>
<aop:config>
<aop:pointcut id="p" expression="execution(* test3.User.buy(..))"/>
<aop:aspect ref="userProxy">
<aop:before method="before" pointcut-ref="p"/>
</aop:aspect>
</aop:config>
</beans>
完全使用注解开发
jdbcTemplate
1,Spring 框架对jdbc进行封装,使用jdbcTemplate方便实现对数据库操作
2,准备工作
引入相关jar包
在spring配置文件配置数据库连接池
3,配置jdbcTemplate对象,注入DataSource
<?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"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="test3"></context:component-scan>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/catering_management"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
4,创建service,dao类,在dao注入jdbcTemplate对象
** dao类
=========================
package test3.dao.daoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import test3.entity.Grocers;
@Repository
public class GrocersDaoImpl {
@Autowired
private JdbcTemplate jdbcTemplate;
public void add(Grocers grocers){
String sql = "INSERT INTO grocers VALUES (?,?,?,?,?)";
jdbcTemplate.update(sql,grocers.getMerchants_id(),grocers.getMerchants_name(),grocers.getLegalperson_name(),grocers.getLegalperson_phone(),grocers.getState());
}
}
=====================
** service 类
=====================
package test3.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test3.dao.daoImpl.GrocersDaoImpl;
import test3.entity.Grocers;
@Service
public class GrocersService {
@Autowired
private GrocersDaoImpl grocersDao;
public void add(Grocers grocers){
grocersDao.add(grocers);
}
}
===============================
** 测试类
==================
package test3.test.Ch1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test3.dao.daoImpl.GrocersDaoImpl;
import test3.entity.Grocers;
public class Ch1 {
public static void main(String[] args) {
Grocers grocers = new Grocers(6,"2","4","5","2");
ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
//名称首字母小写
GrocersDaoImpl grocersDaoImpl = context.getBean("grocersDaoImpl", GrocersDaoImpl.class);
grocersDaoImpl.add(grocers);
}
}
======================================