目录
系列文章:
6. Spring实例参考06-setter注入的10种方式
11. Spring实例参考11-API实现AOP前置/后置通知
本文供以下文章参考使用:
1 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 头文件中引入了aop命名空间 -->
<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 https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自动创建单例 -->
<bean id="indexDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/>
<bean id="aspectLog" class="com.evenif.log.AspectLog"/>
<!-- 自动创建单例服务依赖注入已存在dao -->
<bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl">
<property name="indexDao" ref="indexDao"></property>
</bean>
<!-- 配置aop -->
<aop:config>
<!-- 定义“切点”
包含 所有返回类型 该包下的所有类 所有参数类型(包含无参)的方法
↓ ↓ ↓ ↓
execution( * com.evenif.service.impl.*.* (..) )
-->
<aop:pointcut id="pointcut" expression="execution(* com.evenif.service.impl.*.*(..))"/>
<!-- 指定通知类型pointcut和通知者类aspectLog -->
<aop:advisor advice-ref="aspectLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>2 自定义切面日志类
package com.evenif.log;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.aspectj.AspectJAroundAdvice;
import java.lang.reflect.Method;
import java.util.Arrays;
/*
* MethodBeforeAdvice: 前置通知接口
* AfterReturningAdvice:后置通知接口
* 这两个接口互相独立,如果只需要其中一个,只需要实现一个就行,也可以配置多个类分别实现这些接口,
* 笔者为了方便,直接使用一个日志类同时实现了这两个接口而已。
*/
public class AspectLog implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {
/**
* @param method 目标方法
* @param args 方法参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + "." + method.getName() + ": " + Arrays.toString(args) + "[BEFORE]");
}
/**
* @param returnValue 返回值
* @param method
* @param args
* @param target
* @throws Throwable
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(returnValue +" "+target.getClass().getName() + "." + method.getName() + ": " + Arrays.toString(args) + "[AFTER]");
}
public void afterThrowing(Method method,Exception ex) throws Throwable{
System.err.println(method.getName()+":"+ex);
}
}
3 bean
package com.evenif.bean;
public class Index {
private int id;
private String name;
public Index() {
// System.out.println("create Index class");
}
public Index(int id, String name) {
this.id = id;
this.name = name;
// System.out.println("create Index class with params");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Index{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
4 dao
package com.evenif.dao;
import com.evenif.bean.Index;
import java.util.List;
public interface IndexDao {
Index getById(int id);
void insert(Index index);
void update(Index index);
void delete(int id);
List<Index> loadAll();
}
5 daoImpl
package com.evenif.dao.impl;
import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import java.util.ArrayList;
import java.util.List;
public class IndexMysqlDaoImpl implements IndexDao {
@Override
public Index getById(int id) {
// 假装是从数据库获取的Index对象
return new Index(id, "mysqlIndex");
}
@Override
public void insert(Index index) {
System.out.println("mysql insert : " + index);
}
@Override
public void update(Index index) {
System.out.println("mysql update : " + index);
}
@Override
public void delete(int id) {
System.out.println("mysql delete index : " + id);
}
/**
* @return
*/
@Override
public List<Index> loadAll() {
List<Index> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add(new Index(i + 1, "mysql-" + i));
}
return list;
}
}
6 service
package com.evenif.service;
import com.evenif.bean.Index;
import java.util.List;
public interface IndexService {
Index getIndex(int id);
void addIndex(Index index);
void modifyIndex(Index index);
void deleteIndex(int id);
List<Index> loadIndexList();
}
7 serviceImpl
package com.evenif.service.impl;
import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import com.evenif.service.IndexService;
import java.util.List;
public class IndexServiceImpl implements IndexService {
private IndexDao indexDao;
public void setIndexDao(IndexDao indexDao) {
this.indexDao = indexDao;
}
public IndexServiceImpl() {
}
public IndexServiceImpl(IndexDao indexDao) {
this.indexDao = indexDao;
}
@Override
public Index getIndex(int id) {
return indexDao.getById(id);
}
@Override
public void addIndex(Index index) {
indexDao.insert(index);
}
@Override
public void modifyIndex(Index index) {
indexDao.update(index);
}
@Override
public void deleteIndex(int id) {
indexDao.delete(id);
}
@Override
public List<Index> loadIndexList() {
return indexDao.loadAll();
}
}
8 Main
package com.evenif;
import com.evenif.bean.Index;
import com.evenif.bean.Person;
import com.evenif.bean.User;
import com.evenif.service.IndexService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IndexService service = (IndexService) context.getBean("indexService");
System.out.println(service.getIndex(1));
Index index = new Index(2,"text02");
service.addIndex(index);
service.deleteIndex(index.getId());
service.modifyIndex(index);
service.loadIndexList();
}
}
//执行结果:
// com.evenif.service.impl.IndexServiceImpl.getIndex: [1][BEFORE]
// Index{id=1, name='mysqlIndex'} com.evenif.service.impl.IndexServiceImpl.getIndex: [1][AFTER]
// Index{id=1, name='mysqlIndex'}
// com.evenif.service.impl.IndexServiceImpl.addIndex: [Index{id=2, name='text02'}][BEFORE]
// mysql insert : Index{id=2, name='text02'}
// null com.evenif.service.impl.IndexServiceImpl.addIndex: [Index{id=2, name='text02'}][AFTER]
// com.evenif.service.impl.IndexServiceImpl.deleteIndex: [2][BEFORE]
// mysql delete index : 2
// null com.evenif.service.impl.IndexServiceImpl.deleteIndex: [2][AFTER]
// com.evenif.service.impl.IndexServiceImpl.modifyIndex: [Index{id=2, name='text02'}][BEFORE]
// mysql update : Index{id=2, name='text02'}
// null com.evenif.service.impl.IndexServiceImpl.modifyIndex: [Index{id=2, name='text02'}][AFTER]
// com.evenif.service.impl.IndexServiceImpl.loadIndexList: [][BEFORE]
// [Index{id=1, name='mysql-0'}, Index{id=2, name='mysql-1'}, Index{id=3, name='mysql-2'}] com.evenif.service.impl.IndexServiceImpl.loadIndexList: [][AFTER]
版权声明:本文为qq_34378496原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。