Spring AOP面向切面编程实现及配置详解

网友投稿 272 2022-11-22


Spring AOP面向切面编程实现及配置详解

动态代理

特点

字节码随用随创建,随用随加载

作用

不用修改源码对方法增强

分类

基于接口的动态代理

基于子类的动态代理

创建

使用Proxy类中的newProxyInstance方法

要求

被代理类最少实现一个接口,没有则不能使用

newProxyInstance方法参数

classLoader:类加载器

用于加载代理对象字节码的,和被代理对象使用相同的类加载器

class[ ]:字节码数组

用于让代理对象和被代理对象有相同方法,固定写法。

InvocationHandler:用于提供增强的代码

是让我们写如何代理。一般都是写一个该接口的实现类,通常情况下都是匿名内部类,不是必须的

此接口的实现类都是谁用谁写

IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),

producer.getClass().getInterfaces(),

new InvocationHandler(){

作用:执行被代理对象的任何接口方法都会经过该方法

* proxy 代理对象的引用

* method 当前执行的方法

* args 执行当前方法所需的参数

* return 和被代理对象有相同的返回值

@override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{

// 提供增强的代码

Object returnValue = null

1. 获取方法执行的参数

Float money = (Float)args[0]

2. 判断当前方法是否为指定方法

if("saleProduct".equals(method.getName())){

returnValue = method.invoke(producer,money*0.8)

}

return returnValue;

}

}

)

//代理方法调用的是上面invoke中的方法

proxyProducer.saleProduct(100000)

注意 如果代理的类没有接口,则代理不可用。

AOPxml配置

连接点Joinpoint:指那些被拦截的点,在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。

切入点Pointcut:所谓切入点指的是要对哪些Joinpoint进行拦截的定义。方法会被增强。

所有的切入点都是连接点,但不是所有的连接点都是切入点。

通知Advice:指拦截到Joinpoint之后所要做的事情

在invoke方法里的,有前置通知,后置通知,异常通知,最终通知

引入Introduction

目标对象Target :即被代理的对象

织入Weaving:把增强应用到目标对象来创建新的代理对象的过程。Spring采用动态代理织入。

创建接口类,实现类

创建aop通知功能函数

xml配置

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/beans https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop https://springframework.org/schema/aop/spring-aop.xsd"&ghttp://t;

// 通知类型

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="

http://springframework.org/schema/beans https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop https://springframework.org/schema/aop/spring-aop.xsd"&ghttp://t;

// 通知类型

AOPxml注解

aop注解配置

/**

* 记录日志的工具类,提供了公共的代码

*/

@Component("logger")

@Aspect // 表示当前类是一个切面

public class Logger {

@Pointcut("execution()")

private void pt1(){}

/**

* 用于打印日志:计划在其切入点方法执行前执行(切入点方法就是业务层方法)

*/

@Before(pt1())

public void beforePrintLog() {

System.out.println("前置");

}

public void afterPrintLog() {

System.out.println("后置");

}

public void afterThrowingPringLog() {

System.out.println("异常");

}

public void finalPrintLog() {

System.out.println("最终");

}

// 环绕通知为我们提供了ProceedingJoinPoint,有一个方法proceed(),此方法就明确了调用切入点方法

// 为我们提供了一种可以在代码中手动控制增强方法合适执行的方式

public Object aroundPrintLog(ProceedingJoinPoint pjp) {

Object returnValue = null;

try {

Object[] args = pjp.getArgs(); // 得到方法执行所需参数

System.sWCTJBIciout.println("前置");

returnValue = pjp.proceed(args); // 明确调用业务层的方法

System.out.println("后置");

} catch (Throwable throwable) {

// throwable.printStackTrace();

System.out.println("异常");

} finally {

System.out.println("最终");

}

return returnValue;

// System.out.println("环绕通知");

}

}

xml:

配置spring创建容器要扫描的包

注意 如果用注解自带的调用顺序会出现问题,用环绕通知顺序正常

事务控制

导包

org.springframework

spring-tx

5.2.4.RELEASE

事务管理器:org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager

在bean.xml中配置

1. 配置事物管理器

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd">

2.配置事物的通知

5.配置事物的属性

isolation:指定事物的隔离级别,默认值是default,表示使用数据库的默认隔离级别

propagation:用于指定事物的传播行为,默认是REQUIRED,表示一定会有事物,增删改的选择,查询可以使用support

read-only:用于指定事物是否只读,查询才设置为true

timeout:用于指定事物的超市时间,默认值是-1,表示不超时,如果指定了数值,以秒为单位

rollback-for:用于指定一个异常,当产生该异常时事物回滚,产生其他异常时,事物不回滚。没有默认值,表示任何异常都回滚

no-rollback-for:用于指定一个异常,当产生该异常,事务不会回滚,产生其他异常,事务回滚。没有默认值,表示任何异常都回滚。

3.配置aop切入点表达式

4. 建立切入点表达式喝事物通知的对应关系

基于注解的事务控制

1. 配置事物管理器

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd"

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

3. 配置spring创建容器时要扫描的包

4. 开启spring对注解事物的支持

6. 在需要事物支持的地方使用注解@Transactional

2.在实现类中

@Service(accountService)

@Transactional

public class 实现类 implements 接口类{

@Autowired

// 在持久层也要配置

private IaccountDao accountDao

}

基于注解的配置类

1.创建一个配置总配置类

@Configuration

// 用于配置需要扫描的包

@ComponentScan("hjj.web")

@Import({HibernateConfig.class, TransactionConfig.class})

@PropertySource("hibernateConfig.properties")

@EnableTransactionManagement //开启注解的支持

public class SpringConfiguration{

}

2.另一个java类,连接数据库相关的类

publci class HibernateConfig{

@Value("${hibernate.username}")

private String username;

@Value("${hibernate.password}")

private String password

// 注入进容器

@Bean(name="HibernateTemplate")

public Hibernate crateHibernateTemplate(DataSource datasource){

return new HibernateTemplate(dataSource)

}

@Bean(name="dataSource")

public DataSource crateDataSource(){

配置数据库的用户名密码 创建数据源对象

}

}

3. 新建一个properties,配置文件类

hibernate.username =

hibernate.password =

4. 创建和事物相关的配置类

public class TransactionConfig {

//创建事务管理器对象

@Bean(name="transactionManager")

public PlatformTransactionManager createTransactionManager(DataSource dataSource){

return new DataSourceTransactionManager(dataSource)

}

}

5. mahttp://in方法所在的类

@ContextConfiguration(classes=SpringConfiguration.class)

public class test{

psvm{

业务逻辑

}

}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd">

2.配置事物的通知

5.配置事物的属性

isolation:指定事物的隔离级别,默认值是default,表示使用数据库的默认隔离级别

propagation:用于指定事物的传播行为,默认是REQUIRED,表示一定会有事物,增删改的选择,查询可以使用support

read-only:用于指定事物是否只读,查询才设置为true

timeout:用于指定事物的超市时间,默认值是-1,表示不超时,如果指定了数值,以秒为单位

rollback-for:用于指定一个异常,当产生该异常时事物回滚,产生其他异常时,事物不回滚。没有默认值,表示任何异常都回滚

no-rollback-for:用于指定一个异常,当产生该异常,事务不会回滚,产生其他异常,事务回滚。没有默认值,表示任何异常都回滚。

3.配置aop切入点表达式

4. 建立切入点表达式喝事物通知的对应关系

基于注解的事务控制

1. 配置事物管理器

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd"

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

3. 配置spring创建容器时要扫描的包

4. 开启spring对注解事物的支持

6. 在需要事物支持的地方使用注解@Transactional

2.在实现类中

@Service(accountService)

@Transactional

public class 实现类 implements 接口类{

@Autowired

// 在持久层也要配置

private IaccountDao accountDao

}

基于注解的配置类

1.创建一个配置总配置类

@Configuration

// 用于配置需要扫描的包

@ComponentScan("hjj.web")

@Import({HibernateConfig.class, TransactionConfig.class})

@PropertySource("hibernateConfig.properties")

@EnableTransactionManagement //开启注解的支持

public class SpringConfiguration{

}

2.另一个java类,连接数据库相关的类

publci class HibernateConfig{

@Value("${hibernate.username}")

private String username;

@Value("${hibernate.password}")

private String password

// 注入进容器

@Bean(name="HibernateTemplate")

public Hibernate crateHibernateTemplate(DataSource datasource){

return new HibernateTemplate(dataSource)

}

@Bean(name="dataSource")

public DataSource crateDataSource(){

配置数据库的用户名密码 创建数据源对象

}

}

3. 新建一个properties,配置文件类

hibernate.username =

hibernate.password =

4. 创建和事物相关的配置类

public class TransactionConfig {

//创建事务管理器对象

@Bean(name="transactionManager")

public PlatformTransactionManager createTransactionManager(DataSource dataSource){

return new DataSourceTransactionManager(dataSource)

}

}

5. mahttp://in方法所在的类

@ContextConfiguration(classes=SpringConfiguration.class)

public class test{

psvm{

业务逻辑

}

}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd"

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

3. 配置spring创建容器时要扫描的包

4. 开启spring对注解事物的支持

6. 在需要事物支持的地方使用注解@Transactional

2.在实现类中

@Service(accountService)

@Transactional

public class 实现类 implements 接口类{

@Autowired

// 在持久层也要配置

private IaccountDao accountDao

}

基于注解的配置类

1.创建一个配置总配置类

@Configuration

// 用于配置需要扫描的包

@ComponentScan("hjj.web")

@Import({HibernateConfig.class, TransactionConfig.class})

@PropertySource("hibernateConfig.properties")

@EnableTransactionManagement //开启注解的支持

public class SpringConfiguration{

}

2.另一个java类,连接数据库相关的类

publci class HibernateConfig{

@Value("${hibernate.username}")

private String username;

@Value("${hibernate.password}")

private String password

// 注入进容器

@Bean(name="HibernateTemplate")

public Hibernate crateHibernateTemplate(DataSource datasource){

return new HibernateTemplate(dataSource)

}

@Bean(name="dataSource")

public DataSource crateDataSource(){

配置数据库的用户名密码 创建数据源对象

}

}

3. 新建一个properties,配置文件类

hibernate.username =

hibernate.password =

4. 创建和事物相关的配置类

public class TransactionConfig {

//创建事务管理器对象

@Bean(name="transactionManager")

public PlatformTransactionManager createTransactionManager(DataSource dataSource){

return new DataSourceTransactionManager(dataSource)

}

}

5. mahttp://in方法所在的类

@ContextConfiguration(classes=SpringConfiguration.class)

public class test{

psvm{

业务逻辑

}

}

6. 在需要事物支持的地方使用注解@Transactional

2.在实现类中

@Service(accountService)

@Transactional

public class 实现类 implements 接口类{

@Autowired

// 在持久层也要配置

private IaccountDao accountDao

}

基于注解的配置类

1.创建一个配置总配置类

@Configuration

// 用于配置需要扫描的包

@ComponentScan("hjj.web")

@Import({HibernateConfig.class, TransactionConfig.class})

@PropertySource("hibernateConfig.properties")

@EnableTransactionManagement //开启注解的支持

public class SpringConfiguration{

}

2.另一个java类,连接数据库相关的类

publci class HibernateConfig{

@Value("${hibernate.username}")

private String username;

@Value("${hibernate.password}")

private String password

// 注入进容器

@Bean(name="HibernateTemplate")

public Hibernate crateHibernateTemplate(DataSource datasource){

return new HibernateTemplate(dataSource)

}

@Bean(name="dataSource")

public DataSource crateDataSource(){

配置数据库的用户名密码 创建数据源对象

}

}

3. 新建一个properties,配置文件类

hibernate.username =

hibernate.password =

4. 创建和事物相关的配置类

public class TransactionConfig {

//创建事务管理器对象

@Bean(name="transactionManager")

public PlatformTransactionManager createTransactionManager(DataSource dataSource){

return new DataSourceTransactionManager(dataSource)

}

}

5. mahttp://in方法所在的类

@ContextConfiguration(classes=SpringConfiguration.class)

public class test{

psvm{

业务逻辑

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot中实现启动任务的实现步骤
下一篇:SpringBoot项目Jar包如何瘦身部署的实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~