完善 com.ssm.di.xml包下的TestDI.java文件,获取bean,输出对象信息

main
郭书豪 2 weeks ago
parent cc48720d46
commit 931160f9b7

@ -8,5 +8,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="commons-logging-1.1.1" level="project" />
<orderEntry type="library" name="com.springsource.org.aopalliance-1.0.0" level="project" />
</component>
</module>

@ -0,0 +1,25 @@
<?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">
<bean id="log" class="com.ssm.aop.Log"/>
<bean id="category" class="com.ssm.aop.Category"/>
<bean id="flower" class="com.ssm.aop.Flower"/>
<bean id="announcement" class="com.ssm.aop.Announcement"/>
<bean id="content" class="com.ssm.aop.Content"/>
<aop:config>
<aop:pointcut id="myPointcut" expression="execution(* com.ssm.aop.*.print*(..))"/>
<aop:aspect ref="log">
<aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>

@ -0,0 +1,7 @@
package com.ssm.aop;
public class Announcement {
public void printPub(){
System.out.println("公告编号1 公告标题:春节不打烊 公告内容:正常发货 公告时间2025-01-25");
}
}

@ -0,0 +1,7 @@
package com.ssm.aop;
public class Category {
public void printMessage(){
System.out.println("商品类型1 商品名称:观花类 商品描述:以观赏花色、花形为主 商品状态:缺货");
}
}

@ -0,0 +1,7 @@
package com.ssm.aop;
public class Content {
public void printContent(){
System.out.println("内容编号1 内容标题:介绍 内容信息:花卉商城");
}
}

@ -0,0 +1,7 @@
package com.ssm.aop;
public class Flower {
public void printFlower(){
System.out.println("花卉编号1 花卉名称:玫瑰 花卉产地:中国 花卉价格50.0");
}
}

@ -0,0 +1,12 @@
package com.ssm.aop;
import org.aspectj.lang.JoinPoint;
public class Log {
public void beforeAdvice(JoinPoint joinPoint) {
Object target = joinPoint.getTarget();
String methodName = joinPoint.getSignature().getName();
System.out.println("前置通知:模拟日志的记录...目标类是:" + target
+ ", 被切入通知的目标方法为:" + methodName);
}
}

@ -0,0 +1,23 @@
package com.ssm.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
public static void main(String[] args) {
// 这里必须用我们专门写的 aop 配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-aop.xml");
Category category = (Category) ac.getBean("category");
category.printMessage();
Flower flower = (Flower) ac.getBean("flower");
flower.printFlower();
Announcement announcement = (Announcement) ac.getBean("announcement");
announcement.printPub();
Content content = (Content) ac.getBean("content");
content.printContent();
}
}
Loading…
Cancel
Save