spring基于注解配置aop案例
本文内容纲要:
- spring基于注解配置aop案例- 1、导入jar包
- 2、编写实体类
- 3、编写业务层
- 4、编写持久层
- 5、编写通知类
- 6、配置文件
- 7、测试
- 8、不使用xml方式
spring基于注解配置aop案例
1、导入jar包
基于maven项目
org.aspectj aspectjweaver
1.8.14
2、编写实体类
package com.yl.bean;import java.io.Serializable;
/**
* 用户实体类
*/
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3、编写业务层
业务层接口
package com.yl.service;
/**
用户业务层接口
*/
public interface IUserService {
/**
- 查询用户
*/
public void queryUser();
- 查询用户
}
业务层接口实现类
package com.yl.service.impl;
import com.yl.dao.IUserDao;
import com.yl.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
用户业务层接口实现类
*/
@Service("userService")
public class IUserServiceImpl implements IUserService {
@Resource(name = "userDao")
private IUserDao iUserDao;//用户持久层对象
/**
- 查询用户
*/
@Override
public void queryUser() {
// int i=1/0;
iUserDao.queryUser();
}
}
- 查询用户
4、编写持久层
持久层接口
package com.yl.dao;
/**
用户持久层接口
*/
public interface IUserDao {
/**
- 查询用户
*/
public void queryUser();
- 查询用户
}
持久层接口实现类
package com.yl.dao.impl;
import com.yl.dao.IUserDao;
import org.springframework.stereotype.Repository;
/**
用户持久层接口实现类
*/
@Repository("userDao")
public class IUserDaoImpl implements IUserDao {
/**
- 查询用户
*/
@Override
public void queryUser() {
System.out.println("查询成功");
}
}
- 查询用户
5、编写通知类
package com.yl.inform;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* spring通知类
*/
@Component("userInform")
@Aspect//指定当前类为切面
public class UserInform {
/**
*配置通用切入点
*/
@Pointcut("execution(* com.yl.service.impl.IUserServiceImpl.*())")
private void pt1(){}
/**
* 前置通知
*/
@Before("pt1()")
public void beforeInform(){
System.out.println("开启事务");
}
/**
* 后置通知
*/
@AfterReturning("execution(* com.yl.service.impl.IUserServiceImpl.*())")
public void afterInform(){
System.out.println("提交事务");
}
/**
* 异常通知
*/
@AfterThrowing("execution(* com.yl.service.impl.IUserServiceImpl.*())")
public void throwInform(){
System.out.println("异常通知");
}
/**
* 环绕通知
*/
/*public void aroundInform(ProceedingJoinPoint pjp){
System.out.println("前置通知");
try {
pjp.proceed();
System.out.println("后置通知");
} catch (Throwable throwable) {
System.out.println("异常通知");
}finally {
System.out.println("最终通知");
}
}*/
}
6、配置文件
<?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"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--指定spring要扫描的包-->
<context:component-scan base-package="com.yl"></context:component-scan>
<!--开启spring对注解aop的支持-->
<aop:aspectj-autoproxy/>
</beans>
7、测试
package com.yl.ui;import com.yl.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
*/
public class MainTest {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
IUserService userService= (IUserService) applicationContext.getBean("userService");
userService.queryUser();
}
}
8、不使用xml方式
用配置类替代xml配置文件
@Configuration@ComponentScan(basePackages="com.itheima")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
本文内容总结:spring基于注解配置aop案例,1、导入jar包,2、编写实体类,3、编写业务层,4、编写持久层,5、编写通知类,6、配置文件,7、测试,8、不使用xml方式,
原文链接:https://www.cnblogs.com/Y-wee/p/13822397.html
以上是 spring基于注解配置aop案例 的全部内容, 来源链接: utcz.com/z/296232.html