feat(aop): 添加公告管理功能并完善日志记录

- 新增 Announcement 实体类,包含公告的基本属性和状态类型定义
- 在 Spring 配置文件中注册公告 Bean 并设置初始数据
- 扩展日志切面功能,增加方法参数信息记录
- 更新测试类,集成公告服务的 AOP 切入点测试
- 实现公告信息的控制台打印和字符串表示方法
main
20987 1 month ago
parent fa57fc5b87
commit ffedbbfa25

@ -42,5 +42,18 @@
<property name="deptAddress" value="实验楼A座"/>
<property name="dean" value="李教授"/>
</bean>
<!-- 1. 配置公告实体类Bean -->
<bean id="announcement" class="com.ssm.aop.xml.Announcement">
<property name="id" value="1"/>
<property name="title" value="系统维护通知"/>
<property name="content" value="系统将于今晚22:00-24:00进行维护请提前保存数据。"/>
<property name="publisher" value="系统管理员"/>
<property name="publishTime" value="2025-04-21 10:30"/>
<property name="status" value="1"/> <!-- 1:已发布 -->
<property name="type" value="4"/> <!-- 4:系统 -->
<property name="expireDate" value="2025-04-30"/>
<property name="readCount" value="128"/>
</bean>
</beans>

@ -0,0 +1,171 @@
package com.ssm.aop.xml;
/**
*
*
*/
public class Announcement {
// 公告ID
private Integer id;
// 公告标题
private String title;
// 公告内容
private String content;
// 发布人
private String publisher;
// 发布时间改为String类型避免转换问题
private String publishTime;
// 公告状态0:草稿 1:已发布 2:已下架)
private Integer status;
// 公告类型1:通知 2:重要 3:活动 4:系统 5:紧急)
private Integer type;
// 截止日期改为String类型
private String expireDate;
// 阅读次数
private Integer readCount;
// 无参构造
public Announcement() {}
// 有参构造
public Announcement(Integer id, String title, String content, String publisher,
String publishTime, Integer status, Integer type,
String expireDate, Integer readCount) {
this.id = id;
this.title = title;
this.content = content;
this.publisher = publisher;
this.publishTime = publishTime;
this.status = status;
this.type = type;
this.expireDate = expireDate;
this.readCount = readCount;
}
// Getter & Setter 方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getExpireDate() {
return expireDate;
}
public void setExpireDate(String expireDate) {
this.expireDate = expireDate;
}
public Integer getReadCount() {
return readCount;
}
public void setReadCount(Integer readCount) {
this.readCount = readCount;
}
// 获取状态描述
public String getStatusDesc() {
switch (status) {
case 0: return "草稿";
case 1: return "已发布";
case 2: return "已下架";
default: return "未知";
}
}
// 获取类型描述
public String getTypeDesc() {
switch (type) {
case 1: return "通知";
case 2: return "重要";
case 3: return "活动";
case 4: return "系统";
case 5: return "紧急";
default: return "未知";
}
}
/**
* printInfo
*/
public void printInfo() {
System.out.println("===== 【公告管理】信息 =====");
System.out.println("公告ID : " + id);
System.out.println("公告标题 : " + title);
System.out.println("公告内容 : " + content);
System.out.println("发布人 : " + publisher);
System.out.println("发布时间 : " + publishTime);
System.out.println("状态 : " + getStatusDesc());
System.out.println("类型 : " + getTypeDesc());
System.out.println("截止日期 : " + expireDate);
System.out.println("阅读次数 : " + readCount);
System.out.println("==========================\n");
}
@Override
public String toString() {
return "Announcement{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", publisher='" + publisher + '\'' +
", publishTime='" + publishTime + '\'' +
", status=" + status + "(" + getStatusDesc() + ")" +
", type=" + type + "(" + getTypeDesc() + ")" +
", expireDate='" + expireDate + '\'' +
", readCount=" + readCount +
'}';
}
}

@ -9,6 +9,14 @@ public class Log {
String className = joinPoint.getTarget().getClass().getName();
// 获取目标方法名
String methodName = joinPoint.getSignature().getName();
// 获取方法参数
Object[] args = joinPoint.getArgs();
String argsInfo = "";
if (args != null && args.length > 0) {
argsInfo = ",参数:" + args[0];
}
System.out.println("前置通知:模拟日志记录...目标类是:" + className + ",被切入通知的目标方法为:" + methodName);
}
}

@ -8,10 +8,20 @@ public class Test {
// 加载Spring配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-aop-xml.xml");
// 获取Department的Bean实例
// 获取Student的Bean实例
Student student = (Student) ac.getBean("student");
Department department = (Department) ac.getBean("department");
// 调用打印方法AOP会自动切入前置通知
student.printInfo();
department.printInfo();
// 获取公告服务Bean实例
Announcement service = (Announcement) ac.getBean("announcement");
// 调用业务方法AOP会自动切入前置通知
System.out.println("========== 测试发布公告 ==========");
service.printInfo();
System.out.println("\n========== 测试获取公告详情 ==========");
}
}
Loading…
Cancel
Save