• Java系列: 我的第一个spring aop练习


    看《Spring in action》有一段时间了,陆续也都看懂了,但是看懂和自己动手写确实是两回事,今天花了几个小时陆续开始安装spring,开始使用DI,然后使用AOP,在写AOP例子代码的过程中遇到一个编译错误,调试了很久,最终找到愿意了,少加了一个jar包,在pom文件中添加之后就ok了。

    1 package com.DbInterface.config;
    2 
    3 public interface SetupTable {
    4     public int readSetupNodeByNodeTypeId(int nodeType, int nodeId, boolean includeChild);    
    5     public int readAllNodes();
    6     public int writeNode(int nodeType, int nodeId, String nodeName);
    7     public int deleteNode(int nodeType, int nodeId);
    8 }
    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 失败");        
        }
    }
  • 相关阅读:
    [转]easyui data-options的使用
    HTML5新事物
    jQuery checkbox相关
    mybatis insert前获取要插入的值
    mybatis获得刚刚插入的自增的值
    MySQL 获得当前日期时间(以及时间的转换)
    Linux dirname $0 source if
    CCS
    Linux compress & uncompress
    Programming In Scala Reading Note 8
  • 原文地址:https://www.cnblogs.com/strinkbug/p/5078807.html
Copyright © 2020-2023  润新知