【问题标题】:Using Ajc compiler with Spring problem AspectJ使用带有 Spring 问题 AspectJ 的 Ajc 编译器
【发布时间】:2011-05-04 08:22:16
【问题描述】:

当我尝试使用 ajc 编译器对 spring 进行 aspectj 时,我得到以下错误。当我删除 aspectj 时,代码工作正常 编译时编织是否有任何问题

caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
... 31 more
Caused by: java.lang.NullPointerException
at com.cdf.dfdxc.aspect.logging.LoggingAspect.logEntry(LoggingAspect.java:76)
at com.cdfc.fdged.uow.UdfdFactory.<clinit>(UOWfdy.java:1)

Spring bean 文件

<bean id="hibernateCertificateDao" class="com.Add.exasmple2.dao.HibernateCertificateDaoImpl" autowire="byType">
    <property name="hibernateTemplate">
        <ref bean="hibernateTemplate" />
    </property>
</bean>

<bean id="AddCert" class="com.Add.exasmple2.uow.AddCertUOW" lazy-init="true" autowire="byType">
    <constructor-arg ref="oXMapper"></constructor-arg>
</bean>

<bean id="uOWFactoryBean" class="com.Add.exasmple2.uow.UOWFactory" />

<bean id="businessProcessManager" class="com.Add.exasmple2.infrastructure.BusinessProcessManager"></bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

类路径:/资源/休眠映射

<bean id="exampleDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
        <value>com.ibm.db2.jcc.DB2Driver</value>
    </property>
    <property name="url">
        <value>jdbc:db2://20.15.29.108:50001/XC128086</value>
    </property>
    <property name="password">
        <value>db2dut$</value>
    </property>
    <property name="username">
        <value>db2dut</value>
    </property>
</bean>

<bean id="exampleHibernateProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext
            </prop>
            <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
            </prop>
            <prop key="c3p0.acquire_increment">3</prop>
            <prop key="c3p0.max_size">30</prop>
            <prop key="c3p0.min_size">3</prop>
            <prop key="c3p0.max_statements">0</prop>
            <prop key="c3p0.idle_test_period">0</prop>
            <prop key="c3p0.timeout">0</prop>
            <prop key="current_session_context_class">thread</prop>
            <prop key="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            <prop key="connection.autocommit">true</prop>
            <prop key="show_sql">false</prop>
            <prop key="format_sql">false</prop>
            <prop key="use_sql_comments">false</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>



<tx:annotation-driven transaction-manager="transactionManager" />


<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

记录方面代码

  @Pointcut("within(com.csc.exceed.uow.*)")
public void loggingAspect() {

}

// invoked before the method execution
@Before("loggingAspect()")
public void logEntry(JoinPoint joinPoint) {

    Class<? extends Object> clazz = joinPoint.getTarget().getClass();
    String name = joinPoint.getSignature().getName();

    if (ArrayUtils.isEmpty(joinPoint.getArgs())) {
        if (!(name.startsWith("get")) || (name.startsWith("set")))
            logger.log(LogLevel.INFO, clazz, null, BEFORE_STRING, name,
                    constructArgumentsString(clazz, joinPoint.getArgs()));
    } else {

        logger.log(LogLevel.INFO, clazz, null, BEFORE_WITH_PARAMS_STRING,
                name, constructArgumentsString(clazz, joinPoint.getArgs()));

    }
}

【问题讨论】:

  • 请出示您的 LoggingAspect 代码
  • 我已经添加了代码。如果需要更多信息,请告诉我
  • @Sean 我有新的question 关于事务和方面j

标签: java spring aspectj spring-aop


【解决方案1】:

很难不知道第 76 行是哪一行:

at com.cdf.dfdxc.aspect.logging.LoggingAspect.logEntry(LoggingAspect.java:76)

但是你使用了一个非常激进的切入点

@Pointcut("within(com.csc.exceed.uow.*)")
public void loggingAspect() {}

这匹配所有类型的事件,不仅是方法执行,还包括静态和实例初始化程序、字段访问等(请参阅AspectJ Quick Reference 中的原始切入点概述)。

如果有以下情况:

  • joinPoint.getTarget()返回null,这行会抛出一个NPE:

    Class<? extends Object> clazz = joinPoint.getTarget().getClass();
    
  • joinPoint.getSignature()返回null,这行会抛出一个NPE:

    String name = joinPoint.getSignature().getName();
    
  • 此外,您正在检查 null 或空参数:

    if (ArrayUtils.isEmpty(joinPoint.getArgs())) {
        if (!(name.startsWith("get")) || (name.startsWith("set")))
            logger.log(LogLevel.INFO, clazz, null, BEFORE_STRING, name,
    

    但您仍然使用 args:

                constructArgumentsString(clazz, joinPoint.getArgs()));
    

    这也可能引发 NPE,具体取决于 constructArgumentsString() 中的代码

检查哪一个发生在第 76 行,你就有了失败的候选人。但我的第一个提示是用executioncall 切入点替换你激进的包罗万象切入点。

我会使用以模块化方式定义的组合切入点:

// you can easily reuse this
@Pointcut("within(com.csc.exceed.uow.*)")
public void myApp() {}

// and this
@Pointcut("execution(* *.*(..))")
public void methodExecution(){}

// but this is the pointcut you are actually matching
@Pointcut("myApp() && methodExecution()")
public void methodExecutionInMyApp(){}

@Before("methodExecutionInMyApp()")
public void logMethodExecutions(JoinPoint jp){
    // your code here
}

以下是如何使类成为 NPE 安全的:

Class<?> clazz = joinPoint.getTarget() !=null 
               ? joinPoint.getTarget().getClass() 
               : null;

【讨论】:

  • 抱歉第 76 行是 Class extends Object> clazz = joinPoint.getTarget().getClass();..这给出了错误。实际上我之前使用的是spring aop,只有连接点是切入点,我在这里使用类似,但没有完全理解原因。
  • @Vish 确切地说:Spring AOP 只捕获方法执行,真正的 AspectJ 捕获的远不止这些
  • @Sean 谢天谢地,我已经能够使用上面给出的帮助解决所有问题,我只有一个问题,我无法类对象。这总是给出一个空指针异常......是这里有什么出路:)
  • @Sean 谢谢..我希望你是我的老师 :)..无论如何,当我遇到问题时,我会继续困扰你 :) 你能推荐一些好的 Java 书,我可以在其中掌握基础知识
  • @Vish a) 你可以从我的个人资料中查找我的Skype。你不会是第一个每隔一段时间问我问题的人 b) 书籍。 Java 版:Joshua Bloch 的 Effective Java 第 2 版,AspectJ 和 Spring AOP:Ramnivas Laddad 的 AspectJ in Action 第 2 版,Spring:打印在线参考的 PDF 版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 2019-03-26
相关资源
最近更新 更多