복붙노트

[SPRING] 스프링 문제가있는 Ajc 컴파일러 사용하기 AspectJ

SPRING

스프링 문제가있는 Ajc 컴파일러 사용하기 AspectJ

내가 ajc 컴파일러를 사용하여 스프링으로 aspectj를하려고 할 때, 나는 aspectj을 제거하고 errror.when을 따르고있다. 그리고 코드는 잘 동작한다. 컴파일 시간에 짜증나는 문제가있다.

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)

봄 콩 파일

<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>

classpath : / resources / hibernate-mappings

<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()));

    }
}

해결법

  1. ==============================

    1.76 번 라인이 어떤 것인지 알지 못하면 어렵습니다.

    76 번 라인이 어떤 것인지 알지 못하면 어렵습니다.

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

    하지만 당신은 매우 공격적인 pointcut을 사용하고 있습니다.

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

    이는 메소드 실행뿐 아니라 정적 및 인스턴스 초기화 도구, 필드 액세스 등과 같은 모든 종류의 이벤트와 일치합니다 (AspectJ 빠른 참조의 Primitive Pointcuts 개요 참조).

    다음 중 하나 일 경우 :

    이 중 76 번 라인에서 어떤 것이 발생하는지 확인하고 실패한 후보자를 찾으십시오. 하지만 내 첫 번째 힌트는 적극적인 catch-all pointcut을 실행 또는 call pointcut으로 대체하는 것입니다.

    모듈 방식으로 정의 된 결합 된 pointcut을 사용합니다.

    // 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;
    
  2. from https://stackoverflow.com/questions/5880552/using-ajc-compiler-with-spring-problem-aspectj by cc-by-sa and MIT license