一、eclipse新建java项目取名SpringTest

二、导入sping包到构建路径

还需要aspectjweaver.jar

三、创建java类(当然先要创建各种包)

IHelloService.java

package com.zjptcc.wxw.spring.hello;

public interface IHelloService {

public void sayHello();

public void sayChinaHello();

}

HelloServiceImpl.java

package com.zjptcc.wxw.spring.hello;

public class HelloServiceImpl implements IHelloService {

private String Hello;

private String ChinaHello;

@Override

public void sayHello() {

// TODO 自动生成的方法存根

System.out.println(Hello);

}

@Override

public void sayChinaHello() {

// TODO 自动生成的方法存根

System.out.println(ChinaHello);

}

public String getHello() {

return Hello;

}

public void setHello(String hello) {

Hello = hello;

}

public String getChinaHello() {

return ChinaHello;

}

public void setChinaHello(String chinaHello) {

ChinaHello = chinaHello;

}

}

SimpleHelloBean.java

package com.zjptcc.wxw.spring.hello;

public class SimpleHelloBean {

public void sayHello(){

System.out.println("Hello World!");

}

}

AopTest.java

package com.zjptcc.wxw.spring.hello;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

@Aspect

public class AopTest {

/*Pointcut for sayHello*/

@Pointcut("execution(* *.sayHello())")

public void hellopoint() {

}

@Before("hellopoint()")

public void beforehello() {

System.out.println("接下去调用sayHello()......");

}

@AfterReturning("hellopoint()")

public void afterhello() {

System.out.println("函数sayHello()执行结束......");

}

/*Pointcut for sayChinaHello*/

@Pointcut("execution(* *.sayChinaHello())")

public void helloChinapoint() {

}

@Before("helloChinapoint()")

public void beforehelloChina() {

System.out.println("接下去调用sayChinaHello()......");

}

@AfterReturning("helloChinapoint()")

public void afterhelloChina() {

System.out.println("函数sayChinaHello()执行结束......");

}

}

四、配置文件

在src目录下创建resources目录,并在其下建立hello-beans.xml内容如下:

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" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

Hello,china!

你好,中国欢迎您!

五、测试

测试程序TestHelloService.java如下:

package com.zjptcc.wxw.spring.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zjptcc.wxw.spring.hello.IHelloService;

import com.zjptcc.wxw.spring.hello.SimpleHelloBean;

public class TestHelloService {

/**

* @param args

*/

private static ApplicationContext ctx;

public static void main(String[] args) {

// TODO 自动生成的方法存根

ctx = new ClassPathXmlApplicationContext("resources/hello-beans.xml");

//用接口

IHelloService helloWorld = (IHelloService) ctx

.getBean("helloService");

helloWorld.sayHello();

helloWorld.sayChinaHello();

System.out.println("------------------------------------------------------------------------------------");

//用类

SimpleHelloBean SimpleHello = (SimpleHelloBean) ctx

.getBean("SimpleHelloBean");

SimpleHello.sayHello();

}

}

运行结果如下:

六、利用配置文件实现AOP

java类

package com.zjptcc.wxw.spring.person;

public class PersonService {

private String name;

public void setName(String name) {

this.name = name;

}

public void info(){

System.out.println("此人姓名为:"+name);

}

public String getName(){

return name;

}

}

package com.zjptcc.wxw.spring.person;

public class PersonAop {

public void before_info() {

System.out.println("接下去,调用info()显示人名......");

}

public void after_info() {

System.out.println("调用info()结束......");

}

public void after_get() {

System.out.println("调用getName()结束......");

}

}

resources/person-beans.xml

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" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

露茜

测试

package com.zjptcc.wxw.spring.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zjptcc.wxw.spring.person.PersonService;

public class TestPersonService {

/**

* @param args

*/

private static ApplicationContext ctx;

public static void main(String[] args) {

// TODO 自动生成的方法存根

ctx = new ClassPathXmlApplicationContext("resources/person-beans.xml");

PersonService p = (PersonService) ctx

.getBean("personService");

p.info();

System.out.println(p.getName());

}

}

运行

参考:

Java实例类中的切面_Spring进行面向切面编程的一个简单例子相关推荐

  1. java继续_Java中消除实现继续和面向接口编程

    在匆忙之际理清消除实现继续和面向接口编程这样两个大题目可不是一件轻易的事情,尤其考虑到自身的熟悉水平.坦白的说,这又是一篇"炒冷饭"的文章,但这"冷饭"又确实不 ...

  2. Spring进行面向切面编程的一个简单例子

    2019独角兽企业重金招聘Python工程师标准>>> 一.eclipse新建java项目取名SpringTest 二.导入sping包到构建路径 还需要aspectjweaver. ...

  3. Java Singleton类中的线程安全性的示例代码

    Java Singleton类中的线程安全性的示例代码 Singleton是最广泛使用的创建设计模式之一,用于限制应用程序创建对象.在实际应用程序中,数据库连接或企业信息系统(EIS)等资源是有限的, ...

  4. 探究Java File类中list()、listFiles()的使用及区别,认识和使用匿名内部类

    转载自     探究Java File类中list().listFiles()的使用及区别,认识和使用匿名内部类 内容概要: 1.认识File类: 2.File的构造函数: 3.list()及其重载方 ...

  5. java枚举类中字段有没有必要加final____枚举类字段 Field ‘xxx‘ may be ‘final‘

    java枚举类中字段有没有必要加final 今天在写一个系统统一返回码的枚举类时候,突然想到一个问题,当不小心手抖给枚举类自动生成了set方法,而恰巧在用的地方不小心用了set方法,从而修改了code ...

  6. java string.substring 参数,Java,String类中的subString()方法,stringsubstring

    Java,String类中的subString()方法,stringsubstring public class TestStringSubString { // main函数 public stat ...

  7. Java练习题 类 先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,然后定义两个方法求三角形的周长、面积。

    先创建一个Point类,然后定义Trianglele类. 在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义构造方法对这三个点进行初始化,然后定义两个方法求三角形的周长 ...

  8. vue面向切面_感受面向切面编程

    什么是面向切面 初听面向切面编程时, 一头雾水, 什么是面向切面, 只听说过面向对象(OOP), 面向过程(PO), 函数式编程(FP), 面向切面 ? 面向的难道是某一个面? 面向搜索引擎后才了解到 ...

  9. Java切面理解_Spring AOP面向切面编程:理解篇

    package com.vx.servicehi.annotation; import java.lang.annotation.Annotation; import java.lang.reflec ...

最新文章

  1. 执行系统命令,subprocess使用说明
  2. BZOJ1085: [SCOI2005]骑士精神
  3. annotations_Spring Annotations我从来没有机会使用第2部分:@ConfigurationProperties
  4. VS2008中源文件与模块生成时的文件不同,仍要让调试器使用它吗
  5. 关于换行这个动作,win 和 mac 的实现
  6. ionic入门教程第五课-举例子说明异步回调$q及$q在项目中的用法
  7. 层层深入探究网络连接丢包之谜
  8. .net mysql 类库_在VBnet中连接MySql类库
  9. 统一沟通-技巧-10-Lync-公网证书-Go Daddy-Buy
  10. Unity自学虚拟摇杆
  11. 原生ajax上传获取进度,ajax上传图片获取进度
  12. android模拟器加速,配置android模拟器加速(虚拟化加速)
  13. Mac命令行出现-bash: command not found的解决办法
  14. power shell 命令 动词 大全 再也不用死记硬背命令
  15. 2020年电工(中级)新版试题及电工(中级)考试申请表
  16. 如何锁定计算机桌面图标,用Desklock轻松设置和锁定桌面图标 -电脑资料
  17. linux php环境搭建教程,linux php环境搭建教程
  18. 记ViewModel和LiveData使用的一些坑与注意点
  19. Linux gunzip 命令实例讲解
  20. 感量越大抑制频率约低_开关电源EMI设计与整改策略100条!

热门文章

  1. XMind、Axure、Visio这三个软件产品经理需要掌握哪个?要掌握到什么程度?
  2. PMP之十五矩阵工具总结
  3. 安卓系统AOSP源码修改入门
  4. 无源波分和彩光模块_无源组网向半有源组网改造的5G前传无源WDM方案
  5. 毕业论文word排版常用小技巧——目录导航、参考文献引用……
  6. github clone 代码到本地全步骤
  7. 人力资源管理如何借助数字化提升人效比?
  8. Landsat系列数据级别详解
  9. 制作自己的CTPN训练集
  10. 自动驾驶仿真:ECU TEST 、VTD、VERISTAND连接配置