项目中使用到了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. ironic baremetal rescue process

    1.用户调用Nova的rescue函数 nova/virt/ironic/driver.py class IronicDriver(virt_driver.ComputeDriver): ...... ...

  2. Android记事本07

    昨天: activity横竖屏切换的生命周期 今天: Anr异常的原因和解决方案 遇到的问题: 无.

  3. 写把proto函数搞清楚

    在做blk层之前,先把proto搞清楚 ffi_lua metatype可以给函数加方法, lua中冒号是啥意思?冒号会传入self,但是点号不会传入self

  4. 湘潭邀请赛 2018 E From Tree to Graph

    题意: 给出一棵树以及m,a,b,x0,y0.之后加m条边{(x1,LCA(x1,y1)),(x2,LCA(x2,y2))...(xm,LCA(xm,ym))}.定义z = f(0)^f(1)^... ...

  5. ZJUTACM

    描述 这回是浙江工业大学的ACM程序设计竞赛,欢迎你的到来!但是,请稍等!裁判Joe说了,必须正确回答他的问题,才可以看到PIPI的气球MM,KUKU的气球GG.Joe手上有7张卡片,每张卡片上有一个 ...

  6. 7月11日day3总结

    今天学习过程和总结 一 1.输出流的字符流.字节流 2.加锁.多线程的理解,产生的原因.cpu同时运行最大数.其他的都在及时切换.1.继承Thred类,重写run方法. 2.实现Runnable接口. ...

  7. C++中的 Round(),floor(),ceil()

     2.1             2.6               -2.1               -2.6floor : 不大于自变量的最大整数             2          ...

  8. 64位操作系统安装32位客户端和PL/SQL

    PL/SQ只能使用32位的Oracle客户端.在64位系统下安装了64位的oracle 11g,使用PL/SQL需再安装32位Oracle客户端. 按以下方法试验成功: 1)安装32位的Oracle客 ...

  9. NOIP2011提高组

    D1T1.铺地毯 for循环 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  10. 飞扬的小鸟(NOIP2014)(丧病DP题)

    原题传送门 刚开始我还以为这道题目非常的简单.. 然后随便打了一个DP,直接WA,被zxyer狠狠地D了一顿. 然后发现有好多细节.. 首先假如某横坐标没有管子,那么l[x]=0;h[x]=m+1; ...