简体   繁体   English

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

[英]Error creating bean with name: Injection of autowired dependencies failed, could not resolve placeholder

I am trying to run my application (spring + hibernate) on a tomcat server but I am getting these errors. 我试图在tomcat服务器上运行我的应用程序(spring + hibernate),但是我收到了这些错误。 When I comment the property causing the errors, the next one triggers the same errors. 当我评论导致错误的属性时,下一个会触发相同的错误。 I guess the properties are not recognized during initialization, I tried many solutions but none of them resolved the problem. 我想在初始化期间无法识别属性,我尝试了很多解决方案,但没有一个解决了问题。 Has anyone any idea to resolve this? 有谁有任何想法解决这个问题?

Stacktrace 堆栈跟踪

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]

WebConfig.java 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;
    }

Initializer.java Initializer.java

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"san.prm.webservices.controller"})
@EnableCaching
@EnableTransactionManagement
@Import({ WebConfig.class, WebSecurityConfig.class })
public class Initializer extends SpringBootServletInitializer implements WebApplicationInitializer {

}

app.properties app.properties

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}

have you tried to rename your 'app.properties' file 'application.properties'? 你试过重命名'app.properties'文件'application.properties'吗? What is the path of this file ? 这个文件的路径是什么? It has to be under 'resources' directory if you don't want to manually configure its path. 如果您不想手动配置其路径,则必须位于“资源”目录下。

Make sure your properties file resides in resources and you're copying it during compilation. 确保您的属性文件驻留在资源中,并且您在编译期间将其复制。

Also, the name needs to be application.properties or application-{profile}.properties. 此外,名称必须是application.properties或application- {profile} .properties。 Read this for more info. 阅读本文以获取更多信息。

Configuration | 配置| Spring Framework Docs Spring Framework文档

If not, you can use @PropertySource to specify properties file if you're not naming it as per convention or external configurations. 如果没有,您可以使用@PropertySource指定属性文件,如果您没有按照约定或外部配置命名它。

PropertySource | PropertySource | Spring Framework Docs Spring Framework文档

Property Source Example | 物业来源示例| Mkyong Mkyong

I had app.properties and application.properties, I gathered these two files in application.properties and deleted the other but it does not change anything. 我有app.properties和application.properties,我在application.properties中收集了这两个文件并删除了另一个,但它没有改变任何东西。 The file is in "resources" directory and seems to be recognized by Intellij. 该文件位于“resources”目录中,似乎被Intellij识别。

resources directory 资源目录

I added this in the pom.xml 我在pom.xml中添加了这个

<build>
    <resources>     
        <resource>
            <directory>src/main/resources</directory>
            <includes>                      
                <include>**/*.properties</include>                  
            </includes>
        </resource>            
    </resources>
</build>

and the following line in WebConfig but it is still the same. 和WebConfig中的以下行但它仍然是相同的。

@PropertySource("classpath:/application.properties")

The application I'm trying to run is a fork of an application that used exactly the same properties, and that runs well.. 我正在尝试运行的应用程序是一个应用程序的分支,它使用完全相同的属性,运行良好..

Resolved it by adding this line in the pom 通过在pom中添加此行来解决它

<filtering>true</filtering>

giving this 给这个

<build>
    <resources>     
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <includes>                      
                <include>**/*.properties</include>                  
            </includes>
        </resource>            
    </resources>
</build>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建bean时出错,自动连接依赖项注入失败,无法自动连接字段 - Error creating bean, Injection of autowired dependencies failed, Could not autowire field BeanCreationException:创建名称为&#39;userController&#39;的bean时出错:自动连接依赖项的注入失败 - BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed 创建名称为&#39;empController&#39;的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'empController': Injection of autowired dependencies failed 创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败 - Error creating bean with name 'securityConfig': Injection of autowired dependencies failed 创建名称为&#39;initDbService&#39;的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'initDbService': Injection of autowired dependencies failed 创建名称为&#39;reportsController&#39;的bean时出错:自动连接的依赖项注入失败; - Error creating bean with name 'reportsController': Injection of autowired dependencies failed; 创建名称为“ homeController”的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'homeController': Injection of autowired dependencies failed 创建名为“countriesDao”的 bean 时出错:注入自动装配的依赖项失败; - Error creating bean with name 'countriesDao': Injection of autowired dependencies failed; 创建名称为&#39;serviceController&#39;的bean时出错:自动连接的依赖项注入失败; - Error creating bean with name 'serviceController': Injection of autowired dependencies failed; 创建名称为&#39;employeeController&#39;的bean时出错:自动连接依赖项的注入失败 - Error creating bean with name 'employeeController': Injection of autowired dependencies failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM