【问题标题】:Spring, Hibernate checks every jar in my Maven Shade JARSpring,Hibernate 检查我的 Maven Shade JAR 中的每个 jar
【发布时间】:2012-09-25 03:44:45
【问题描述】:

当我从java -jar foo.jar 运行我的 jar 时,就像这些行数以千计一样,所以应用程序至少需要 1 分钟才能启动。

2012-10-04 09:31:56,543 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.DependsOn
2012-10-04 09:31:56,546 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.EnableAspectJAutoProxy
2012-10-04 09:31:56,548 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.EnableLoadTimeWeaving$AspectJWeaving
2012-10-04 09:31:56,553 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.EnableLoadTimeWeaving
2012-10-04 09:31:56,558 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.FilterType
2012-10-04 09:31:56,561 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.Import
2012-10-04 09:31:56,563 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.ImportAware
2012-10-04 09:31:56,567 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.ImportBeanDefinitionRegistrar
2012-10-04 09:31:56,571 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.ImportResource
2012-10-04 09:31:56,575 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.ImportSelector
2012-10-04 09:31:56,578 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.Jsr330ScopeMetadataResolver
2012-10-04 09:31:56,581 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.Lazy
2012-10-04 09:31:56,585 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.LoadTimeWeavingConfiguration
2012-10-04 09:31:56,589 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.LoadTimeWeavingConfigurer
2012-10-04 09:31:56,593 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.MetadataUtils
2012-10-04 09:31:56,596 [AWT-EventQueue-0] DEBUG org.hibernate.ejb.packaging.AbstractJarVisitor - Filtering: org.springframework.context.annotation.Primary

我正在使用 Maven Shade 创建我的 JAR:

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>

Spring 和 Hibernate 配置:

<bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager"
      class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

如何过滤它以不搜索 POM 下载的所有 JAR?

编辑:

完整的 Spring 配置:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<context:component-scan base-package="my.foo.bar" />

<context:property-placeholder location="classpath:/config/database.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

完全持久化:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
         version="1.0">
<persistence-unit name="persistenceUnit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="dialect.ImprovedH2Dialect"/>
        <property name="hibernate.default_schema" value="ags" />
        <property name="hibernate.show_sql" value="true"/>
        <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
    </properties>
</persistence-unit>

【问题讨论】:

标签: java spring hibernate maven maven-shade-plugin


【解决方案1】:

你需要在persistence.xml中调整一些设置,添加:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
         version="1.0">
<persistence-unit name="persistenceUnit">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>my.foo.bar</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.dialect" value="dialect.ImprovedH2Dialect"/>
        <property name="hibernate.default_schema" value="ags" />
        <property name="hibernate.show_sql" value="true"/>
        <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
    </properties>
</persistence-unit> 

并指定所有实体类。

http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/configuration.html

【讨论】:

  • 我将此行添加到我的persistence.xml 并且我的所有实体都已注释,仍然扫描我的 Shade JAR 中的每个 JAR。
  • 你能编辑你的问题并提供你完整的春季背景和完整的persistence.xml吗?
  • 我做到了,但仍然写了数千行。很奇怪。
【解决方案2】:

尝试添加到你的 persistence.xml

 <exclude-unlisted-classes>true</exclude-unlisted-classes>

 <class>my.foo.bar.Entity1</class>
 <class>my.foo.bar.Entity2</class>
...
 <class>my.foo.bar.EntityN</class>

并删除&lt;class&gt;my.foo.bar&lt;/class&gt;

而且,也许&lt;jar-file&gt;jar_with_my_foo_bar_Entity.jar&lt;/jar-file&gt; 会有所帮助

【讨论】:

  • 我已经拥有每个这样的实体。我创建了 1 个巨型 JAR 文件,其中包括我的实体和其他库。它是用Maven Shade 创建的
  • 因此,尝试将调试级别设置为 INFO,并将日志记录格式更改为没有类名的内容。但我认为你不能让它比现在快得多。
  • 是的,但这更像是一种黑客行为,并不能解决实际问题
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 2012-01-08
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 2016-04-22
相关资源
最近更新 更多