您的位置:首页 > 编程语言 > Java开发

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 失败");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: