w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Reference a bean as part of a constructor to a different bean in java config
@Bean
public User user() {
    return new User("first", "second", "third");
}

@Bean
public Department department() {
    return new Department("fourth", "fifth", user());
}

Now you can user @Autowired in other classes that are in the container and inject your user or department (e.g. in a controller)

@Autowired
private User user; // will return the user from the user() java config
defined bean




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.