编写了小组共用的 Log.java编写实体类 User.java测试类TestAop

main
wangcong 3 weeks ago
parent 7ae70ea3fa
commit d54cd94bf8

20
.gitignore vendored

@ -0,0 +1,20 @@
# 编译输出目录
out/
target/
build/
*.class
# IDE 配置
.idea/
*.iml
.vscode/
# 临时文件
*.log
*.tmp
*.swp
*~
# 操作系统文件
.DS_Store
Thumbs.db

@ -0,0 +1,32 @@
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 组长的User实体 -->
<bean id="user" class="com.ssm.aop.xml.User">
<property name="id" value="1"/>
<property name="username" value="zhangsan"/>
<property name="userpass" value="123456"/>
<property name="truename" value="张三"/>
<property name="phone" value="13800130000"/>
<property name="createTime" value="2024-01-01"/>
</bean>
<!-- AOP日志 -->
<bean id="log" class="com.ssm.aop.xml.Log"/>
<!-- AOP配置 -->
<aop:config>
<aop:aspect ref="log">
<aop:before method="before"
pointcut="execution(* com.ssm.aop.xml..printInfo(..))"/>
</aop:aspect>
</aop:config>
</beans>

@ -0,0 +1,13 @@
package com.ssm.aop.xml;
import org.aspectj.lang.JoinPoint;
import java.util.Date;
public class Log {
public void before(JoinPoint jp) {
System.out.println("==================================");
System.out.println("【前置通知】开始执行方法:" + jp.getSignature().getName());
System.out.println("【前置通知】执行时间:" + new Date());
System.out.println("==================================");
}
}

@ -0,0 +1,12 @@
package com.ssm.aop.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-aop.xml");
User user = (User) ac.getBean("user");
user.printInfo();
}
}

@ -0,0 +1,67 @@
package com.ssm.aop.xml;
public class User {
private Integer id;
private String username;
private String userpass;
private String truename;
private String phone;
private String createTime;
public void printInfo() {
System.out.println("===== 组长-用户信息 =====");
System.out.println("ID" + id);
System.out.println("用户名:" + username);
System.out.println("真实姓名:" + truename);
System.out.println("电话:" + phone);
System.out.println("注册时间:" + createTime);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public String getTruename() {
return truename;
}
public void setTruename(String truename) {
this.truename = truename;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}

@ -8,7 +8,7 @@ public class TestAnnotation {
// 加载注解配置
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-annotation.xml");
// 只获取组长自己的 User
User user = (User) ac.getBean("user");
user.printInfo();
StockAccount stockAccount = (StockAccount) ac.getBean("stockAccount");

Loading…
Cancel
Save