看《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. iOS开发之网络编程--小文件下载

    文件下载方式: 如果下载的文件比较小,下载方式: 直接用NSData的 +(id)dataWithContentsOfURL:(NSURL*)url; 利用NSURLConnection发送一个HTT ...

  2. Burp Suite安装及详细使用教程-Intruder模块详解

    01 介绍 安装要求: Java 的V1.5 + 安装( 推荐使用最新的JRE ), 可从这里免费 http://java.sun.com/j2se/downloads.html Burp Suite ...

  3. 使用网站processon在线作图

    网站:https://www.processon.com/ 该网站提供多种图形的在线制作,并支持多人协作.   目前提供以下图形的制作: 个人管理界面:

  4. SQL基础(1)-创建及修改表

    1. 建表语句 CREATE TABLE fdh_client_info ( id varchar2(50) primary key, name varchar2(30) not null, sex ...

  5. cocos2d-x之使用plist文件初试

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } FileUtils *fu=FileUtils::getInstan ...

  6. 避免jQuery名字冲突--noConflict()方法

    众所周知,在jQuery语法中,$符号是jQuery的简写方式.但在某些情况下,可能需要在同一个页面引入其他javascript库(比如Prototype).因为$简短方便,很多的库也是使用$符号.为 ...

  7. Linux系统搭建LAMP平台

    知识背景(来自:百度百科): LAMP指的Linux(操作系统).Apache HTTP 服务器,MySQL(有时也指MariaDB,数据库软件) 和PHP(有时也是指Perl或Python) 的第一 ...

  8. Plus One

    Plus One https://leetcode.com/problems/plus-one/ Given a non-negative number represented as an array ...

  9. Linux学习之七——乱码的解决方案

    一.乱码的原因 乱码是编码不统一引起的,有下面一些地方需要注意 1. Linux 系统默认支持的语系数据:这与 /etc/sysconfig/i18n 有关:2. 你的终端界面 (bash) 的语系: ...

  10. C和CPP关于条件运算符的区别

    条件运算符形式: cond ? expr1 : expr2; 在C语言中执行过程是: 先对cond求值,值为真返回expr1的值,否则返回expr2的值.(右值) gcc测试结果: 在Cpp中如果两个 ...