当前位置:K88软件开发文章中心编程语言JavaSpring → 文章内容

Spring 中基于 AOP 的 @AspectJ

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:21:02

e com.tutorialspoint;public class Student { private Integer age; private String name; public void setAge(Integer age) { this.age = age; } public Integer getAge() { System.out.println("Age : " + age ); return age; } public void setName(String name) { this.name = name; } public String getName() { System.out.println("Name : " + name ); return name; } public void printThrowException(){ System.out.println("Exception raised"); throw new IllegalArgumentException(); }}下面是 MainApp.java 文件的内容:package com.tutorialspoint;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Student student = (Student) context.getBean("student"); student.getName(); student.getAge(); student.printThrowException(); }}下面是配置文件 Beans.xml:<?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy/> <!-- Definition for student bean --> <bean id="student" class="com.tutorialspoint.Student"> <property name="name" value="Zara" /> <property name="age" value="11"/> </bean> <!-- Definition for logging aspect --> <bean id="logging" class="com.tutorialspoint.Logging"/> </beans>一旦你已经完成的创建了源文件和 bean 配置文件,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:Going to setup student profile.Name : ZaraStudent profile has been setup.Returning:ZaraGoing to setup student profile.Age : 11Student profile has been setup.Returning:11Going to setup student profile.Exception raisedStudent profile has been setup.There has been an exception: java.lang.IllegalArgumentException.....other exception content

上一页  [1] [2] 


Spring 中基于 AOP 的 @AspectJ