spring eclipselink 静态编织未启用或未发生

spring eclipselink static weaving was not enabled or did not occur

项目用途:

在这个项目中,我们使用提供的带有实体的 jar,并且我们创建了没有实体的配置 persistence.xml。该项目创建为 war 以部署在 weblogic 12c 服务器中,但我们正在使用 springboot 内置 tomcat 服务器进行测试。偶尔在带有 Weblogic 的测试服务器上。

当我们在任一容器中启动应用程序时,我们都会收到很多 EL warning,如下所示:

[EL Warning]: metadata: 2019-02-28 17:10:14.684--ServerSession(1764986459)--Reverting the lazy setting on the OneToOne or ManyToOne attribute [readonlyUserInformation] for the entity class [class com.adquira.mkp.persistence.entities.auditory.AuditoryEvent] since weaving was not enabled or did not occur.

在到处搜索和寻找如何解决这个问题之后,我们发现的唯一类似问题是 this question about static weaving not working in springboot

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.adus</groupId>
    <artifactId>adus-backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>adus-backend</name>
    <description>Adus back-end development</description>

    <properties>
        <java.version>1.8</java.version>
        <springfox-swagger.version>2.9.2</springfox-swagger.version>
        <eclipselink.version>2.6.5</eclipselink.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.adquira.orm</groupId>
            <artifactId>adquira-orm</artifactId>
            <version>1.0.20</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.2</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>   
        <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>${eclipselink.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.core</artifactId>
            <version>${eclipselink.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>${eclipselink.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置class

package com.adus.adusbackend;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;


@Configuration
@EnableJpaRepositories(basePackages = { "com.adus.adusbackend.repository.user","com.adus.adusbackend.repository.market"})
public class DatasourceConfiguration  {

    @Bean(destroyMethod = "close")
    @ConfigurationProperties(prefix = "spring.datasource")
      public DataSource dataSource() {
        return DataSourceBuilder.create().build();
      }

    @Bean
    EclipseLinkJpaVendorAdapter jpaVendorAdapter() {
        EclipseLinkJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
        jpaVendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.oracle.Oracle12Platform");
        jpaVendorAdapter.setGenerateDdl(Boolean.FALSE);
        jpaVendorAdapter.setShowSql(Boolean.TRUE);

        return jpaVendorAdapter;
    }

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
        entityManagerFactoryBean.setJpaDialect(new EclipseLinkJpaDialect());

        // Instead of persistence.xml
        entityManagerFactoryBean.setPersistenceUnitName("des");
        entityManagerFactoryBean.setPackagesToScan("com.adquira.mkp.persistence.entities");

        Properties jpaProperties = new Properties();
        jpaProperties.put(PersistenceUnitProperties.WEAVING, detectWeavingMode());
        jpaProperties.put(PersistenceUnitProperties.DDL_GENERATION, "none");
        entityManagerFactoryBean.setJpaProperties(jpaProperties);

        entityManagerFactoryBean.afterPropertiesSet();

        return entityManagerFactoryBean;
    }

    @Bean
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

    private String detectWeavingMode() {
        return InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? "true" : "static";
    }
}

您有两个挑战。

首先:要完全实现静态编织并增强实体,您需要一个正确配置的 Maven 插件来做到这一点。您需要将 weaver 插件添加到您的插件部分。来自 Eclipselink Wiki-page 的示例:

<plugins>
    ...
    <plugin>
        <groupId>de.empulse.eclipselink</groupId>
        <artifactId>staticweave-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
            <execution>
                <phase>process-classes</phase>
                <goals>
                    <goal>weave</goal>
                </goals>
                <configuration>
                    <persistenceXMLLocation>
                        META-INF/persistence.xml</persistenceXMLLocation>
                    <logLevel>FINE</logLevel>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.eclipse.persistence</groupId>
                <artifactId>org.eclipse.persistence.jpa</artifactId>
                <version>${eclipselink.version}</version>
            </dependency>
        </dependencies>
    </plugin>
    ...
</plugins>

其次:您需要让插件知道外部 jar 中的 类。关于它的文章很少,我不确定它是否可能(容易)。 wiki 页面仅提及 编织源 ,它可以在 jar 中,但没有直接说明是否也可以编译 类。

出于这个原因,我总是让我的实体库 jar 在从源代码编译时很容易得到增强。但是无论如何,有一些相关的帖子,例如 this.