项目中使用到了Spring,写了一个简单的例子,跟大家分享一下,由于自己写东西,所以在技术选择上充分自由,虽然对于Spring的利弊众说纷纭,我也不能评判,反正我是尝试用了,记得在上学时候老师讲Spring的时候给出的几个关键点是,控制反转(IOC),和依赖注入。这些概念真的是让人望而却步。所以上学的时候关于这部分内容就基本忽略了。我的浅见是Spring有自己的反射机制,能够将开发者配置到配置文件中的类实例化。

  我写的内容就是通过注入的方式实现一个Hello的功能,然后在通过aop的方式来实现一些上下文的铺垫,使用的是Spring3.0,下面看一下代码,首先我定义了一个ISayHello类,内容如下:

package spring_test;

public interface ISayHello {
public void sayHello();
}

  在定义两个类分别继承ISayHello接口,实现sayHello方法,AmericaSay,ChineseSay内容分别如下:  

package spring_test;

public class AmericaSay implements ISayHello {

    @Override
public void sayHello() {
System.out.println("Hello");
} }

  

package spring_test;

public class ChineseSay implements ISayHello {

    @Override
public void sayHello() {
System.out.println("你好");
} }

  然后在定一个映射类,用来获取在配置文件中获取到的对象:  

package spring_test;

public class PeopleSayHello {
public ISayHello iSayHello; public ISayHello getiSayHello() {
return iSayHello;
} public void setiSayHello(ISayHello iSayHello) {
this.iSayHello = iSayHello;
} public void SayHello() {
iSayHello.sayHello();
}
}

  OK,准备工作做完了,下面也就是最重要的部分,如何在配置文件中配置Bean:  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ChineseSayBean " class="spring_test.ChineseSay"></bean>
<bean id="SayHelloBean" class="spring_test.PeopleSayHello">
<property name="iSayHello" ref="ChineseSayBean"></property>
</bean>
</beans>

  现在准备工作都完成了,下面该是如何让这些准备工作实现价值的时候了:  

package spring_test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Spring_001_main {
public static void main(String[] arg) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"sayHello.xml");
PeopleSayHello s = (PeopleSayHello) ctx.getBean("SayHelloBean");
s.SayHello();
}
}

  运行结果:输出您好。感觉有些突兀,我们在给他添加一些铺垫,在输出你好之前,和你好之后在加一些内容,我们通过aop来实现,首先需要将配置文件中加上aop的约束,以及切片的定义。关于切片大家可以了解一下:http://shouce.jb51.net/spring/aop.html 

<?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.1.xsd">
<bean id="HostBean" class="spring_test.aop.Host"></bean>
<bean id="ChineseSayBean " class="spring_test.ChineseSay"></bean>
<bean id="SayHelloBean" class="spring_test.PeopleSayHello">
<property name="iSayHello" ref="ChineseSayBean"></property>
</bean> <aop:config>
<aop:aspect id="HostAspect" ref="HostBean">
<aop:pointcut id="sayhello"
expression="execution(* spring_test.ChineseSay.*(..))" />
<aop:before method="doBefore" pointcut-ref="sayhello" />
<aop:after method="doAfter" pointcut-ref="sayhello" />
</aop:aspect>
</aop:config>
</beans>

   上面这段话的简单解释就是定义一个切面HostAspect,该切面引用HostBean,就是要调用HostBean指向类中的方法,在切面中有个切入点sayhello,在切入点sayhello有前置通知和后置通知,分别调用doBefore和doAfter方法。

,,下面就是在不敢变源代码的接触上加上主持人的角色和任务,在同学上台之前,由主持人引荐,在发言人下台之后,主持人送一下,下面就是添加的主持人的角色:  

package spring_test.aop;

import org.aspectj.lang.JoinPoint;

public class Host {
public void doBefore(JoinPoint jp) {
System.out.println("欢迎下一位同学发言,鼓掌:papapapapapa");
} public void doAfter(JoinPoint jp) {
System.out.println("谢谢同学的发言,再次把掌声送给他,papappapapa");
}
}

  OK,我们要实现的功能已经完成了,你信不,很方便。看一下执行结果:  

欢迎下一位同学发言,鼓掌:papapapapapa
你好,我是雷锋,请多多照顾
谢谢同学的发言,再次把掌声送给他,papappapapa

  很简单的一个功能,可以下载我的demo,实现这个功能以后大家就可以想想关于过滤器啊,拦截器这一类功能也可以通过这种方法来完成。

  

  下载地址:

  http://files.cnblogs.com/fantiantian/javatest.rar

spring相关的jar文件可以从一下地址下载:

http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.2.RELEASE.zip

http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.2.RELEASE-dependencies.zip

Spring的AOP浅尝的更多相关文章

  1. 浅尝Spring注解开发_自定义注册组件、属性赋值、自动装配

    Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含自定义扫描组件.自定义导入组件.手动注册组件.自动注入方法和参数.使用Spring容器底层组件等 配置 @Confi ...

  2. 浅尝Spring注解开发_Bean生命周期及执行过程

    Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...

  3. 浅尝Spring注解开发_AOP原理及完整过程分析(源码)

    浅尝Spring注解开发_AOP原理及完整过程分析(源码) 浅尝Spring注解开发,基于Spring 4.3.12 分析AOP执行过程及源码,包含AOP注解使用.AOP原理.分析Annotation ...

  4. 浅尝Spring注解开发_Servlet3.0与SpringMVC

    浅尝Spring注解开发_Servlet 3.0 与 SpringMVC 浅尝Spring注解开发,基于Spring 4.3.12 Servlet3.0新增了注解支持.异步处理,可以省去web.xml ...

  5. 浅谈Spring的AOP实现-动态代理

    说起Spring的AOP(Aspect-Oriented Programming)面向切面编程大家都很熟悉(Spring不是这次博文的重点),但是我先提出几个问题,看看同学们是否了解,如果了解的话可以 ...

  6. 浅谈Spring的AOP实现-代理机制

    说起Spring的AOP(Aspect-Oriented Programming)面向切面编程大家都很熟悉(Spring不是这次博文的重点),但是我先提出几个问题,看看同学们是否了解,如果了解的话可以 ...

  7. 浅尝装饰器和AOP

    [写在前面] 参考文章:https://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html[从简单的例子入手进行讲解,由浅入深,很到位] 装饰器部 ...

  8. Spring:AOP

    摘要 本文内容为我在网上搜集Spring AOP资料的汇总.摘抄. AOP是一种编程思想,其对不同对象进行了横向的抽象,将不同对象的.和主流程无关的公共逻辑抽象出来以方便维护.AOP的实现基础为AOP ...

  9. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

随机推荐

  1. 洛谷 P2173 [ZJOI2012]网络 解题报告

    P2173 [ZJOI2012]网络 题目描述 有一个无向图G,每个点有个权值,每条边有一个颜色.这个无向图满足以下两个条件: 对于任意节点连出去的边中,相同颜色的边不超过两条. 图中不存在同色的环, ...

  2. code forces 996BWorld Cup

    B. World Cup time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. 用hibernate.properties代替hibernate.cfg.xml配置常用的属性

    我们使用hibernate时经常在hibernate.cfg.xml文件中配置数据库连接的相关属性,是否显示sql语句,数据库的方言等,这些配置其实也可以在.properties文件中配置.现在我把这 ...

  4. java中截取字符串的方式

    1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len=s.length(); 2.ch ...

  5. Springboot-plus 安装使用的一些问题

    最近在研究一些springboot的框架,然后看到了这个款 springboot  plus ,下载下来研究下. 将安装部署的一些问题记录下来了. 第一个,就是部署的时候,我使用的MySQL数据库,导 ...

  6. 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest(solved 8/11)

    这套题似乎是省选前做的,一直没来写题解---补上补上>_< 链接:http://codeforces.com/gym/101147 一样先放上惨不忍睹的成绩好了--- Problem A ...

  7. 编译gdb 报错 No module named gdb.frames

    将源码目录下的python模块拷贝到指定目录即可 cd /root/gdb-7.12/ cp -rp gdb/python/lib/gdb /usr/local/share/gdb/python/ 编 ...

  8. 获取URL中的文件的扩展名

    问题: 尽可能多地写出获取文件扩展名的方法: //方法一(分割数组) function getExt($url){ $arr = explode('.',$url); $len = count($ar ...

  9. python--控制窗体

    窗体的显示和隐藏 #!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat import win32con import win32 ...

  10. 纠正网上乱传的android调用Webservice方法。

    1.写作背景: 笔者想实现android调用webservice,可是网上全是不管对与错乱转载的文章,结果不但不能解决问题,只会让人心烦,所以笔者决定将自己整理好的能用的android调用webser ...