SpringBoot应用在使用attributeconverter时无法完成启动。

huangapple go评论86阅读模式
英文:

SpringBoot application does not finish starting, when using attributeconverter

问题

EDITED: 此应用在添加最新的 AttributeConverter 代码之前可以正常启动和运行。我怀疑这是由于自动装配的顺序问题。因为如果我从属性转换器中删除自动装配的 bean,应用程序就可以正常启动。

Edited: cipherCreator 代码,SecretKey 代码

我正在尝试在将值持久化到数据库时使用加密。我实现了一个 Attribute 转换器类,还有另一个类用于初始化密码器并注入密钥的配置值。

当我为实体类添加了用于加密的 AttributeConverter 后,框架无法完成启动。

它在 ===> 触发延迟初始化Spring Data存储库 后停止。它不会抛出任何异常来停止,只是停在那里。如果我从 Cipher creator 中移除 Autowired 注解,它就会启动。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-08-22 17:10:50.808  INFO 23468 --- [           main] c.c.b.data.xyzApplication   : 激活的配置文件:secret,默认
2020-08-22 17:10:53.811  INFO 23468 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : 以 DEFERRED 模式引导 Spring Data JPA 存储库。
2020-08-22 17:10:54.165  INFO 23468 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : 在 340 毫秒内完成了 Spring Data 存储库扫描。找到 7 个 JPA 存储库接口。
2020-08-22 17:10:54.812  INFO 23468 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=65cd15a6-6963-3882-983e-554bc1ab6bc0
2020-08-22 17:10:55.187  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : 类型为 [org.springframework.cloud.openfeign.FeignClientFactoryBean] 的 Bean 'com.xyz' 不符合所有 BeanPostProcessor 的处理条件(例如:不符合自动代理条件)
2020-08-22 17:10:55.189  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : 类型为 [org.springframework.cloud.openfeign.FeignClientFactoryBean] 的 Bean 'com.xyz' 不符合所有 BeanPostProcessor 的处理条件(例如:不符合自动代理条件)
2020-08-22 17:10:55.190  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : 类型为 [org.springframework.cloud.openfeign.FeignClientFactoryBean] 的 Bean 'com.xyz' 不符合所有 BeanPostProcessor 的处理条件(例如:不符合自动代理条件)
2020-08-22 17:10:55.192  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : 类型为 [org.springframework.cloud.openfeign.FeignClientFactoryBean] 的 Bean 'com.xyz' 不符合所有 BeanPostProcessor 的处理条件(例如:不符合自动代理条件)
2020-08-22 17:10:55.194  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : 类型为 [org.springframework.cloud.openfeign.FeignClientFactoryBean] 的 Bean 'com.xyz' 不符合所有 BeanPostProcessor 的处理条件(例如:不符合自动代理条件)
2020-08-22 17:10:55.346  INFO 23468 --- [           main] trationDelegate$BeanPostProcessorChecker : 类型为 [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] 的 Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' 不符合所有 BeanPostProcessor 的处理条件(例如:不符合自动代理条件)
2020-08-22 17:10:56.480  INFO 23468 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : 使用端口号初始化了 Tomcat:8080(http)
2020-08-22 17:10:56.502  INFO 23468 --- [           main] o.apache.catalina.core.StandardService   : 正在启动服务 [Tomcat]
2020-08-22 17:10:56.502  INFO 23468 --- [           main] org.apache.catalina.core.StandardEngine  : 正在启动 Servlet 引擎:[Apache Tomcat/9.0.35]
2020-08-22 17:10:56.709  INFO 23468 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : 正在初始化 Spring 嵌入式 WebApplicationContext
2020-08-22 17:10:56.709  INFO 23468 --- [           main] o.s.web.context.ContextLoader            : 根 WebApplicationContext:初始化完成,用时 5867 毫秒
2020-08-22 17:10:57.755  INFO 23468 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : 正在初始化 ExecutorService 'applicationTaskExecutor'
2020-08-22 17:10:57.840  INFO 23468 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - 正在启动...
2020-08-22 17:11:00.487  INFO 23468 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - 启动完成。
2020-08-22 17:11:00.747  INFO 23468 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: 正在处理 PersistenceUnitInfo [name: default]
2020-08-22 17

<details>
<summary>英文:</summary>

EDITED: This application starts fine and runs fine before adding the latest AttributeConverter code. I suspect it is due to the autowiring order. because If I remove the the autowired bean from the attribute converter the application starts fine.

Edited: code of cipherCreator, SecretKey

I am trying to use encryption when persisting values to a database. I implemented an Attribute converter class and and another class for initializing the cipher as well as injecting a config value for the key.

When I decorate my entity class with the AttributeConverter for encryption, the framework does not finish starting. 

It stops after ===&gt; **Triggering deferred initialization of Spring Data repositories.** It does not throw any exceptions to stop. just sits there. If I remove the Autowired annotation from the Cipher creator it starts. 

. ____ _ __ _ _
/\ / ' __ _ () __ __ _ \ \ \
( ( )_
_ | '_ | '| | ' / ` | \ \ \
\/ )| |)| | | | | || (| | ) ) ) )
' |
| .__|| ||| |_, | / / / /
=========|
|==============|/=////
:: Spring Boot :: (v2.3.0.RELEASE)

2020-08-22 17:10:50.808 INFO 23468 --- [ main] c.c.b.data.xyzApplication : The following profiles are active: secret,default
2020-08-22 17:10:53.811 INFO 23468 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-08-22 17:10:54.165 INFO 23468 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 340ms. Found 7 JPA repository interfaces.
2020-08-22 17:10:54.812 INFO 23468 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=65cd15a6-6963-3882-983e-554bc1ab6bc0
2020-08-22 17:10:55.187 INFO 23468 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.189 INFO 23468 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.190 INFO 23468 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.192 INFO 23468 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.194 INFO 23468 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'com.xyz' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:55.346 INFO 23468 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a2393a66] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-08-22 17:10:56.480 INFO 23468 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-22 17:10:56.502 INFO 23468 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-22 17:10:56.502 INFO 23468 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-08-22 17:10:56.709 INFO 23468 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-22 17:10:56.709 INFO 23468 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5867 ms
2020-08-22 17:10:57.755 INFO 23468 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-22 17:10:57.840 INFO 23468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-08-22 17:11:00.487 INFO 23468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-08-22 17:11:00.747 INFO 23468 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-08-22 17:11:00.879 INFO 23468 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.15.Final
2020-08-22 17:11:01.165 WARN 23468 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-08-22 17:11:01.307 INFO 23468 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-08-22 17:11:01.467 INFO 23468 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories�
2020-08-22 17:11:01.951 INFO 23468 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-08-22 17:11:02.470 WARN 23468 --- [ task-1] org.hibernate.cfg.AnnotationBinder :

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Base64;

@Component
@Converter
public class CustomerEncryptor implements AttributeConverter<String, String>{

@Autowired
CipherCreator cipherCreator;

@Override
public String convertToDatabaseColumn(String attribute) {
    if(StringUtils.isEmpty(attribute))
        return attribute;
    try {
        return do something...
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
public String convertToEntityAttribute(String dbData) {
    if(StringUtils.isEmpty(dbData))
        return dbData;
    try {
        do something...
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}

}

I have noticed that if I do not use the 
[@]Autowired
CipherCreator cipherCreator;
the application starts. 

any ideas are appreciated

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

@Setter
@Getter
@Component
public class CipherCreator {
private static String ALGORITHM = "AES";
private Key key;
private Cipher cipherEnc;
private Cipher cipherDec;

@Autowired
public CipherCreator(SecretKeyProperty keyProperty) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    key = new SecretKeySpec(keyProperty.getSECRETKEY().getBytes(), ALGORITHM);

    cipherEnc = Cipher.getInstance(ALGORITHM);
    cipherEnc.init(Cipher.ENCRYPT_MODE, key);
    cipherDec = Cipher.getInstance(ALGORITHM);
    cipherDec.init(Cipher.DECRYPT_MODE, key);
}

}

@Component
public class SecretKeyProperty {

public String SECRETKEY;


@Autowired
public SecretKeyProperty(@Value(&quot;${encryption.key}&quot;) final String key)  {
    this.SECRETKEY = key;
}

public String getSECRETKEY() {
    return SECRETKEY;
}

public void setSECRETKEY(String sECRETKEY) {
    SECRETKEY = sECRETKEY;
}

}


</details>


# 答案1
**得分**: 3

这是由于对[JPA中的引导模式进行更改](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#bootstrapmode-for-jpa-repositories)导致的结果。引用更改内容如下:

&gt; 将通常的默认值更改为deferred - 它并行引导EntityManagerFactory,并延迟将存储库初始化推迟到ApplicationContext初始化结束

为了解决您的问题,请使用引导模式default (`spring.data.jpa.repositories.bootstrap-mode=default`)。

*(感谢VMware的St&#233;phane Nicoll在此事中提供的帮助,详见[spring github的讨论](https://github.com/spring-projects/spring-boot/issues/24027))*


<details>
<summary>英文:</summary>

This is due to the result of a change to the [boostrap mode in JPA](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#bootstrapmode-for-jpa-repositories). To quote the change:

&gt; Change the general default to deferred – it parallelizes the bootstrap of the EntityManagerFactory and delays repository initialization to the end of the ApplicationContext initialization

To fix your issue, use bootstrap mode default (`spring.data.jpa.repositories.bootstrap-mode=default`).


*(Props to St&#233;phane Nicoll at VMware for his help with this, see [this discussion at spring github](https://github.com/spring-projects/spring-boot/issues/24027))*

</details>



# 答案2
**得分**: 1

尝试将 `@Component` 注解添加到你的 `AttributeConverter` 上,但不要添加 `@Converter` 注解。

我猜问题是由于 Spring 创建其 bean 与 JPA 创建其 bean 之间某种相互竞争的情况造成的,这得益于两个社区努力使 [`AttributeConverter` 与 Spring bean 兼容][1]。在我的项目中,仅使用 `@Component` 就可以:我想此时 Spring 会创建 _所有_ bean,包括 `AttributeConverter` bean,然后 JPA 在之后借用它... 而不是 Spring 和 JPA 竞争创建 bean,使用彼此的依赖,这可能会导致相互竞争。

[1]: https://stackoverflow.com/a/54686119/6751571

<details>
<summary>英文:</summary>

Try adding the `@Component` annotation to your `AttributeConverter`, but _not_ the `@Converter` annotation.

I guess the issue is due to some kind of interlocking between Spring creating its beans, and JPA creating its beans, and both being aware of the other, thanks to both communities’ efforts to make [`AttributeConverter` compatible with Spring beans][1].
In my project, just using `@Component` works: I suppose Spring then creates _all_ beans, including the `AttributeConverter` bean, which is borrowed by JPA afterwards… instead of Spring _and_ JPA competing to create beans, using each others’ dependencies, which supposedly results in an interlocking.


  [1]: https://stackoverflow.com/a/54686119/6751571

</details>



# 答案3
**得分**: -1

Spring Boot嵌入式Tomcat可以通过spring-boot-starter或spring-boot-starter-web(flux)启动。请确保在pom文件中包含这些依赖。

<details>
<summary>英文:</summary>

Spring boot embedded tomcat starts either with the spring-boot-starter or with the spring-boot-starter-web(flux). Make sure you have those dependencies in the pom

</details>



huangapple
  • 本文由 发表于 2020年8月23日 08:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63542462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定