Java 创建spring boot应用程序并可以';我不能让@autowired工作
如果我硬编码我的输入文件位置,我就可以很好地运行和构建它,但是当我尝试使用autowired填充字段时,它就死了,并且不会启动应用程序,因为它找不到我创建的bean。所以我会给你们一个我拥有的分类,欢迎任何反馈或想法 注意 我运行的是springboot版本1.4.2,因为我使用的是Velocity,之后它就不受支持了,所以在看这篇文章时请记住这一点。如果您需要有关我的设置的其他信息,或者您有任何澄清问题,请告诉我。谢谢 好的,这是我的布局: application.ymlJava 创建spring boot应用程序并可以';我不能让@autowired工作,java,spring,spring-boot,gradle,yaml,Java,Spring,Spring Boot,Gradle,Yaml,如果我硬编码我的输入文件位置,我就可以很好地运行和构建它,但是当我尝试使用autowired填充字段时,它就死了,并且不会启动应用程序,因为它找不到我创建的bean。所以我会给你们一个我拥有的分类,欢迎任何反馈或想法 注意 我运行的是springboot版本1.4.2,因为我使用的是Velocity,之后它就不受支持了,所以在看这篇文章时请记住这一点。如果您需要有关我的设置的其他信息,或者您有任何澄清问题,请告诉我。谢谢 好的,这是我的布局: application.yml atb: #jo
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
@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();
}
}
@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.
}
@SpringBootApplication
@Component
public class AgttransApplication
{
public static void main(String[] args)
{
SpringApplication.run(AgttransApplication.class, args);
}
}
ProcessController.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();
}
}
@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.
}
@SpringBootApplication
@Component
public class AgttransApplication
{
public static void main(String[] args)
{
SpringApplication.run(AgttransApplication.class, args);
}
}
}
AgttransApplication.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();
}
}
@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.
}
@SpringBootApplication
@Component
public class AgttransApplication
{
public static void main(String[] args)
{
SpringApplication.run(AgttransApplication.class, args);
}
}
文件结构
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
运行时输出
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'.
尝试将
@Value(${inputDirectory}”)
更改为@Value(${atb:inputDirectory}”)
或@Value(${atb.inputDirectory}”)
对于字符串,不需要定义bean和使用autowire,只需使用字段上的@Value
注释:
@Value("${inputDirectory}")
private String inputDirectory;
而且,由于您使用的是yaml,正如@AHungerArtist所说的,您需要使用
${atb.inputDirectory}
,所以我的项目设置与此无关。我不得不硬编码我们的常春藤存储库的依赖项。到常春藤存储库的默认链接与该过程有关,并且创建了周期性依赖项,以及我们的项目不需要的依赖项。这是我们用来硬编码常春藤存储库链接的代码
ivy {
url "//ourserver/Repository/ivy-thirdparty"
layout "pattern", {
ivy "[organisation]/[module]/[revision]/[module]-[revision].xml"
artifact "[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]"
}
}
希望这个答案能帮助其他研究这个问题的人。但是这里发布的代码是干净的,如果任何人需要模板,它也可以用于其他设置。请添加stacktraceDidn,否则将无法再次使用。我以前也试过。对不起,我没提那件事。不过,我现在确实没有这样做,因为正如@Nir-Levy所说,无论如何都需要这样做。谢谢你的关注。不幸的是,它没有。我想可能Spring1.4不支持。yml???我现在尝试使用资源文件的旧.properties方法。如果它改变了什么,我会发布的。不,这也没用。。。这是非常令人沮丧的。。。我已经有3个开发人员像我一样困惑于这一点。他们都看了我的代码,除了你们指出的以外,没有发现任何错误。这些事情现在都解决了。。。不确定是什么问题。将它添加到主文本以便我可以使用代码标记。我尝试将自动连线输出,并以这种方式执行,但得到了完全相同的错误。这与无法识别.yml文件有关。我添加了另一个应用程序-default.yml,并将spring环境设置为完全无效,它也做了同样的事情。它没有将application.yml文件视为有效文件。我会把我的文件树放在这里,这样你就可以看到我的结构。也许是那里有什么东西。