简体   繁体   English

创建一个Spring Boot应用程序并且无法使@autowired工作

[英]Creating a spring boot app and can't get @autowired to work

I've got this running and building well if I hard code my input file location, but when I try to use autowired to get the field populated it just dies and won't start the app because it can't find the bean I created. 如果我对输入文件的位置进行硬编码,则我可以很好地运行它,并且构建良好,但是当我尝试使用自动装配来填充字段时,它就死了,并且因为无法找到我创建的bean而无法启动应用程序。 So I'll give you guys a breakdown of what I have, any feedback or ideas are welcome! 因此,我将给大家详细介绍我所拥有的,欢迎任何反馈或想法!

note I'm running springboot version 1.4.2 because I'm using Velocity and it's not supported after that so keep that in mind as you're looking at this. note备注我正在运行springboot版本1.4.2,因为我使用的是Velocity,但此后不再受支持,因此在您查看此内容时请记住这一点。 Let me know if you need additional information about my set up or if you have any clarifying questions. 如果您需要有关我的设置的其他信息或有任何澄清的问题,请告诉我。 Thanks! 谢谢!

Ok, here's my layout: application.yml 好的,这是我的布局: application.yml

atb:
  #jons directory
  inputDirectory: C:/dev/git/agenttransferbatch/src/main/resources/testinputfile.txt
  #Geoff's directory
#  inputDirectoy: C:/dev/workspaces/agenttransferbatch/src/main/resources/testinputfile.txt
spring:dataSource:
    url: jdbc:db2://pmp.shelterinsurance.com
    driverClassName: com.ibm.db2.jcc.DB2Driver
    username: myusername
    password: mypassword

AppConfig.java AppConfig.java

@Configuration
public class AppConfig
{
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public String getInputDirectory(@Value("${inputDirectory}") String inputDirectory)
    {
        return inputDirectory;
    }

    @Bean
    public ProcessController getProcessController()
    {
        return new ProcessController();
    }
}

ProcessController.java ProcessController.java

@Component
public class ProcessController
{
    @Autowired
    String inputDirectory;

@PostConstruct
public void init()
{
    if(inputDirectory != null && inputDirectory.length() > 0)
    {
        processInputFile();
    }
    else
    {
        System.out.println("filePath is null or length 0");
    }
}

private void processInputFile()
{
    // code to process my input file is here, not relevant to this problem so won't bore you with it.
}

} }

AgttransApplication.java AgttransApplication.java

@SpringBootApplication
@Component
public class  AgttransApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(AgttransApplication.class, args);
    }
}

file structure 文件结构

src
  main
    java
      com.shelter.agttrans
        configs
          AppConfig
        dataaccessobjects
          IndividualDAO
          WholesaleDAO
        dataobjects
          TransDataObject
        services
          ProcessController
        AgttransApplication
      controllers
        templates
          XMLController
    output
    resources
      application.yml
  test
  .gitignore
  build.gradle
  gradle.properties

Output when it runs 运行时输出

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'processController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'inputDirectory' in string value "${inputDirectory}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:376) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:754) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at com.shelter.agttrans.AgttransApplication.main(AgttransApplication.java:13) [main/:na]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'inputDirectory' in string value "${inputDirectory}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:219) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:193) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:813) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1079) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1022) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1131) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:589) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:370) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    ... 16 common frames omitted
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bootRun'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_152\bin\java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 9.061 secs
Process 'command 'C:\Program Files\Java\jdk1.8.0_152\bin\java.exe'' finished with non-zero exit value 1
10:43:28 AM: External task execution finished 'bootRun'.

For Strings, you don't need to define a bean and use autowire, you can simply use the @Value annotation on the field: 对于字符串,您不需要定义bean并使用自动装配,只需在字段上使用@Value批注:

@Value("${inputDirectory}")
private String inputDirectory;

and also, since you are using yaml, as @AHungerArtist stated, you need to use ${atb.inputDirectory} 并且,由于您正在使用yaml(如@AHungerArtist所述),因此您需要使用${atb.inputDirectory}

尝试将@Value("${inputDirectory}")更改为@Value("${atb:inputDirectory}")@Value("${atb.inputDirectory}")

So it turns out it was nothing with my project set up. 因此事实证明,我的项目设置没有任何意义。 I had to hard code a dependency to our ivy repository. 我不得不硬编码对我们的常春藤存储库的依赖。 The default link to the ivy repository had other things involved with the process and was creating cyclical dependencies and also dependencies we didn't need for our project. 到常春藤存储库的默认链接还有其他与流程有关的内容,并且创建了周期性依赖关系以及项目不需要的依赖关系。 This is the code we put in to hard code the ivy repository link. 这是我们在常春藤存储库链接中进行硬编码的代码。

ivy {
    url "//ourserver/Repository/ivy-thirdparty"
    layout "pattern", {
        ivy "[organisation]/[module]/[revision]/[module]-[revision].xml"
        artifact "[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]"
    }
}

Hopefully that answer will help anyone else looking at this problem. 希望这个答案可以帮助其他人解决这个问题。 But the code posted here is clean and will work for other setups as well if anyone needs a template. 但是这里发布的代码是干净的,如果有人需要模板,那么代码也可以用于其他设置。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM