创建名称为 bean 时出错:自动装配依赖项注入失败,无法解析占位符

Ama*_*los 13 java spring spring-boot

我正在尝试在 tomcat 服务器上运行我的应用程序(spring + hibernate),但出现这些错误。当我评论导致错误的属性时,下一个属性会触发相同的错误。我猜想在初始化期间无法识别这些属性,我尝试了很多解决方案,但没有一个解决问题。有谁有办法解决这个问题吗?

堆栈跟踪

2019-06-17 15:07:19.706  INFO 12736 --- [on(5)-127.0.0.1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5442 ms
2019-06-17 15:07:19.737  WARN 12736 --- [on(5)-127.0.0.1] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'san.prm.webservices.config.WebConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'maxAttachmentSize' in value "${maxAttachmentSize}"
2019-06-17 15:07:19.752  INFO 12736 --- [on(5)-127.0.0.1] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-06-17 15:07:19.771 ERROR 12736 --- [on(5)-127.0.0.1] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'san.prm.webservices.config.WebConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'maxAttachmentSize' in value "${maxAttachmentSize}"
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:154) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:134) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:87) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE]
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169) [spring-web-4.3.17.RELEASE.jar:4.3.17.RELEASE]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244) [catalina.jar:8.0.32]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:8.0.32]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) [catalina.jar:8.0.32]
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) [catalina.jar:8.0.32]
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) [catalina.jar:8.0.32]
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1692) [catalina.jar:8.0.32]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:8.0.32]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [na:1.8.0_161]
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [na:1.8.0_161]
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:465) [catalina.jar:8.0.32]
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:415) [catalina.jar:8.0.32]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:8.0.32]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [na:1.8.0_161]
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [na:1.8.0_161]
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) [na:1.8.0_161]
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) [na:1.8.0_161]
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) [na:1.8.0_161]
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401) [na:1.8.0_161]
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) [na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:361) [na:1.8.0_161]
    at sun.rmi.transport.Transport$1.run(Transport.java:200) [na:1.8.0_161]
    at sun.rmi.transport.Transport$1.run(Transport.java:197) [na:1.8.0_161]
    at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_161]
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196) [na:1.8.0_161]
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568) [na:1.8.0_161]
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826) [na:1.8.0_161]
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683) [na:1.8.0_161]
    at java.security.AccessController.doPrivileged(Native Method) [na:1.8.0_161]
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682) [na:1.8.0_161]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_161]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_161]
    at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_161]
Run Code Online (Sandbox Code Playgroud)

WebConfig.java

@Configuration
@Import( { CoreConfig.class, PRMKeycloakRestClientConfiguration.class } )
@ComponentScan(basePackages = { "san.prm.webservices.controller", "san.prm.webservices.authentication"})
public class WebConfig {

    @Value("${maxAttachmentSize}")
    private long maxAttachmentSize;

    @Bean
    public ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }

    @Bean
    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }

    @Bean
    public ExceptionStatusService exceptionStatusService() {
        ExceptionStatusServiceImpl service = new ExceptionStatusServiceImpl();
        service.setDefaultStatus(HttpStatus.BAD_REQUEST);
        service.setExceptionStatus(getMappings());
        return service;
    }

    private Map<Class<? extends PRMException>, HttpStatus> getMappings() {
        ...
    }

    @Bean
    public Module jacksonPageWithJsonViewModule() {
        SimpleModule module = new SimpleModule("jackson-page-with-jsonview", Version.unknownVersion());
        module.addSerializer(Page.class, new PageSerializer());
        return module;
    }
Run Code Online (Sandbox Code Playgroud)

初始化程序.java

@Configuration
@Import( { CoreConfig.class, PRMKeycloakRestClientConfiguration.class } )
@ComponentScan(basePackages = { "san.prm.webservices.controller", "san.prm.webservices.authentication"})
public class WebConfig {

    @Value("${maxAttachmentSize}")
    private long maxAttachmentSize;

    @Bean
    public ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }

    @Bean
    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }

    @Bean
    public ExceptionStatusService exceptionStatusService() {
        ExceptionStatusServiceImpl service = new ExceptionStatusServiceImpl();
        service.setDefaultStatus(HttpStatus.BAD_REQUEST);
        service.setExceptionStatus(getMappings());
        return service;
    }

    private Map<Class<? extends PRMException>, HttpStatus> getMappings() {
        ...
    }

    @Bean
    public Module jacksonPageWithJsonViewModule() {
        SimpleModule module = new SimpleModule("jackson-page-with-jsonview", Version.unknownVersion());
        module.addSerializer(Page.class, new PageSerializer());
        return module;
    }
Run Code Online (Sandbox Code Playgroud)

应用程序属性

commonFilePath=${prm.app.commonfilepath}
maxAttachmentSize=${prm.app.maxattachmentsize}
documentFilePath=${prm.app.documentfilepath}
modeleDocumentFilePath=${prm.app.modeledocumentfilepath}
singleKey.keystore.path=${prm.app.singleKey.keystore.path}
genericPush.enabled=${prm.app.genericPush.enabled}
genericPush.keystore.path=${prm.app.genericPush.keystore.path}
genericPush.requester=${prm.app.genericPush.requester}
genericPush.stream=${prm.app.genericPush.stream}
genericPush.url=${prm.app.genericPush.url}
droitsFormulaires.enabled=false
clamav.host=${prm.app.clamav.host}
clamav.port=${prm.app.clamav.port}
Run Code Online (Sandbox Code Playgroud)

Ama*_*los 4

通过在pom中添加这一行解决了这个问题

<filtering>true</filtering>
Run Code Online (Sandbox Code Playgroud)

给予这个

<build>
    <resources>     
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <includes>                      
                <include>**/*.properties</include>                  
            </includes>
        </resource>            
    </resources>
</build>
Run Code Online (Sandbox Code Playgroud)