1.在com.ssm.aop.xml新建了Log.java和Message.java,新建了bean-aop-xml.xml文件,在libs里引入了包。2.为留言管理在TestXml里输出实体信息3.输出实体属性的基础上多输出前置通知。

main
刘旭航 4 months ago
parent 39b888593a
commit edcc16e3cf

@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="com.springsource.org.aopalliance-1.0.0">
<CLASSES>
<root url="jar://$PROJECT_DIR$/src/libs/com.springsource.org.aopalliance-1.0.0.jar!/" />
<root url="jar://$PROJECT_DIR$/src/libs/com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar!/" />
<root url="jar://$PROJECT_DIR$/src/libs/spring-aspects-5.1.6.RELEASE.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

@ -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,24 @@
<?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">
<bean id="message" class="com.ssm.aop.xml.Message">
<property name="id" value="123456"></property>
<property name="content" value="好看"></property>
<property name="author" value="张三"></property>
<property name="age" value="22"></property>
</bean>
<bean id="log" class="com.ssm.aop.xml.Log"/>
<aop:config>
<aop:aspect id="log" ref="log">
<aop:pointcut id="pointcut_log" expression="execution(public void com.ssm.aop.xml.Message.printInfo())"/>
<aop:before method="before_log" pointcut-ref="pointcut_log"/>
</aop:aspect>
</aop:config>
</beans>

@ -0,0 +1,24 @@
<?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">
<bean id="message" class="com.ssm.aop.xml.Message">
<property name="id" value="123456"></property>
<property name="content" value="好看"></property>
<property name="author" value="张三"></property>
<property name="age" value="22"></property>
</bean>
<bean id="log" class="com.ssm.aop.xml.Log"/>
<aop:config>
<aop:aspect id="log" ref="log">
<aop:pointcut id="pointcut_log" expression="execution(public void com.ssm.aop.xml.Message.printInfo())"/>
<aop:before method="before_log" pointcut-ref="pointcut_log"/>
</aop:aspect>
</aop:config>
</beans>

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

@ -0,0 +1,45 @@
package com.ssm.aop.xml;
public class Message {
private int id;
private String content;
private String author;
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void printInfo(){
System.out.println("用户id"+id+"留言内容:"+content+"用户姓名:"+author+"用户年龄:"+age);
}
}

@ -1,9 +1,15 @@
package com.ssm.aop.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestXML {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-aop-xml.xml");
Message message = (Message)applicationContext.getBean("message");
message.printInfo();
}
}

Loading…
Cancel
Save