Spring学习笔记 AOP的HelloWorld
2012-09-18 17:40
423 查看
HelloWorld实现的基本步骤如下:
1.建立接口
2.建立接口的实现类
3.建立main方法类
4.建立Spring配置文件
测试运行,通过后
5.建立在接口实现类方法前需要执行的代码类(这种类在Spring中叫做Advice)
6.修改Spring配置文件
测试运行,查看结果
1.建立接口
package aop; public interface WebSite { public void showContent(); }
2.建立接口的实现类
package aop; public class ArvinRongHomePage implements WebSite { @Override public void showContent() { System.out.println("Arvin Rong's Space."); } }
3.建立main方法类
package aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AopMain { /** * @author ArvinRong, 2012-8-31 */ public static void main(String[] args) { String configString = "aop/spring-config.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(configString); WebSite webSite = context.getBean("website",WebSite.class); webSite.showContent(); } }
4.建立Spring配置文件
<?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:context="http://www.springframework.org/schema/context" 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"> <bean id="website" class="aop.ArvinRongHomePage" /> </beans>
测试运行,通过后
Arvin Rong's Space.
5.建立在接口实现类方法前需要执行的代码类(这种类在Spring中叫做Advice)
package aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class BeforeContentAdvice implements MethodBeforeAdvice { @Override public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { System.out.println("Welcome to my page."); } }
6.修改Spring配置文件
<?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:context="http://www.springframework.org/schema/context" 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"> <bean id="websiteTarget" class="aop.ArvinRongHomePage" /><!--id由website更改为websiteTarget--> <bean id="websiteAdvice" class="aop.BeforeContentAdvice" /> <bean id="website" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces" value="aop.WebSite" /> <property name="interceptorNames"> <list> <value>websiteAdvice</value> </list> </property> <property name="target" ref="websiteTarget" /> </bean> </beans>
测试运行,查看结果
Welcome to my page. Arvin Rong's Space.
相关文章推荐
- 【Spring学习笔记】之【6.2 AOP之HelloWorld】
- Spring学习笔记 什么是AOP
- [Spring学习笔记 4 ] AOP 概念原理以及java动态代理
- Spring3.X学习笔记5-SpringAOP基础
- spring学习笔记 -- day08 基于XML的Spring中的AOP
- [原]spring学习笔记9.2-Spring对Aop的支持-annotation的方式
- 学习笔记--代理与AOP及实现类似SPRING的可配置的AOP框架
- Spring4学习笔记-AOP(基于配置文件的方式)
- Spring in Action 4th 学习笔记 之 AOP
- 学习笔记:spring与hibernate整合(采用aop来管理事务来实现声明式事务)
- spring学习笔记(1)——创建spring项目之HelloWorld
- SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法
- Spring学习笔记AOP(三)
- JavaWeb学习笔记-spring-08-AOP-概述
- JavaWeb学习笔记-spring-17-AOP-基于schema配置切面
- [Spring学习笔记 5 ] Spring AOP 详解1
- Spring学习笔记002 - AOP
- 【spring学习笔记三】aop思想介绍及实现原理
- spring学习笔记5--简单模拟实现AOP(JDK版)
- Spring学习笔记 AOP开发