Note that auto-completion in Eclipse and STS is CTRL+Space
, even on a Mac.
Consider writing @Configuration
classes where each bean method is defined as a skeleton, just
returning null. If you do this first:
@Configuration
public class AppConfig {
@Bean
public AccountService accountService() {
return null;
}
@Bean
public AccountRepository accountRepository() {
return null;
}
@Bean
public CustomerRepository customerRepository() {
return null;
}
}
Dependency injection should be performed one of three ways.
Local dependency, invoke the bean method:
@Bean
public AccountService accountService() {
return new AccountService(accountRepository(), customerRepository());
}
External dependency, defined in a different @Configuration
class. Option 1: Use @Autowired
:
@Configuration
public class AppConfig {
private DataSource dataSource;
@Autowired
public AppConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public CustomerRepository customerRepository() {
return new CustomerRepository(dataSource);
}
}
External dependency, defined in a different @Configuration
class. Option 2: Use a method
parameter:
@Configuration
public class AppConfig {
@Bean
public CustomerRepository customerRepository(DataSource dataSource) {
return new CustomerRepository(dataSource);
}
}