2011-05-04 94 views
1

當我試圖與spring使用AJJ編譯器的aspectj,我越來越關注errror.when我刪除aspectj然後代碼工作正常 有什麼與編譯時造成的問題使用Ajc編譯器與春天的問題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())); 

    } 
} 
+0

請出示您的LoggingAspect代碼 – 2011-05-04 09:19:16

+0

我已添加代碼。如果需要更多信息,請告訴我 – Vish 2011-05-04 09:48:01

+0

是的,看我的回答 – 2011-05-04 11:40:35

回答

1

這很難無需知道哪個線76:

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

,但使用的是一個非常積極的切入點

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

此舉辦各種活動,不僅方法執行,而且靜態和實例inititializers,現場訪問等的匹配(見原始切入點概述在AspectJ Quick Reference)。

如果這些:

  • 回報nulljoinPoint.getTarget(),這條線將拋出一個NPE:

    Class<? extends Object> clazz = joinPoint.getTarget().getClass(); 
    
  • 回報nulljoinPoint.getSignature(),這條線將拋出一個NPE:

    String name = joinPoint.getSignature().getName(); 
    
  • 而且,在這裏你都檢查空或空ARGS:

    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; 
+0

肖恩謝謝你這樣一個美好的答案實際上行76是 – Vish 2011-05-04 12:51:18

+0

@Vish是嗎?什麼? – 2011-05-04 12:56:16

+0

對不起76行是Class <?擴展對象> clazz = joinPoint.getTarget()。getClass(); ..這是給我error.actualy我早先使用春天aop和那裏只有連接點是切入點,我用類似於這裏,沒有充分理解原因。 – Vish 2011-05-04 13:09:12