0

I'm using @ConfigurationProperties for auto configuration of properties. My code is working in IDE. But when I run the jar in command line, it is not working.

Configuration class:

@Configuration
@ConfigurationProperties(prefix="location")
public class Location {

private String base;

public String getBase() {
    return base;
}

public void setBase(String base) {
    this.base = base;
}
}

Main class:

@SpringBootApplication
@EnableConfigurationProperties(Location.class)
@EnableAutoConfiguration
public class Application {

public static void main(String[] args) {

    SpringApplication.run(Application.class, args);
}
}

application.yml:

location:
 base: c:\test

If I autowire Location class, I see an instance created. But there is not property set. The code is not entering setBase() method.

The application prints this in the console.

AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' 
annotation found and supported for autowiring
Milad Bahmanabadi
  • 946
  • 11
  • 27
user2316771
  • 111
  • 1
  • 1
  • 11
  • See this: https://stackoverflow.com/questions/14708725/dependency-injection-with-spring-jsr-330-annotations-in-websphere-7-is-not-wor – Vishwa Ratna Jan 22 '19 at 05:22
  • 2
    Remove `@Configuration`... You either use an `@Component` (like `@Configuration`) or use `@EnableConfigurationProperties` don't mix both for the same class as you will end up with 2 instances. Also (non related) `@SpringBootApplication` already implies `@EnableAutoConfiguration`. – M. Deinum Jan 22 '19 at 06:36
  • This smells like therese another issue at the root cause. What does your packages look like and where are your (now) properties placed? – Darren Forsythe Jan 22 '19 at 21:40
  • com.ef.sr - This has main application class. com.ef.sr.config - This has location class. The application.properties is placed under src/main/resources – user2316771 Jan 22 '19 at 21:45

2 Answers2

1

Make sure that application.yml file is in the root of your classpath, usually it's put in the resources folder.

The fact that the setBase() method is not called suggests that your application.yml file is not being found. Spring looks in the root of your classpath for the application.yml file.

The comment from M. Deinum is correct saying that your duplicated annotations will result in 2 spring beans for Location class. However, as you say you managed to autowire the bean without getting an error it suggests that your Location class isn't in a package that is found by spring when it's scanning for beans. If there were 2 beans then you'd get an error when you autowired it. By default spring will scan use the package where the @SpringBootApplication is as the base. It will then look in this package and all sub packages.

If your package structure is like this...

myapp.main
    Application.java
myapp.config
    Location.java

Then you need to add scanBasePackages="myapp" to the @SpringBootApplication annotation.

Also change your main class and remove the @Enable.. annotations. i.e.:

@SpringBootApplication(scanBasePackages="myapp")
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }
}
pcoates
  • 2,102
  • 1
  • 9
  • 20
  • I have all the configuration and package structure. But nothing worked. Finally, I have to use properties file instead of yaml. – user2316771 Jan 22 '19 at 21:30
-1

As nothing worked with yaml, I had to change to property file and use

@PropertySource({"classpath:application.properties"})

for the spring to identify the properties file

user2316771
  • 111
  • 1
  • 1
  • 11