2017-05-27 146 views
0

我試圖爲Spring AOP.The代碼執行一個簡單的程序,因爲這是如下 - :的BusinessService的.java春錯誤創建名稱爲豆「org.springframework.aop.config.internalAutoProxyCreator

package com.kruders.spring.aop; 

public interface BusinessService { 
    void doSomeThing(); 
} 

BusinessImpl.java

package com.kruders.spring.aop; 

import org.springframework.stereotype.Service; 

public class BusinessImpl implements BusinessService { 
    public void doSomeThing() { 
     System.out.println("Do Something Here"); 
    } 
} 

BusinessAspect.java

package com.kruders.spring.aspect; 

import org.aspectj.lang.ProceedingJoinPoint; 

public class BusinessAspect { 
    public void before() { 
     System.out.println("Before method is called"); 
    } 

    public void after() { 
     System.out.println("After method is called"); 
    } 

    public void afterReturning() { 
     System.out.println("After returning method is called"); 
    } 

    public void afterThrowing() { 
     System.out.println("After throwing method is called"); 
    } 

    public void around(ProceedingJoinPoint joinPoint) throws Throwable { 
     System.out.println("Around method is called"); 
     System.out.println("Around before is running"); 
     joinPoint.proceed(); 
     System.out.println("Around after is running"); 
    } 
} 

Main.java

package com.kruders.spring.core; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

import com.kruders.spring.aop.BusinessService; 

public class Main { 
    public static void main(String args[]) { 
     ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Business.xml"); 
     BusinessService businessService = (BusinessService)appContext.getBean("businessService"); 
     businessService.doSomeThing(); 
    } 
} 

彈簧Business.xml

<?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:context="http://www.springframework.org/schema/context" 
    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-3.0.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <bean id="businessService" class="com.kruders.spring.aop.BusinessImpl" /> 
    <!-- Aspect --> 
    <bean id="businessAspect" class="com.kruders.spring.aspect.BusinessAspect" /> 

    <aop:config> 
     <aop:aspect ref="businessAspect"> 
      <aop:pointcut id="businessExp" 
          expression="execution(* com.kruders.spring.aop.BusinessImpl*.*(..))" /> 
      <aop:before 
        method="before" 
        pointcut-ref="businessExp"/> 
      <aop:after 
        method="after" 
        pointcut-ref="businessExp"/> 
      <aop:after-returning 
        method="afterReturning" 
        pointcut-ref="businessExp"/> 
      <aop:after-throwing 
        method="afterThrowing" 
        pointcut-ref="businessExp"/> 
      <aop:around 
        method="around" 
        pointcut-ref="businessExp"/> 
     </aop:aspect> 
    </aop:config> 
</beans> 

我已經包括了所有的AOP罐子和用彈簧4.3.6

Spring Aop Jars-: 

aspectj-1.6.9,aspectj-DEVELOPMENT-20160512153500,aspectjrt,aspectj-weaver,spring-aop jars 

,但仍然得到部份錯誤。

INFO: Refreshing org[email protected]621be5d1: startup date [Sat May 27 08:59:02 IST 2017]; root of context hierarchy 
May 27, 2017 8:59:02 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [Spring-Business.xml] 
May 27, 2017 8:59:02 AM org.springframework.aop.framework.DefaultAopProxyFactory <clinit> 
INFO: CGLIB2 not available: proxyTargetClass feature disabled 
May 27, 2017 8:59:02 AM org.springframework.context.support.ClassPathXmlApplicationContext refresh 
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.AbstractMethodError: org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors()Ljava/util/List; 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.AbstractMethodError: org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors()Ljava/util/List; 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:223) 
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:702) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:527) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at com.kruders.spring.core.Main.main(Main.java:10) 
Caused by: java.lang.AbstractMethodError: org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors()Ljava/util/List; 
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.setBeanFactory(AbstractAdvisorAutoProxyCreator.java:57) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1647) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1615) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 
    ... 11 more 
+0

錯誤堆棧跟蹤'java.lang.AbstractMethodError',看起來你正在調用一個標記爲'abstract'的方法。 – harshavmb

+0

我在我的BusinessImpl類中爲抽象方法提供了實現,在代碼工作正常時,當所有的Claas和接口都在同一個包中時 –

+0

對我來說看起來很好。我正在檢查'java.lang.AbstractMethodError:org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors()'。你的班級路線中是否有老式彈簧/ aop罐子?這是一個mvn項目嗎? – harshavmb

回答

0

看起來像這是一個類路徑問題。如討論的here,在aspectjspring-aop罐之間存在一些衝突。

移植有你的項目Maven項目及以下是pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>springaop</groupId> 
    <artifactId>springaop</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <resources> 
      <resource> 
       <directory>src</directory> 
       <excludes> 
        <exclude>**/*.java</exclude> 
       </excludes> 
      </resource> 
     </resources> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aspects</artifactId> 
      <version>4.3.8.RELEASE</version> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>4.3.8.RELEASE</version> 
     </dependency> 

     <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.2.6.RELEASE</version> 
     </dependency> 


    </dependencies> 
</project> 

此外,在主類,

ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Business.xml"); 

有警告說appContext未關閉。 appContext是執行一些I/O操作的ResourceLoader,在執行I/O操作後釋放資源至關重要。所以,把它改成ClassPathXmlApplicationContext其中有.close()這個方法最後釋放資源,你可以在finally塊中更精確地做到這一點。

最後,主要的方法是如下:

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
       "Spring-Business.xml"); 
     BusinessService businessService = (BusinessService) appContext.getBean("businessService"); 
     businessService.doSomeThing(); 
     appContext.close(); 

希望這有助於!祝你好運!

0

aopaplliance-alpha1.jar不包含Advice類,因此爲了能夠使用它,您必須單獨下載aopalliance-1.0.jar文件並將其添加到您的依賴項中。

具有該文件的一個鏈接是here

相關問題