Primavera de arranque 2 no puede leer la propiedad como cuerdas

Lebowski:

Al migrar una aplicación de Primavera Inicio desde la primavera de arranque 1.4.0 a la primavera de arranque 2, empecé a recibir los errores en tratar de leer una propiedad de mi archivo .properties.

En el archivo de propiedades, la propiedad se define como:

environment=dev

Y en una de mis clases, Estoy importando la propiedad a través de la @Valueanotación, como esto:

@Getter
@Setter
public class CustomUserFilter extends SwitchUserFilter {
    ...
    @Value("${environment}")
    private String environment;
    ...

La clase por encima de las anulaciones org.springframework.security.web.authentication.switchuser.SwitchUserFilter, para permitir a los usuarios funciones de conmutación.

Hasta la primavera de arranque 1.4.0, yo era capaz de importar esta propiedad en mi clase como una cadena. Sin embargo, desde que la migración a la primavera de arranque 2, me sale el siguiente error de compilación:

Error: (43, 20) de Java: getEnvironment () en demo.config.CustomUserFilter no puede implementar getEnvironment () en org.springframework.core.env.EnvironmentCapable tipo de retorno java.lang.String no es compatible con org.springframework.core. env.Environment

No estoy seguro de por qué ocurre esto. También probé cambiando el tipo de esta variable para Environment (org.springframework.core.env.Environment)(como se muestra a continuación):

...
@Value("${environment}")
private Environment environment;
...

, Pero luego empecé a recibir el siguiente error:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'switchUserFilter': Unsatisfied dependency expressed through field 'environment'; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment': no matching editors or conversion strategy found
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1336)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:226)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:182)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAsRegistrationBean(ServletContextInitializerBeans.java:177)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addAdaptableBeans(ServletContextInitializerBeans.java:159)
    at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:81)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:261)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:234)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:185)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
    ... 52 more
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:60)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1089)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
    ... 70 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.core.env.Environment': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:299)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:117)
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:70)
    ... 74 more

Cualquier pistas sobre lo que podría estar mal? Estoy usando la primavera de arranque 2.0 , Spring 5 , Java 11 , y Tomcat 8.5.35 . ¡Gracias!

Lebowski:

El problema no estaba relacionado con la primavera de arranque. Mi clase anterior se extiende org.springframework.security.web.authentication.switchuser.SwitchUserFilter, que se extiende aún más org.springframework.web.filter.GenericFilterBean. Esta GenericFilterBeanclase también tiene un atributo private Environment environment;. Además, esta clase se modificó en la primavera de 5 liberación para incluir un método getter sobre este atributo. Spring 5 introdujo un getEnvironment()tipo con el regreso Environmenten clase GenericFilterBean, que estaba en conflicto con mi getEnvironment()con el tipo de retorno String.

Para evitar este conflicto, simplemente cambió el nombre de mi atributo de environmenta envy cosas trabajé después de eso.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=208898&siteId=1
Recomendado
Clasificación