看《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系列: 我的第一个spring aop练习的更多相关文章

  1. 框架源码系列三:手写Spring AOP(AOP分析、AOP概念学习、切面实现、织入实现)

    一.AOP分析 问题1:AOP是什么? Aspect Oriented Programming 面向切面编程,在不改变类的代码的情况下,对类方法进行功能增强. 问题2:我们需要做什么? 在我们的框架中 ...

  2. Spring系列(四):Spring AOP详解

    一.AOP是什么 AOP(面向切面编程),可以说是一种编程思想,其中的Spring AOP和AspectJ都是现实了这种编程思想.相对OOP(面向过程编程)来说,提供了另外一种编程方式,对于OOP过程 ...

  3. Java缓存框架使用EhCache结合Spring AOP

    一.Ehcache简介     EhCache是一个纯Java的进程内缓存框架,具有如下特点:     1. 快速简单,非常容易和应用集成.     2.支持多种缓存策略 .     3. 缓存数据有 ...

  4. Spring系列(五):Spring AOP源码解析

    一.@EnableAspectJAutoProxy注解 在主配置类中添加@EnableAspectJAutoProxy注解,开启aop支持,那么@EnableAspectJAutoProxy到底做了什 ...

  5. 使用IDEA搭建一个Spring + AOP (权限管理 ) + Spring MVC + Mybatis的Web项目 (零配置文件)

    前言: 除了mybatis 不是零配置,有些还是有xml的配置文件在里面的. 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration  ...

  6. Spring AOP 系列总括

    Spring有两大核心,IOC和AOP.IOC在Java Web项目中无时无刻不在使用,然而AOP用的比较少,尤其是对一些初级程序员,在架构师搭好的框架上开发应用代码,AOP几乎是透明的.然而,项目中 ...

  7. Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)

    参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...

  8. 【转】Java Spring AOP详解

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  9. Spring系列(六):Spring事务源码解析

    一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...

随机推荐

  1. FLUSH TABLES WITH READ LOCK

    最近在mysql主从复制中用到锁,翻了资料回忆一下.一下内容参考于:http://blog.csdn.net/arkblue/article/details/27376991 1.FLUSH TABL ...

  2. Effective Java 05 Avoid creating unnecessary objects

    String s = new String("stringette"); // Don't do this. This will create an object each tim ...

  3. Effective Java 35 Prefer annotations to naming patterns

    Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...

  4. Outlook 2013 在邮件里面点击超链接时弹出“组织策略阻止我们为您完成此操作”

    现象描叙:     在Outlook在邮件里面点击超链接时,打不开超链接页面,弹出如下提示: 这个是因为之前安装了其它浏览器(例如,我安装了360的浏览器),并且设置为了默认浏览器,后来卸载了该浏览器 ...

  5. oracle数据库ORA-01654 错误的解决方法

    引言: 数据库突然报: ORA-01654: unable to extend index BO.INDEX_indexname by 311072 in tablespace 错误,上网查原因,发现 ...

  6. andriod增、删、改、查

    将数据库的增删改查单独放进一个包 */ package com.itheima28.sqlitedemo.dao; import java.util.ArrayList; import java.ut ...

  7. 分享一个linux环境下快速读取行数的命令

    最初是因为我需要计算一天的日志行数,如果用传统意义上的cat  a.log |wc -l的话因为是单线程,所以需要计算半小时的样子,后来同组的小伙伴教了我一个方法可以有效提高计算速度,将计算时间减半. ...

  8. .net framework 4.6.2 下载

    .net framework   .net framework版本: 4.6.2 File Name: NDP462-KB3151800-x86-x64-AllOS-ENU.exe 发布日期: 201 ...

  9. 【温故而知新-Javascript】对象

    1 创建对象 Javascript 支持对象的概率.有多种方法可以用来创建对象. <!DOCTYPE html> <html lang="en"> < ...

  10. ZooKeeper 安装部署及hello world(转)

    ZooKeeper  安装部署及hello world 先给一堆学习文档,方便以后查看官网文档地址大全:OverView(概述)http://zookeeper.apache.org/doc/r3.4 ...