0

In my Spring boot application, I have a dependency Spring project. From my SpringBoot class, I am trying to call a dependency class but the class has no value set.

@CrossOrigin(origins = "*")
@RestController

@Configuration
@ComponentScan(basePackages = "com.dependency.package")
public class BootClass {


    private DependencyClass dependencyClass;


    public BootClass() {
        this.dependencyClass = new DependencyClass();

    }
}

My DependencyClass object just gives me an empty object {}. Any ideas?

My Dependency class looks like this:

@Component
public class DependencyClass {


    @Value("${jdbc.driver.class.name}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

}

Thank you,

Julian

Julian
  • 781
  • 1
  • 11
  • 32

3 Answers3

1

This is the classic Spring beginner mistake: calling new to instantiate an object.

You cannot call new if you want Spring to manage beans and provide dependencies. You have to give it to the Spring bean factory to manage. It's all or none.

Yours looks like a perfect case for constructor injection.

@Configuration
@ComponentScan(basePackages = "com.dependency.package")
public class BootClass {


    private final DependencyClass dependencyClass;


    @Autowired
    public BootClass(@Qualifier(name = "dependencyClass") DependencyClass dependencyClass) {
        this.dependencyClass = dependencyClass;

    }
}

Surely you can think of a better name for DependencyClass. I'd suggest something like DatabaseConfiguration.

That is not to say that every object should be under the control of the bean factory, or that new should never be called. Objects with short scope that aren't shared can certainly be instantiated.

It's objects that require dependency injection that need to be under the control of the bean factory.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Hi duffymo, what version of springboot is this? I am using "1.4.3.RELEASE". @Qualifier(name = "") gives me an error. should it be value instead of name? I used the name dependencyClass only as an example to highlight the different relationships, i should prob have been more clear – Julian Apr 12 '18 at 09:15
  • Yes, fix the error. – duffymo Apr 12 '18 at 09:22
0

Just Mark that with @Autowired Annotation as shown below. That would do.

@Autowired
private DependencyClass dependencyClass;
Arun
  • 3,440
  • 11
  • 60
  • 108
  • This does not work without the inclusion of the dependency's properties file, which seems to be my problem – Julian Apr 12 '18 at 09:18
0

Extending the answer of @duffymo:

Once you are done what he suggested, also make sure that "jdbc.driver.class.name" and "jdbc.url" are present in your main project properties file. If you are expecting that these values will be populated just because you have it in properties file of "Dependent" project, you might get disappointed. Check this for more details:

https://stackoverflow.com/a/49564724/3458292

Agam
  • 1,015
  • 2
  • 11
  • 21
  • Hi Agam, I think this might be my problem. I can get the dependency object using "@ComponentScan" but because the properties file isn't there which might be causing the values to be not populated. thank you, i will try this and report back – Julian Apr 12 '18 at 09:17