Java系列: 我的第一个spring aop练习
2015-12-26 20:25
393 查看
看《Spring in action》有一段时间了,陆续也都看懂了,但是看懂和自己动手写确实是两回事,今天花了几个小时陆续开始安装spring,开始使用DI,然后使用AOP,在写AOP例子代码的过程中遇到一个编译错误,调试了很久,最终找到愿意了,少加了一个jar包,在pom文件中添加之后就ok了。
package com.DbInterface.config; public interface SetupTable { public int readSetupNodeByNodeTypeId(int nodeType, int nodeId, boolean includeChild); public int readAllNodes(); public int writeNode(int nodeType, int nodeId, String nodeName); public int deleteNode(int nodeType, int nodeId); }
package com.DbInterface; import com.DbInterface.config.*; public class DbInterface { public SetupTable getSetupTable() { return setupTable; } public void setSetupTable(SetupTable setupTable) { this.setupTable = setupTable; } public RealtimeTable getRealtimeTable() { return realtimeTable; } public void setRealtimeTable(RealtimeTable realtimeTable) { this.realtimeTable = realtimeTable; } public CommandTable getCommandTable() { return commandTable; } public void setCommandTable(CommandTable commandTable) { this.commandTable = commandTable; } private SetupTable setupTable; private RealtimeTable realtimeTable; private CommandTable commandTable; }
package com.DbInterfaceForPG; import com.DbInterface.config.SetupTable; public class SetupTableForPG implements SetupTable { public int readSetupNodeByNodeTypeId(int nodeType, int nodeId, boolean includeChild) { System.out.println(String.format("readSetupNodeByNodeTypeId, nodeType=%d, nodeId=%d, includeChild=%b", nodeType, nodeId, includeChild)); return 0; } public int readAllNodes(){ System.out.println("readAllNodes"); return 0; } public int writeNode(int nodeType, int nodeId, String nodeName) { System.out.println(String.format("writeNode, nodeType=%d, nodeId=%d, nodeName=%s", nodeType, nodeId, nodeName)); return 0; } public int deleteNode(int nodeType, int nodeId) { System.out.println(String.format("deleteNode, nodeType=%d, nodeId=%d", nodeType, nodeId)); return 0; } }
<?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" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <aop:config> <aop:aspect ref="logger"> <aop:pointcut id="dbmethod" expression="execution(* com.DbInterfaceForPG.SetupTableForPG.*(..))" /> <aop:before pointcut-ref="dbmethod" method="logBefore" /> <aop:after pointcut-ref="dbmethod" method="logAfter" /> </aop:aspect> </aop:config> <bean id="dbInterface" class="com.DbInterface.DbInterface" autowire="byType"/> <bean id="setupTable" class="com.DbInterfaceForPG.SetupTableForPG"/> <bean id="logger" class="com.PecTrend.util.LoggerWithMethodName"/> </beans>
public class App { public static void main( String[] args ) { ApplicationContext ctx = new ClassPathXmlApplicationContext("PecTrend.xml"); DbInterface dbInterface = (DbInterface)ctx.getBean("dbInterface"); if(dbInterface.getSetupTable() != null) dbInterface.getSetupTable().readSetupNodeByNodeTypeId(123, 456, false); else System.out.println("装配 setupTable 失败"); } }
相关文章推荐
- java web基础 --- URL重定向Filter
- struts2基础复习
- SpringMVC架构
- java.exe 和 javaw.exe 的区别
- 理解Spring框架---利用,xml,java反射简单实现Spring
- java根据IP地址获取本地定位
- Java加入背景音乐
- 大话设计模式之外观模式(Java版实现) 买股票还是买基金在此揭晓
- php eclipse 如何修改php文件创建时默认样式
- 【java基础】引用类型数组初始化中,setter和getter函数空指针问题
- Win7下用Eclipse远程连接Hadoop进行开发
- 【JAVA笔记——道】并发编程CAS算法
- java取出一组中文每个汉字的首字母 如 :中国 zg
- Java之关于Serialization
- Spring加载resource时classpath*:与classpath:的区别
- spring注入properties属性配置
- 记JAVA的二三事(1)——"==","引用"
- [Java VM 系列] GC原理学习
- java web基础 --- Filter
- MyEclipse Java Web 调试