2016-06-14 61 views
0

我經歷了所有嘗試使我的項目在Tomcat上運行的可能性,並且使用AspectJ Load Time Weaver編寫了模型(getter)並將它們編織在一起。 基本上,我遵循Spring Documentation http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable中的所有步驟。我也遵循了Lazy/Eager loading/fetching in Neo4j/Spring-Data提到的相同方法。 我的項目分爲兩個主要項目: - 核心:spring-data-neo4j(存儲庫和配置),專用包中的域模型,LoggingAspect和LazyLoadingAspect。附:我沒有在XML文件中使用任何配置。我純粹使用Annotation。 - 內容:在Tomcat上運行的Web應用程序,它依賴於核心項目,當我調用域項目中的getter方法時,我想編織它。使用Spring Data進行延遲加載Neo4j 4 +在Tomcat上運行的AspectJ LTW

運行核心本身我設法使用maven插件運行,並添加了aspectj的依賴關係。但是當我搬到Tomcat時,所有的事情都開始了。我嘗試了所有的可能性,例如使用-javaagent,創建自定義的context.xml,把春天的儀器放到tomcat/lib文件夾,等等等等,我收到以下異常:

 
java.lang.IllegalStateException: Post-processor tried to replace bean instance of type [com.test.server.graph.domain.model.Sequence] with (proxy) object of type [org.springframework.beans.factory.aspectj.$Proxy96] - not supported for aspect-configured classes! 
    at org.springframework.beans.factory.wiring.BeanConfigurerSupport.checkExposedObject(BeanConfigurerSupport.java:173) 
    at org.springframework.beans.factory.wiring.BeanConfigurerSupport.configureBean(BeanConfigurerSupport.java:143) 
    at org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect.configureBean(AnnotationBeanConfigurerAspect.aj:63) 
    at org.springframework.beans.factory.aspectj.AbstractDependencyInjectionAspect.ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(AbstractDependencyInjectionAspect.aj:88) 
    at com.test.server.graph.domain.model.Sequence.(Sequence.java:29) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
    at org.neo4j.ogm.annotations.EntityFactory.instantiate(EntityFactory.java:135) 
    at org.neo4j.ogm.annotations.EntityFactory.instantiateObjectFromTaxa(EntityFactory.java:110) 
    at org.neo4j.ogm.annotations.EntityFactory.newObject(EntityFactory.java:61) 
    at org.neo4j.ogm.context.GraphEntityMapper.mapNodes(GraphEntityMapper.java:147) 
    at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:132) 
    at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:107) 
    at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:102) 
    at org.neo4j.ogm.context.RestModelMapper.mapEntity(RestModelMapper.java:157) 
    at org.neo4j.ogm.context.RestModelMapper.map(RestModelMapper.java:76) 
    at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQueriesDelegate.java:94) 
    at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQueriesDelegate.java:73) 
    at org.neo4j.ogm.session.Neo4jSession.query(Neo4jSession.java:313) 
    at org.springframework.data.neo4j.template.Neo4jTemplate.query(Neo4jTemplate.java:217) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) 
    at com.sun.proxy.$Proxy58.query(Unknown Source) 

的pom.xml(核心的.project)

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aspects</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-neo4j</artifactId> 
     <version>${sdn.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.neo4j</groupId> 
     <artifactId>neo4j-ogm-core</artifactId> 
     <version>2.0.3-SNAPSHOT</version> 
    </dependency> 

我的配置類(核項目)

@org.springframework.context.annotation.Configuration 
@ComponentScan(basePackages = "org.test.server.graph") 
@EnableNeo4jRepositories(basePackages = "org.test.server.graph.repository") 
@EnableAspectJAutoProxy 
@EnableSpringConfigured 
public class Neo4jConfig extends Neo4jConfiguration { 
    @Bean 
    public Configuration getConfiguration() { 
     Configuration config = new Configuration(); 
     config.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver") 
       .setURI(System.getProperty("neo4j.host")).setCredentials(System.getProperty("neo4j.user"),System.getProperty("neo4j.password")); 
     return config; 
    } 

    @Bean 
    public SessionFactory getSessionFactory() { 
     return new SessionFactory(getConfiguration(), "org.test.server.graph.domain"); 
    } 

    @Bean 
    @Scope(value = "prototype") 
    public Session getSession() throws Exception { 
     return super.getSession(); 
    } 
} 

域模型類

所推薦的Spring文檔註釋我中的類領域模型

@Configurable

MVC-調度員的servlet(webapp項目)

<context:spring-configured /> 
<context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" /> 

我嘗試使用tomcat-maven-plugin和tomcat獨立安裝(v7和v8)運行。

- 更新 -

Sequence.java

@NodeEntity 
@Configurable 
public class Sequence extends DatabaseObject { 

    @Relationship(type = "hasModifiedResidue", direction = Relationship.OUTGOING) 
    private List<AbstractModifiedResidue> hasModifiedResidue; 

    @Relationship(type = "referenceEntity", direction = Relationship.OUTGOING) 
    private ReferenceSequence referenceEntity; 

    public Sequence() {} 

    //getter and setters 

} 

DatabaseObject.java

@NodeEntity 
@Configurable(
    preConstruction = false 
) 
public abstract class DatabaseObject implements Serializable, Comparable<DatabaseObject> { 

    @GraphId 
    private Long id; 

    // other common attributes + getter and setters, no more annotation 

LazyLoadingAspect

@Aspect 
@Component 
public class LazyFetchAspect { 

    @Autowired 
    private Neo4jOperations neo4jTemplate; 

    @Around("modelGetter()") 
    public Object autoFetch(ProceedingJoinPoint pjp) throws Throwable { 
     System.out.println(" Testing Aspect "); 

     return pjp.proceed(); 
    } 

    @Pointcut("execution(public * com.test.server.graph.domain.model.*.get*(..))") 
    public void modelGetter() { 
    } 
} 
+0

我的猜測是,AspectJ裝載時織不工作,當你在Tomcat下運行的東西。我認爲你應該驗證加載時織入確實生效並修復配置,直到它完成。你也可以考慮使用編譯時編織(你可以用maven來完成,你也可以讓Eclipse使用AspectJ編譯器)。我在編譯時編織時使用'@ Configurable'取得了巨大的成功。嘗試使用'-javaagent'虛擬機參數運行Tomcat JVM。 –

+0

此外,您應該爲AspectJ加載時織機啓用調試日誌記錄,以便您可以真正看到它在做什麼。有關如何執行此操作的詳細信息,請參閱我的[回覆](http://stackoverflow.com/a/35224749/2699901)上一個無關的問題。 –

+0

@NándorElődFekete。核心項目本身在運行,我可以在執行時看到輸出「測試方面」。你需要考慮兩件事,我運行的IntelliJ應該不是問題,我正在使用-javaagent運行tomcat。我將啓用調試日誌記錄,我會提出更新。乾杯。 – Gui

回答

0

的主要問題似乎是,你申請方面豆類:

Post-processor tried to replace bean instance of type [com.test.server.graph.domain.model.Sequence] with (proxy) object of type [org.springframework.beans.factory.aspectj.$Proxy96] - not supported for aspect-configured classes! 

這不會工作,所以你必須確保你的模型類是真正的POJO,而不是豆從春天排除它們。 (確保在domain.model中。*沒有春天註釋) - 也許如果你發佈你的Sequence.java,我可以看到什麼可能會導致衝突。

另一方面,您必須確保aspectj僅分配給模型,而不是任何bean。所以一定要確保你有一個/META-INF/aop.xml只包括你的POJO模型:我的pom.xml的

<!DOCTYPE aspectj PUBLIC 
     "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> 
    <aspectj> 
     <weaver> 
      <!-- only weave classes in our application-specific packages --> 
      <include within="com.test.server.graph.domain.model.*" /> 
     </weaver> 
     <aspects> 
      <!-- weave in just this aspect --> 
      <aspect name="my.util.aspects.Neo4jFetchAspect" /> 
     </aspects> 
    </aspectj> 

零件(我有一個不同的設置現在 - 這就是爲什麼春天版本是這樣舊):

<dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>1.6.12</version> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.6.12</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aspects</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 

我沒有任何特別打造的插件或類似

+0

感謝您的回覆。我在主要問題中添加了類作爲更新。它的@Configurable是因爲Spring Documentation說的。該模型有Neo4j註釋(SDN4),我想LTW它們以應用LazyLoading。我的graphDB深度真的很大,如果我將它們全部用於某個實體,它將永遠持續下去。 – Gui

+0

@Guilherme這可能是可配置的問題。我很肯定你不能混用這個。當你刪除註釋並確保spring沒有達到這個類時會發生什麼? – Niko

+0

我得到NoSuchMethodError。你的pom.xml中的依賴是什麼?或者你使用什麼樣的配置? – Gui

0

將所有建議的建議,這是我的輸出後。

2016-06-15 09:15:21,144 INFO [localhost-startStop-1] DispatcherServlet: FrameworkServlet 'mvc-dispatcher': initialization started 
2016-06-15 09:15:21,166 INFO [localhost-startStop-1] XmlWebApplicationContext: Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Wed Jun 15 09:15:21 BST 2016]; root of context hierarchy 
2016-06-15 09:15:21,198 INFO [localhost-startStop-1] XmlBeanDefinitionReader: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml] 
2016-06-15 09:15:21,683 INFO [localhost-startStop-1] DefaultListableBeanFactory: Overriding bean definition for bean 'org.springframework.context.config.internalBeanConfigurerAspect' with a different definition: replacing [Root bean: class [org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=aspectOf; initMethodName=null; destroyMethodName=null] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.context.annotation.aspectj.SpringConfiguredConfiguration; factoryMethodName=beanConfigurerAspect; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/context/annotation/aspectj/SpringConfiguredConfiguration.class]] 
2016-06-15 09:15:21,866 INFO [localhost-startStop-1] PropertySourcesPlaceholderConfigurer: Loading properties file from class path resource [core.properties] 
2016-06-15 09:15:21,876 INFO [localhost-startStop-1] DefaultContextLoadTimeWeaver: Could not obtain server-specific LoadTimeWeaver: Could not initialize TomcatLoadTimeWeaver because Tomcat API classes are not available 
2016-06-15 09:15:21,877 INFO [localhost-startStop-1] DefaultContextLoadTimeWeaver: Found Spring's JVM agent for instrumentation 
[[email protected]34e5c9] info AspectJ Weaver Version 1.8.9 built on Monday Mar 14, 2016 at 21:18:16 GMT 
[[email protected]] info register classloader [email protected] 

**[[email protected]] info using configuration /Users/SearchV2/content/target/classes/META-INF/aop.xml** 

**[[email protected]] info using configuration file:/Users/.m2/repository/org/springframework/spring-aspects/4.3.0.RELEASE/spring-aspects-4.3.0.RELEASE.jar!/META-INF/aop.xml** 

** REGISTERING MY ASPECT ** 
[[email protected]] info register aspect com.test.server.graph.aop.LazyFetchAspect 

PostProcessorRegistrationDelegate$BeanPostProcessorChecker: Bean 'neo4jConfig' of type [class com.test.server.graph.config.Neo4jConfig$$EnhancerBySpringCGLIB$$f406a5c8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
2016-06-15 09:15:22,267 INFO [localhost-startStop-1] Neo4jConfiguration: Initialising PersistenceExceptionTranslationPostProcessor 
[[email protected]] weaveinfo Join point 'method-execution(java.lang.String com.test.server.graph.domain.model.DatabaseObject.getStId())' in Type 'com.test.server.graph.domain.model.DatabaseObject' (DatabaseObject.java:82) advised by around advice from 'com.test.server.graph.aop.LazyFetchAspect' (LazyFetchAspect.java) 

....許多getters被建議。

做一些查詢,我得到下面的異常後:

SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.NoSuchMethodError: com.test.server.graph.aop.LazyFetchAspect.aspectOf()Lcom/test/server/graph/aop/LazyFetchAspect;] with root cause 
java.lang.NoSuchMethodError: com.test.server.graph.aop.LazyFetchAspect.aspectOf()Lcom/test/server/graph/aop/LazyFetchAspect; 
    at com.test.server.graph.domain.model.DatabaseObject.getStId(DatabaseObject.java:82) 
相關問題