手写框架项目:实现基本的crud和自定义aop切面功能

使用自己手写的框架来实现一个简单的图书管理项目

1.项目架构

跟正常使用的springboot项目很像,这里就不多解释了。

引入依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <name>demo</name>
    <groupId>com.cg</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
<!--        引入mvc和mybatis-->

        <dependency>
            <groupId>com.cg</groupId>
            <artifactId>spring-mybatis-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>utf-8</encoding>
                    <!--                    运行时获取方法参数名-->
                    <compilerArgument>-parameters</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2. 功能测试

2.1 增删改查和页面渲染

 

2.2 环绕日志 

@Component
@Aspect
public class LogAspect {
    @PointCut("public:com.cg.demo.controller.*")
    public void pointcut() {

    }

    @Before
    public void before(ProceedingJoinPoint joinPoint) {
        String controller = joinPoint.getBeanName();
        Method method = joinPoint.getMethod();
        String methodName = method.getName();
        System.out.println(controller + "的" + methodName + "方法收到请求");
    }

    @After
    public void after(ProceedingJoinPoint joinPoint) {
        String controller = joinPoint.getBeanName();
        Method method = joinPoint.getMethod();
        String methodName = method.getName();
        System.out.println(controller + "的" + methodName + "方法结束请求");

    }
}

 2.3 权限管理

@Aspect
@Component
public class HasRoleAspect {
    @PointCut("annotation:com.cg.demo.annotation.HasRole")
    public void pointcut() {

    }

    @Before
    public void before(ProceedingJoinPoint joinPoint) {
        Method method = joinPoint.getMethod();
        HasRole hasRole = method.getAnnotation(HasRole.class);
        if (hasRole == null) return;
        HttpRequest request = (HttpRequest) RequestContext.getHttpRequest();
        String username = request.getCookie("username");
        if (!hasRole.value().equals(username)) {
            throw new RuntimeException("权限不够");
        }
    }
}

用户是111的时候无法执行权限操作

更多截图就不放了,感兴趣的可以去源码地址中查看 

3.项目地址

手写框架

4.总结

        预期目标已经完成了,本专栏也差不多告一段落了。写项目的时候遇到了各种各样的问题,需要反复去框架代码中进行修改,所以之前的博客代码难免会有不同的地方。不断遇到问题不断解决问题,最终实现了这样一个简单的项目,当然,还有很多的功能没有实现,也有很多地方的设计有缺陷,不过收获也是满满的,成就感也满满,感谢大家的观看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值