What is the difference between using or without Spring Beans?

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");
        // Some process...
    }

}

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. , ?

+4
2

, 2 DAO Oracle, MySQL, DAO. bean Spring. - DAO, Spring Oracle MySQL Spring @Autowired

, Oracle MySQL .

@Service
public class Business {
    @Autowired
    private Dao daoImpl;

    //Business methods that invoks Dao methods 
}

Spring ( XML) :

<bean id="daoImpl" class="app.com.MySQLDaoImpl OR app.com.OracleDaoImpl"/>

bean, , - -!
.

+2

Spring ! , coupling!

( , , JMS- ...).

, Spring ( ) , .

: Spring - , DI, . , Java (, JPA) DI.

+2

All Articles