Spring对DAO层和Service层的加强

Spring对DAO的增强体现在Dao对数据库的操作上(通俗的讲就是使用一个jdbc的模板) Spring对Service层的增强体现在对事务的管理上,通过aop的思想将事务很好的整合了一.Spring的AOP的注解入门(一) AOP对DAO的增强1. 创建web项目,引入jar包2. 创建相关的包和类创建包com.cdut.aop.demo1,创建接口...
摘要由CSDN通过智能技术生成

Spring对DAO的增强体现在Dao对数据库的操作上(通俗的讲就是使用一个jdbc的模板)
Spring对Service层的增强体现在对事务的管理上,通过aop的思想将事务很好的整合了

一.Spring的AOP的注解入门

(一) AOP对DAO的增强

1. 创建web项目,引入jar包
2. 创建相关的包和类
  • 创建包com.cdut.aop.demo1,创建接口:
public interface CustomerDao {

    public void save();
    public void update();
    public void find();
    public Integer delete();

}
  • 创建类CustomerDaoImpl.java
public class CustomerDaoImpl implements CustomerDao {
   

    @Override
    public void save() {
        System.out.println("这是一个=保存=客户的方法");
    }

    @Override
    public void update() {
        System.out.println("这是一个=更新=客户的方法");
    }

    @Override
    public void find() {
        System.out.println("这是一个=查找=客户的方法");
    }

    @Override
    public Integer delete() {
        System.out.println("这是一个=删除=客户的方法");
        return null;
    }

}
3. 编写切面类,并且完成配置
@Aspect
public class MyAspectAnno {
   

    //方式一:在自定义的切面类里面讲通知和切点绑定
    @Before("MyAspectAnno.pointcut1() || MyAspectAnno.pointcut2()")
    public void before(){
        System.out.println("this is 前置");
    }

    @Pointcut("execution(* com.cdut.spring.demon.CustomerDaoImpl.save(..))")
    private void pointcut1(){};

    @Pointcut("execution(* com.cdut.spring.demon.CustomerDaoImpl.update(..))")
    private void pointcut2(){};


    //方式二: 直接在通知上织入
    @After("execution(* com.cdut.spring.demon.CustomerDaoImpl.find(..)))")
    public void after(){
        System.out.println("这个是后置的增强");
    }
}
4. 创建核心配置文件,配置实现类

src创建applicationContext.xml

引入约束(模板)
<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    </beans>
     <!--开启自动代理  -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!--配置目标类: 其实就是CustomerDaoImpl -->
    <bean id="customerDao" class="com.cdut.spring.demon.CustomerDaoImpl"> </bean>

    <!-- 配置切面类 -->
    <bean id="myAspectAnno" class="com.cdut.spring.demon.MyAspectAnno"></bean>
    </beans>
5.编写测试类

注意Spring和JUnit的整合

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demon {
   

    //注入一个CustomerDao对象
    @Resource(name="customerDao")
    private CustomerDao customerDao;

    @Test
    public void test01(){
        customerDao.find();
        customerDao.save();
        customerDao.update();
        customerDao.delete();
    }
}
6. 输出结果显示
这是一个=查找=客户的方法
这个是后置的增强
this is 前置
这是一个=保存=客户的方法
this is 前置
这是一个=更新=客户的方法
这是一个=删除=客户的方法
7.总结
  • 这个小案例是使用Spring的AOP思想对DAO的一种增强.

    • Spring的AOP思想就是代理模式的体现–横向编程
  • 流程分析:

    • 首先是编写基本的类,注意在编写切面类的是后需要将通知和切点进行绑定

    • 第一种方式:先将通知和一个自定义的切点的绑定, 再将自定义的切点和连接点进行绑定

    • 第二种方式:一步到位 直接在通知上面将连接点绑定 不用去自定义一个切点了

    • 然后在配置文件中开启自动代理. 将目标类切面类配置进去

    • 最后编写一个测试类进行测试

(二) Spring的AOP的注解通知

1 其他通知的使用
  • 前置通知、后置通知、环绕通知、异常通知、后置通知
@Aspect
public class MyAspectAnno {

    @Before("execution(* com.cdut.spring.demon.CustomerDaoImpl.save(..))")
    public void before(JoinPoint joinpoint){
        System.out.println("前置增强==========");
    }

    @AfterReturning(value="execution(* com.cdut.spring.demon.CustomerDaoImpl.delete(..))",returning="result")
    public void afterReturning(JoinPoint joinpoint,Object result){
        System.out.println("后置增强=========="+result);
    }

    @AfterThrowing(value="execution(* com.cdut.spring.demon.CustomerDaoImpl.find(..))",throwing="ex")
    public void afterThrowing(JoinPoint joinpoint,Throwable ex){
        System.out.println("异常抛出增强======"+ex.getMessage());
    }

    @After(value="execution(* com.cdut.spring.demon.CustomerDaoImpl.find(..))")
    public void after(JoinPoint joinpoint){
        System.out.println("最终通知========");
    }


    @Around(value="execution(* com.cdut.spring.demon.CustomerDaoImpl.update(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 Spring 框架编写的学生管理代码,完成了您的要求: 1. 控制台输出学生信息 ```java public class Student { private String name; private String id; private int age; private String hobby; // 构造方法和 getter/setter 略 public void introduce() { System.out.println("你好!我是" + name + ",我的学号是" + id + ",我的年龄是" + age + "。"); } } ``` 2. 在业务和数据访问输出信息 ```java public interface StudentDao { void save(Student student); void delete(String id); void update(Student student); Student getById(String id); } public class StudentDaoImpl implements StudentDao { @Override public void save(Student student) { System.out.println("这是 dao ,正在执行 save 方法。"); // 保存学生信息到数据库 } @Override public void delete(String id) { System.out.println("这是 dao ,正在执行 delete 方法。"); // 从数据库删除学生信息 } @Override public void update(Student student) { System.out.println("这是 dao ,正在执行 update 方法。"); // 更新数据库中的学生信息 } @Override public Student getById(String id) { System.out.println("这是 dao ,正在执行 getById 方法。"); // 从数据库中获取指定学号的学生信息 return null; } } public interface StudentService { void add(Student student); void delete(String id); void update(Student student); Student getById(String id); } public class StudentServiceImpl implements StudentService { private StudentDao dao; public void setDao(StudentDao dao) { this.dao = dao; } @Override public void add(Student student) { System.out.println("这是 service ,正在执行 add 方法。"); dao.save(student); } @Override public void delete(String id) { System.out.println("这是 service ,正在执行 delete 方法。"); dao.delete(id); } @Override public void update(Student student) { System.out.println("这是 service ,正在执行 update 方法。"); dao.update(student); } @Override public Student getById(String id) { System.out.println("这是 service ,正在执行 getById 方法。"); return dao.getById(id); } } ``` 3. 配置增强 ```xml <bean id="student" class="com.example.Student"> <property name="name" value="张三"/> <property name="id" value="001"/> <property name="age" value="20"/> <property name="hobby" value="篮球"/> </bean> <bean id="studentDao" class="com.example.StudentDaoImpl"/> <bean id="studentService" class="com.example.StudentServiceImpl"> <property name="dao" ref="studentDao"/> </bean> <bean id="logAspect" class="com.example.LogAspect"/> <aop:config> <aop:aspect id="logAspect" ref="logAspect"> <aop:before method="before" pointcut="execution(* com.example.Student.introduce())"/> <aop:after method="after" pointcut="execution(* com.example.StudentService.*(..))"/> <aop:after-returning method="afterReturning" pointcut="execution(* com.example.StudentDao.*(..))" returning="result"/> <aop:around method="around" pointcut="execution(* com.example.StudentService.*(..))"/> </aop:aspect> </aop:config> ``` ```java public class LogAspect { public void before() { System.out.println("前置通知:准备介绍学生信息。"); } public void after() { System.out.println("后置通知:学生信息介绍完毕。"); } public void afterReturning(Object result) { System.out.println("返回通知:学生信息已保存到数据库。"); } public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕通知:开始执行 " + joinPoint.getSignature().getName() + " 方法。"); Object result = joinPoint.proceed(); System.out.println("环绕通知:执行 " + joinPoint.getSignature().getName() + " 方法结束。"); return result; } } ``` 以上代码实现了使用 Spring 框架进行学生管理,并且增强了相关方法的输出信息。其中,控制台输出了学生信息和前置通知,业务输出了爱好和后置通知,数据访问输出了执行方法和返回通知,环绕通知则对上述方法进行了增强

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值