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