springboot应用程序在使用attributeconverter时未完成启动

zengzsys  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(627)

编辑:在添加最新的attributeconverter代码之前,此应用程序可以正常启动和运行。我怀疑是因为自动布线的缘故。因为如果我从属性转换器中删除autowiredbean,应用程序就可以正常启动。
编辑:密码创建者代码,secretkey
我尝试在将值持久化到数据库时使用加密。我实现了一个属性转换器类和另一个用于初始化密码以及为密钥注入配置值的类。
当我用attributeconverter装饰实体类进行加密时,框架不会完成启动。
它在==>触发spring数据存储库的延迟初始化之后停止。它不会抛出任何异常来停止。就坐在那里。如果我从密码创建者中删除自动连线注解,它将启动。

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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;
    }
}

我注意到如果我不使用

[@]Autowired
    CipherCreator cipherCreator;

应用程序启动。
有什么想法都可以

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("${encryption.key}") final String key)  {
        this.SECRETKEY = key;
    }

    public String getSECRETKEY() {
        return SECRETKEY;
    }

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

}
niknxzdl

niknxzdl1#

嵌入springboot的tomcat可以从springbootstarter启动,也可以从springbootstarterweb(flux)启动。确保pom中有这些依赖项

0qx6xfy6

0qx6xfy62#

这是由于jpa中boostrap模式的改变。引用更改:
将常规默认值更改为deferred–它并行化entitymanagerfactory的引导,并将存储库初始化延迟到applicationcontext初始化结束
要解决您的问题,请使用引导模式默认值( spring.data.jpa.repositories.bootstrap-mode=default ).
(圣道具)évmware的phane nicoll对此的帮助,请参阅spring github上的讨论)

jdg4fx2g

jdg4fx2g3#

尝试添加 @Component 对您的 AttributeConverter ,但不是 @Converter 注解。
我猜这个问题是由于spring创建了它的bean,jpa创建了它的bean,并且都意识到了另一个bean之间的某种连锁,这要感谢两个社区的努力 AttributeConverter 与 Spring Bean 兼容。在我的项目中,使用 @Component 作品:我想Spring创造了所有的豆子,包括 AttributeConverter bean,这是jpa后来借用的…而不是spring和jpa竞争创建bean,使用彼此的依赖关系,这可能会导致一个互锁的结果。

相关问题