Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

绿色网站无广告
会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

java,spring,aspectj

来源: zld406504302 分享至:

最近要项目有个新需求,统计查询的接口要进行性能监控,记录日志;对时间参数进行验证。想了想用spring的aop 处理一下。第一次接触aop ,第一次配置,遇到诸多问题。现在tomcat依然起不来,应该是jar version有问题,也可能是jar包之间有冲突。只能在ApplicationContext beanFactory= new ClassPathXmlApplicationContext("*applicationContext*.xml") 加载,测试可以通过。废话少说,代码贴上(仅是架子,没有完整功能实现)

一)切面class定义

package cn.com.superv.ead.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAspect{
//任何通知方法都可以将第一个参数定义为 org.aspectj.lang.JoinPoint类型
private Long startTime ;
private Long endTime ;
private JoinPoint call ;
@Before("execution(public* cn.com.superv.ead.manager.*.*(..)) || execution(* cn.com.superv.hibernate.base.manager.*.*(..))")
public void doBefore(JoinPoint call)
{
this.startTime = System.currentTimeMillis() ;
System.out.println("log Start method: " + call.getTarget().getClass().getName() + "."+ call.getSignature().getName());
}
@After("execution(public* cn.com.superv.ead.manager.*.*(..)) || execution(* cn.com.superv.hibernate.base.manager.*.*(..))")
public void doAfter(JoinPoint call)
{
System.out.println("log Ending method: " + call.getTarget().getClass().getName() + "."+ call.getSignature().getName());
}
@Around("execution(public* cn.com.superv.ead.manager.*.*(..)) || execution(* cn.com.superv.hibernate.base.manager.*.*(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{

long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");

return retVal;

}
@AfterThrowing("execution(public* cn.com.superv.ead.manager.*.*(..)) || execution(* cn.com.superv.hibernate.base.manager.*.*(..))")
public void doThrowing() {
System.err.println(" ------------------advice 'doThrowing' say : is some exception here !-----------" );
}

}

二)application*.xml 配置

1:切面配置

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!----由于切面class已注解的方式设置了各个属性,这里只需开启代理-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!----此处省略bean定义-->

<bean>xxxxxxxxxxx</bean>

</beans>

三) 测试类附上

package cn.com.superv.ead.manager.test;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.superv.ead.intf.netmessagemanage.SndPIMItemL;
import cn.com.superv.ead.manager.EadNMWapSendManager;
import cn.com.superv.hibernate.base.manager.UserInfoManager;
/**
* @author Administrator
* NetMessage_OP
* 2011-10-31 下午01:20:37
* Description:
*/
@Aspect
public class LogAspectTest extends TestCase{
private static final ApplicationContext beanFactory= new ClassPathXmlApplicationContext("*applicationContext*.xml");

// protected void setUp() throws Exception {
// beanFactory=new ClassPathXmlApplicationContext("applicationContext*.xml");
// }

public void testLogAspect()
{
UserInfoManager service=(UserInfoManager)beanFactory.getBean("UserInfoManager");
System.out.println(service.getAllSpArea());
}
public void testEADWapSendManager(){
EadNMWapSendManager eadnmwapsendmanager=(EadNMWapSendManager)beanFactory.getBean("EADNMWapSendManager");
List<String> mdnList = new ArrayList<String>();
mdnList.add("15821888419");
mdnList.add("13764615783");
mdnList.add("13466629031");
mdnList.add("13512122513");
String mdnStr = StringUtils.join(mdnList , "','");
List<SndPIMItemL> objList = eadnmwapsendmanager.getSendDetailListByCondition("10205238", "2011-01-01", "2011-06-01", "0", mdnStr, 10099740);
for(SndPIMItemL sp : objList)
System.out.println(sp.getResultCause()+"-------"+sp.getPhoneNbr()+"-------"+sp.getSndTime());
}
public static void main(String[] args) {
LogAspectTest LogAspectTest = new LogAspectTest();
LogAspectTest.testEADWapSendManager();
LogAspectTest.testLogAspect();
}
}

四)测试结果

[INFO ](SimpleThreadPool.java:247) - 2011-11-16 19:36:47,640 Job execution threads will use class loader of thread: main
[INFO ](QuartzScheduler.java:195) - 2011-11-16 19:36:47,656 Quartz Scheduler v.1.5.2 created.
[INFO ](RAMJobStore.java:138) - 2011-11-16 19:36:47,656 RAMJobStore initialized.
[INFO ](StdSchedulerFactory.java:1014) - 2011-11-16 19:36:47,656 Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
[INFO ](StdSchedulerFactory.java:1018) - 2011-11-16 19:36:47,656 Quartz scheduler version: 1.5.2
[INFO ](QuartzScheduler.java:1853) - 2011-11-16 19:36:47,671 JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@1183871
[INFO ](QuartzScheduler.java:400) - 2011-11-16 19:36:47,671 Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
log Start method: cn.com.superv.ead.manager.impl.EadNMWapSendManagerImpl.getSendDetailListByCondition
log Ending method: cn.com.superv.ead.manager.impl.EadNMWapSendManagerImpl.getSendDetailListByCondition
process time: 328 ms
null-------15821888419-------2011-02-20 05:12
null-------13764615783-------2011-02-20 04:58
null-------15821888419-------2011-02-20 04:58
null-------15821888419-------2011-02-20 04:58
log Start method: cn.com.superv.hibernate.base.manager.impl.UserInfoManagerImpl.getAllSpArea
log Ending method: cn.com.superv.hibernate.base.manager.impl.UserInfoManagerImpl.getAllSpArea
process time: 141 ms
[null, null, null, null,

五)tomcat 启动异常如下

[ERROR](ContextLoader.java:204) - 2011-11-16 19:32:13,218 Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext-hibernate.xml]: Instantiation of bean failed; nested exception is java.lang.AbstractMethodError: org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.determineConstructor(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Constructor;
Caused by:
java.lang.AbstractMethodError: org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.determineConstructor(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Constructor;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:736)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:709)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:386)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:285)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:245)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:188)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3795)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4252)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
[ERROR](StandardContext.java:3799) - 2011-11-16 19:32:13,250 Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext-hibernate.xml]: Instantiation of bean failed; nested exception is java.lang.AbstractMethodError: org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.determineConstructor(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Constructor;
Caused by:
java.lang.AbstractMethodError: org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.determineConstructor(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Constructor;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:736)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:709)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:386)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:285)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:245)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:188)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3795)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4252)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
2011-11-16 19:32:13 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2011-11-16 19:32:13 org.apache.catalina.core.StandardContext start
严重: Context [/EadService] startup failed due to previous errors


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史