Spring实例参考12-自定义类实现AOP

目录

前言 

 1 自定义日志类:

2 配置文件:

3 bean:

4 dao:

5 daoImpl

6 service

7 serviceImpl

8 Main:


系列文章:

Spring实例参考01-一个简单实例​​​​​​​

spring实例参考02-一个有基本框架雏形的实例

 Spring实例参考03-通过构造方法创建对象

Spring实例参考04-通过工厂创建对象

Spring实例参考05-导入其他文件:import

Spring实例参考06-setter注入的10种方式

Spring实例参考07-bean的作用域

Spring实例参考08-bean的自动装配

Spring实例参考09-静态代理

Spring实例参考10-动态代理

Spring实例参考11-API实现AOP前置/后置通知

Spring实例参考12-自定义类实现AOP

Spring实例参考13-注解实现AOP


前言 

本文供以下文章参考使用:
Spring基础回顾__evenif的博客-CSDN博客

 1 自定义日志类:

package com.evenif.log;

public class CustomLog {
    public void before(){
        System.out.println("custom aspect log before...");
    }
    public void after(){
        System.out.println("custom aspect log after...");
    }
}

2 配置文件:

<?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="customLog" class="com.evenif.log.CustomLog"/>
    <!-- 自动创建单例服务依赖注入已存在dao -->
    <bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl">
        <property name="indexDao" ref="indexDao"></property>
    </bean>
    <aop:config>
        <aop:aspect ref="customLog">
            <aop:pointcut id="pointcut" expression="execution(* com.evenif.service.impl.*.*(..))"/>
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

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();

    }
}
//执行结果:
// custom aspect log before...
//custom aspect log after...
//Index{id=1, name='mysqlIndex'}
//custom aspect log before...
//mysql insert : Index{id=2, name='text02'}
//custom aspect log after...
//custom aspect log before...
//mysql delete index : 2
//custom aspect log after...
//custom aspect log before...
//mysql update : Index{id=2, name='text02'}
//custom aspect log after...
//custom aspect log before...
//custom aspect log after...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_evenif

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值