项目中使用到了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

最新文章

  1. 自己动手制作CSharp编译器
  2. JavaScript实现存储HTML字符串
  3. JavaScript的“闭包”到底是什么(2)
  4. python之6-4装饰器.md
  5. express框架目录结构
  6. IOS 学习笔记(2) 视图UINavigationController
  7. How to setup linked servers for SQL Server and Oracle 64 bit client
  8. POJ2299--树状数组求逆序数
  9. UWB DWM1000 智能跟踪小车 --[蓝点无限]
  10. Navicat premium 破解步骤
  11. 浏览器标签栏logo添加
  12. Python基础之面向对象进阶二
  13. 通过ping命令查看服务器是linux还是windows系列
  14. Windows平台ping测试局域网所有在用IP
  15. 【学习笔记】JS经典异步操作,从闭包到async/await
  16. 019_nginx upstream中keepalive参数
  17. 一个能够编写、运行SQL查询并可视化结果的Web应用:SqlPad
  18. Occlusion Culling遮挡剔除理解设置和地形优化应用
  19. (笔记)Mysql命令delete from:删除记录
  20. B. Beautiful Paintings

热门文章

  1. [Ceres]C++优化库
  2. wireshark简单使用
  3. bash语法注意点
  4. ASP.NET 抓取网页内容
  5. Could not automatically select an Xcode project. Specify one in your Podfile like so
  6. asp.net 条码 一维条码 生成, 一共有32中格式类型
  7. easyui中tab页中js脚本无法加载的问题及解决方法
  8. Git基本用法简介
  9. svn merge详解
  10. myeclipse 常规web项目创建