Spring核心技术学习总结

02月 28th, 2008

spring的核心技术由两大部分组成:ioc和aop,下面我们就分别对它们进行介绍。
1 ioc技术
1.1 预备知识
ioc即inversion of control(控制反转)的简写,它是一种设计模式,spring只不过是实现了该模式。ioc是工厂模式的升华,同时涉及到了反射的概念。所以,在正式介绍ioc之前,首先介绍一下几个基本的概念及技术:接口、工厂模式、反射。
1.1.1 接口
作为面向对象的语言,和c++不同的是,java不支持。

字串5

1.2 正式进入ioc
ioc实际上是一个高层次的概念,是一种思想或者设计模式,spring等j2ee框架或者对象"a,aop框架将自动为该对象创建一个"代理对象"
{
public void sayhello()
{
system.out.println("hello, i’m a");
}
};package com.longthsoft.learn.spring.models; public class b //"目标对象"b,aop框架将自动为该对象创建一个"代理对象"
{
public void sayhi()
{
system.out.println("hi, i’m b"); 字串1
}
}
step3 接下来编写aop中的"切面"类,代码如下:
package com.longthsoft.learn.spring;
import org.aspectj.lang.annotation.afterreturning;
import org.aspectj.lang.annotation.aspect;
import org.aspectj.lang.annotation.pointcut;
@aspect //利用aspectj语法创建一个"切面"
public class simpleaspect
{
@pointcut("execution(* com.longthsoft.learn.spring.models.*.say*())")
//创建"切入点",该"切入点"捕获了指定项目中相关类的方法("文件(applicationcontext.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" 字串2
xsi:schemalocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<aop:aspectj-autoproxy /> //织入"目标对象"和"切面"之间的关联关系

<bean /> 字串2

<bean />
</beans>
step5 编写测试程序,代码如下:
package com.longthsoft.learn.spring;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

import com.longthsoft.learn.spring.models.a;
import com.longthsoft.learn.spring.models.b;

public final class boot
{
public static void main(string[] args)
{
applicationcontext ctx = new classpathxmlapplicationcontext("applicationcontext.xml");
a a = (a) ctx.getbean("a");
a.sayhello();

b b = (b) ctx.getbean("b");
b.sayhi();
}
}
最后运行结果如下:
hello, i’m a
merry christmas
hi, i’m b
merry christmas
3 引用资源
3.1 候捷 《java反射机制》
3.2 夏昕 《spring开发指南 v0.8》
3.3 佚名 《aop技术介绍--(aop技术基础)》

标签:, ,

相关日志


This entry was posted on 星期四, 02月 28th, 2008 at 9:39 pm and is filed under 程序设计. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply