一尘不染

使用在Tomcat上运行的Spring Data Neo4j 4 + AspectJ LTW进行延迟加载

tomcat

我尝试了所有可能的方法,试图使我的项目在Tomcat上运行,并切入模型(获取器)并使用AspectJ Load Time
Weaver进行编织。基本上,我遵循了Spring文档http://docs.spring.io/spring/docs/current/spring-
framework-reference/html/aop.html#aop-
atconfigurable中的所有步骤。我还遵循了[Neo4j / Spring-Data中提到的[懒惰/渴望加载/获取的相同方法。我的项目分为两个主要项目:-核心:spring-data-
neo4j(存储库和配置),专用包中的域模型LoggingAspect和LazyLoadingAspect。ps我没有在XML文件中使用任何配置。我纯粹在使用注释。-内容:在Tomcat上运行的Web应用程序,它取决于核心项目,并且在域项目中调用getter方法时希望进行编织。

运行内核本身,我设法使用maven插件使其运行并添加了Aspectj的依赖项。但是,当我移到Tomcat时,一切就开始了。我尝试了所有可能性,例如,使用-
javaagent,创建自定义context.xml,将spring-instrument放入tomcat / lib文件夹等,等等。我收到以下异常:

java.lang.IllegalStateException:后处理器试图将[com.test.server.graph.domain.model.Sequence]类型的bean实例替换为[org.springframework.beans.factory.aspectj。$ Proxy96]-方面配置的类不支持!
    在org.springframework.beans.factory.wiring.BeanConfigurerSupport.checkExposedObject(BeanConfigurerSupport.java:173)
    在org.springframework.beans.factory.wiring.BeanConfigurerSupport.configureBean(BeanConfigurerSupport.java:143)
    在org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect.configureBean(AnnotationBeanConfigurerAspect.aj:63)上
    在org.springframework.beans.factory.aspectj.AbstractDependencyInjectionAspect.ajc $在返回$ org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect $ 2 $ 1ea6722c(AbstractDependencyInjectionAspect.aj:88)之后
    在com.test.server.graph.domain.model.Sequence。(Sequence.java:29)
    在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处
    在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    在java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    在org.neo4j.ogm.annotations.EntityFactory.instantiate(EntityFactory.java:135)
    在org.neo4j.ogm.annotations.EntityFactory.instantiateObjectFromTaxa(EntityFactory.java:110)
    在org.neo4j.ogm.annotations.EntityFactory.newObject(EntityFactory.java:61)
    在org.neo4j.ogm.context.GraphEntityMapper.mapNodes(GraphEntityMapper.java:147)
    在org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:132)
    在org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:107)
    在org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:102)
    在org.neo4j.ogm.context.RestModelMapper.mapEntity(RestModelMapper.java:157)
    在org.neo4j.ogm.context.RestModelMapper.map(RestModelMapper.java:76)
    在org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQueriesDelegate.java:94)
    在org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQueriesDelegate.java:73)
    在org.neo4j.ogm.session.Neo4jSession.query(Neo4jSession.java:313)
    在org.springframework.data.neo4j.template.Neo4jTemplate.query(Neo4jTemplate.java:217)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在java.lang.reflect.Method.invoke(Method.java:497)
    在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    在com.sun.proxy。$ Proxy58.query(未知来源)

pom.xml(core.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文档的建议,我注释了Domain模型中的类。

@可配置

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

}

数据库对象

@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

延迟加载方面

@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() {
    }
}

阅读 343

收藏
2020-06-16

共1个答案

一尘不染

主要问题似乎是您正在将方面应用于bean:

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,而不是bean,并将它们从spring中排除。(确保在domain.model。*中没有spring注释)-也许如果您发布Sequence.java,我可以看到可能引起冲突的原因。

另一方面,您必须确保Aspectj仅分配给模型,而不分配给任何bean。因此,请确保您有一个/META-INF/aop.xml仅包含pojo模型:

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

我的pom.xml的一部分(我现在有一个不同的设置-这就是为什么spring版本这么旧的原因):

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

我没有任何特殊的构建插件或类似的插件

2020-06-16