Maybe I will get a lot of downvotes, but it so confuses for me the whole fact of using beans or not. Suppose this example
interface ICurrency {
String getSymbol();
}
public class CurrencyProcessor {
private ICurrency currency ;
public CurrencyProcessor(ICurrency currency) {
this.currency = currency;
}
public void doOperation(){
String symbol = currency.getSymbol();
System.out.println("Doing process with " + symbol + " currency");
}
}
So, to implement the ICurrency impl injection, I think I can do this in two ways:
Method 1: Without Spring beans
public class CurrencyOperator {
private ICurrency currency ;
private CurrencyProcessor processor;
public void operateDefault(){
currency = new USDollarCurrency();
processor = new CurrencyProcessor(currency)
this.processor.doOperation();
}
}
Where USDollarCurrency is an implementation of the ICurrency interface
Method 2: Using Spring beans
@ContextConfiguration(classes = CurrencyConfig.class)
public class CurrencyOperator {
@Autowired private ICurrency currency ;
@Autowired private CurrencyProcessor processor;
public void operateDefault(){
this.processor.doOperation();
}
}
@Configuration
public class CurrencyConfig {
@Bean
public CurrencyProcessor currencyProcessor() {
return new CurrencyProcessor(currency());
}
@Bean
public ICurrency currency() {
return new USDollarCurrency();
}
, Spring beans. , , , DI, , , , CurrencyProcessor, , , - , objets, ? , , :
1. beans ?
2. Spring , , ?
3. , ?