使用Spring自动生成代理类,spring利用的是动态代理机制
接口
Java代码
public interface UserDao {
void addUser();
void deleteUser();
}
public interface UserDao {
void addUser();
void deleteUser();
}
两个实现:
Java代码
public class UserDaoMySqlImpl implements UserDao {
@Override
public void addUser() {
System.out.println("UserDaoMySqlImpl.addUser()");
}
@Override
public void deleteUser() {
System.out.println("UserDaoMySqlImpl.deleteUser()");
}
}
public class UserDaoOracleImpl implements UserDao {
@Override
public void addUser() {
System.out.println("UserDaoOracleImpl.addUser()");
}
@Override
public void deleteUser() {
System.out.println("UserDaoOracleImpl.deleteUser()");
}
}
Device
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class UserDaoBeforeAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("UserDaoBeforeAdvice.afterReturning()");
}
}
public class UserDaoMySqlImpl implements UserDao {
@Override
public void addUser() {
System.out.println("UserDaoMySqlImpl.addUser()");
}
@Override
public void deleteUser() {
System.out.println("UserDaoMySqlImpl.deleteUser()");
}
}
public class UserDaoOracleImpl implements UserDao {
@Override
public void addUser() {
System.out.println("UserDaoOracleImpl.addUser()");
}
@Override
public void deleteUser() {
System.out.println("UserDaoOracleImpl.deleteUser()");
}
}
Device
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class UserDaoBeforeAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("UserDaoBeforeAdvice.afterReturning()");
}
}
配置文件
Xml代码
<!-- 配置实现类的bean,可以把所有的实现类都写上 -->
<bean id="userdao" class="com.dowebber.spring.aop_after.UserDaoMySqlImpl"></bean>
<!--配置Advice的bean -->
<bean id="beforeadvice" class="com.dowebber.spring.aop_after.UserDaoBeforeAdvice"></bean>
<!--配置spring自动生成代理对象所需要的一些参数 -->
<bean id="userdaoproxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.dowebber.spring.aop_after.UserDao"/>
<property name="target">
<ref bean="userdao"/>
</property>
<property name="interceptorNames">
<list>
<value>beforeadvice</value>
</list>
</property>
</bean>
<!-- 配置实现类的bean,可以把所有的实现类都写上 -->
<bean id="userdao" class="com.dowebber.spring.aop_after.UserDaoMySqlImpl"></bean>
<!--配置Advice的bean -->
<bean id="beforeadvice" class="com.dowebber.spring.aop_after.UserDaoBeforeAdvice"></bean>
<!--配置spring自动生成代理对象所需要的一些参数 -->
<bean id="userdaoproxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.dowebber.spring.aop_after.UserDao"/>
<property name="target">
<ref bean="userdao"/>
</property>
<property name="interceptorNames">
<list>
<value>beforeadvice</value>
</list>
</property>
</bean>
测试类
Java代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUserDaoAfterDevice {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/dowebber/spring/aop_after/spring.xml");
UserDao dao = (UserDao)context.getBean("userdaoproxy");
dao.addUser();
}
}
分享到:
相关推荐
本文实例讲述了PHP简单实现解析xml为数组...xml version=1.0 encoding=UTF-8?> <main xss=removed versionCode=1.0> <controller co=Aritles> <meth title=测试插件 do=aritle/> </controller> <controller co=Ari>
1. XML文档必须有且仅有一个根元素,如`<root>...</root>`。 2. 文档声明(Document Type Declaration, DTD)或XML Schema用于定义XML文档的结构,但不是必需的。 3. XML元素区分大小写,例如`<title>`与`<Title>`是...
xml version="1.0" encoding="gbk"?> <dataRoot> <record> <a1>6</a1> <b2>7</b2> <c3>8</c3> <d4>9</d4> <f5>10</f5> </record> <record> <a1>11</a1> <b2>12</b2> <c3>13</c3> <d4>14</d4> <f5>15</f5> </record> ...
xml version=/'1.0/' encoding=/'gbk/'?> <dataRoot> <record> <name score age >name score age </name score age > </record> <record> <name score age >d1 61 6 </name score age > </record> <record> <name ...
xml version=/'1.0/' encoding=/'gbk/'?> <dataRoot> <record> <name score age >name score age </name score age > </record> <record> <name score age >d1 61 6 </name score age > </record> <record> <name ...
xml version="1.0" encoding="gbk" ?> <root> <Group gid="1" name="组1" remark="组1备注"> <item id="1" name="版本号" remark="02">987</item> <item id="2" name="代号C" remark="实际">666</item> </...
xml version="1.0" encoding="gbk"?> <business comment="注册码信息导入" id="ZCMDR"> <body yylxdm="1"> <input> <zcmxx>注册码信息</zcmxx> </input> </body> </business> ``` - **参数说明** - `zcmxx`: ...
xml version="1.0" encoding="GBK"?> <!DOCTYPE 联系人列表[ <!ENTITY content "某公司部分联系人信息"> <!ELEMENT 联系人列表 (说明,联系人)> <!ELEMENT 联系人 (姓名,ID,公司,电话,地址)> <!ELEMENT 姓名 ...
xml version="1.0" encoding="GBK"?> <project name="测试脚本" default="copyfile" basedir="."> <target name="copyfile"> <copy file=”d:/a.txt” todir=”c:/temp” overwrite=”true”/> </target> </...
xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> ...
xml version="1.0" encoding="GBK"?> <a> <b> <c type="0" centerx="1" centery="2" dispname="3" linestr=""> <d name="OBJECTID" value="100" /> <d name="NAME" value="aaa" /> </c> <c type="1" ...
xml version='1.0' encoding='GBK'?> <resp> <code>1000</code> <balance>2000</balance> <pkgcount="2"> <item name="套餐名称" msgNum="套餐可用条数"/> <item name="套餐名称" msgNum="套餐可用条数"/> </...
xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> ...
xml version="1.0" encoding="GBK"?> <!DOCTYPE Configure PUBLIC "-//MortBay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"> <Configure id="Server" class="org.mortbay.jetty....
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=...
<constant name="struts.i18n.encoding" value="GBK"/> <package name="test" extends="struts-default"> <action name="login" class="com.jb.action.LoginAction" method="login"> <result name="success">/...
xml version="1.0" encoding="..."?>`的声明,它指定了文件的编码方式。如果这个声明与实际编码不符,或者在读取和写入文件的过程中没有正确处理编码,就会出现乱码。 解决XML文件中文乱码问题的关键步骤包括: 1....