实现电影管理模块AOP前置通知,完成切面配置和测试

main
crush 1 month ago
parent df7fb6da91
commit 4b2fa736bc

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="com.springsource.org.aopalliance-1.0.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/com.springsource.org.aopalliance-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="com.springsource.org.aspectj.weaver-1.6.8.RELEASE">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="commons-logging-1.1.1 (2)">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/commons-logging-1.1.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="spring-aop-5.1.6.RELEASE">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/spring-aop-5.1.6.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="spring-aspects-5.1.6.RELEASE">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/spring-aspects-5.1.6.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="spring-beans-5.1.6.RELEASE">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/spring-beans-5.1.6.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="spring-context-5.1.6.RELEASE">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/spring-context-5.1.6.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="spring-core-5.1.6.RELEASE">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/spring-core-5.1.6.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="spring-expression-5.1.6.RELEASE">
<CLASSES>
<root url="jar://$PROJECT_DIR$/film-SpringProject/src/lib/spring-expression-5.1.6.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -9,5 +9,14 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib1" level="project" />
<orderEntry type="library" name="lib" level="project" />
<orderEntry type="library" name="com.springsource.org.aopalliance-1.0.0" level="project" />
<orderEntry type="library" name="commons-logging-1.1.1 (2)" level="project" />
<orderEntry type="library" name="com.springsource.org.aspectj.weaver-1.6.8.RELEASE" level="project" />
<orderEntry type="library" name="spring-aop-5.1.6.RELEASE" level="project" />
<orderEntry type="library" name="spring-aspects-5.1.6.RELEASE" level="project" />
<orderEntry type="library" name="spring-beans-5.1.6.RELEASE" level="project" />
<orderEntry type="library" name="spring-context-5.1.6.RELEASE" level="project" />
<orderEntry type="library" name="spring-core-5.1.6.RELEASE" level="project" />
<orderEntry type="library" name="spring-expression-5.1.6.RELEASE" level="project" />
</component>
</module>

@ -0,0 +1,29 @@
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 启用组件扫描扫描com.ssm.aop.xml包下的注解 -->
<context:component-scan base-package="com.ssm.aop.xml"/>
<!-- 启用AOP自动代理 -->
<aop:aspectj-autoproxy/>
<!-- 配置电影管理模块的Bean采用setter方式注入属性 -->
<bean id="dyguanli" class="com.ssm.aop.xml.dyguanli">
<property name="movieName" value="流浪地球"/>
<property name="director" value="郭帆"/>
<property name="actor" value="吴京"/>
<property name="releaseDate" value="2019-02-05"/>
<property name="duration" value="125"/>
<property name="genre" value="科幻"/>
</bean>
</beans>

@ -0,0 +1,32 @@
package com.ssm.aop.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
*
*
*/
@Aspect
@Component
public class Log {
/**
*
* com.ssm.aop.xmlprintInfo
*/
@Before("execution(* com.ssm.aop.xml.*.printInfo(..))")
public void beforePrintInfo(JoinPoint joinPoint) {
// 获取目标类名
String className = joinPoint.getTarget().getClass().getSimpleName();
// 获取方法名
String methodName = joinPoint.getSignature().getName();
System.out.println("========== [前置通知] ==========");
System.out.println("即将执行方法: " + className + "." + methodName);
System.out.println("时间: " + new java.util.Date());
System.out.println("================================");
}
}

@ -0,0 +1,24 @@
package com.ssm.aop.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* - AOP
*
*/
public class Test {
public static void main(String[] args) {
// 1. 初始化Spring容器加载AOP配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("aop-config.xml");
System.out.println("========== AOP前置通知测试 ==========\n");
// 2. 获取电影管理模块的bean并调用printInfo方法
// 注意这里会触发AOP前置通知
dyguanli movie = (dyguanli) context.getBean("dyguanli");
movie.printInfo();
System.out.println("\n========== 测试完成 ==========");
}
}

@ -0,0 +1,106 @@
package com.ssm.aop.xml;
/**
* AOP
* com.ssm.aop.xml
*/
public class dyguanli {
// 属性定义
private String movieName; // 电影名称
private String director; // 导演
private String actor; // 主演
private String releaseDate; // 上映日期
private Integer duration; // 时长(分钟)
private String genre; // 类型
/**
*
*/
public dyguanli() {
}
/**
*
*/
public dyguanli(String movieName, String director, String actor, String releaseDate) {
this.movieName = movieName;
this.director = director;
this.actor = actor;
this.releaseDate = releaseDate;
}
// ==================== Getter 和 Setter 方法 ====================
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
// ==================== 重写toString方法 ====================
@Override
public String toString() {
return "dyguanli{" +
"movieName='" + movieName + '\'' +
", director='" + director + '\'' +
", actor='" + actor + '\'' +
", releaseDate='" + releaseDate + '\'' +
", duration=" + duration +
", genre='" + genre + '\'' +
'}';
}
// ==================== 定义printInfo方法 ====================
// 这个方法将被AOP前置通知拦截
public void printInfo() {
System.out.println("=== 电影信息 ===");
System.out.println("电影名称: " + movieName);
System.out.println("导演: " + director);
System.out.println("主演: " + actor);
System.out.println("上映日期: " + releaseDate);
System.out.println("时长: " + duration + " 分钟");
System.out.println("类型: " + genre);
System.out.println("==================");
}
}
Loading…
Cancel
Save