看《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. jquery1.0源码【1-60行】构造函数及全局$变量

    一.jquery源码1-60行 该部分代码主要完成jquery对象的创建,以及全局变量$与jQurey类的映射: /* * jQuery - New Wave Javascript * * Copyr ...

  2. windows 和 linux ssh互连

    从windows连接到linux: linux开启sshd服务即可,主要是windows的配置如下: 1.使用软件,putty可以直接使用 2.使用cmd控制台连接linux,安装SSH Secure ...

  3. chrome和搜狗浏览器的js问题

    1.在chrome中如果是js添加颜色必须用"#00CC00",必须加#号...不然会出问题,但是在搜狗浏览器中可以没有#号也能正确识别...

  4. PS网页设计教程XXX——在PS中创建一个漫画书主题网页布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  5. D_S 线性表的顺序表示和实现

    线性表的顺序表示又称为顺序存储结构或顺序映像 顺序存储定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构,简言之,逻辑上相邻,物理上也相邻 顺序存储方法:用一组地址连续的存储单元依次存 ...

  6. 杂谈SharpDx中的WIC组件——我们需要WIC的图片编码功能么?

    在前文 SharpDX之Direct2D教程II——加载位图文件和保存位图文件 中,发现在VB2010中不能很好的运用SharpDx中的WIC组件进行图片的编码工作.可能是我的设置问题,也可能是Sha ...

  7. shell script 学习笔记-----标准输出

    1.将标准输出(stdout)和标准错误输出(stderr)分别重定向到两个不同的文件 其中符号'>'默认将标准输出重定向,意思和'1>'相同,‘2>'表示重定向标准错误输出,数字1 ...

  8. windows下如何安装jira

    ---恢复内容开始--- 准备工作: 1.安装jdk,详细不在介绍 2.建立jira帐号:https://id.atlassian.com/login?application=mac&cont ...

  9. 一致性哈希算法学习及JAVA代码实现分析

    1,对于待存储的海量数据,如何将它们分配到各个机器中去?---数据分片与路由 当数据量很大时,通过改善单机硬件资源的纵向扩充方式来存储数据变得越来越不适用,而通过增加机器数目来获得水平横向扩展的方式则 ...

  10. ehcache 一二事 - ssm 中ehcashe的简单配置应用

    Ehcache是一个开源Java分布式缓存.可以配合mybatis来使用   首先,在资源文件夹中新建ehcache.xml   内容如下: <?xml version="1.0&qu ...