4

Probably i'll get a lot of downvotes, but it's so confusing for me all this fact of whether use beans or not. Lets 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 inject the ICurrency impl injection i think that i can do it by two ways:


Way 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 ICurrency interface implementation


Way 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();
}

I really don't understand what would be the benefits of using Spring's beans. I read some things but what i most found was about the benefits of using DI, and as i understand, both ways are injecting the dependency that CurrencyProcessor require, what is changing is the way that i am creating and using objets, am i wrong? So in concrete, my questions are: 1. What are the benefits of using Beans at this case? 2. Why should i use Spring instead of doing it manually like first way? 3. Talking about performance, which of this cases is better?

jscherman
  • 5,839
  • 14
  • 46
  • 88
  • 3
    "as i understand, both ways are injecting the dependency that CurrencyProcessor require" - in what possible way is the first code injecting it? It's `CurrencyProcessor` itself that decides which `ICurrency` is used, because it news it up itself. There's no injection there... you've tightly coupled `CurrencyProcessor` to `USDollarCurrency`. – Jon Skeet Feb 26 '15 at 16:36
  • Also, there's much more to Spring Beans than dependency injection. Spring manages the full lifecycle of an object for you. Read [this](http://stackoverflow.com/questions/17193365/what-in-the-world-are-spring-beans). – Sotirios Delimanolis Feb 26 '15 at 16:40
  • Thanks for your answers. I don't know if i am gettting right, the difference between both examples is WHERE i am creating objects , right ? because i am always deciding which ICurrency i am using, wheter using beans or instantiating inside CurrencyOperator method. I am soooo confused! :P – jscherman Feb 27 '15 at 01:50

2 Answers2

2

Your example without Spring doesn't dependency injection! With dependency injection, the actual implementation of the interface is determined outside the code itself in order to reduce the coupling!

You should be able to need another implementation (you could for example switch from one JMS client to another...).

To answer to your last question, using Spring is (a very little bit) less performant but much more flexible.

EDIT : Spring is not the only tool that can be used for DI but it is the most popular and it contains a lot of features. Note that many Java standards also (such as JPA) use DI.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
  • Doesn't java include a lot of the stuff Spring used to be used for. Is it still worth learning Spring or should you stick to vanilla java? –  Feb 26 '15 at 17:01
  • @Poldie You are right. Many Spring aspects were integrated in Java standards so you might not need Spring. Why include another dependency when you can do everything with standard libraries. Nevertheless Spring contains so many features that it can be worth using it. It depends. – C.Champagne Feb 26 '15 at 17:30
2

Suppose you have 2 DAO classes one for Oracle, the seconde for MySQL, and both classes are implementing a DAO interface. You define an implementation as a bean in Spring configuration file. In the business class you have an attribut of type DAO, while in the spring configuration file you choose the real type wheather Oracle or MySQL to inject or using spring annotation @Autowired

This reduce coupling and it will be easy to move from Oracle to MySQL.

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

    //Business methods that invoks Dao methods 
}

In the Spring configuration file (XML file) you use the following:

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

By just changing the class attribut of your bean you change the whole implementation, without any change in your business class !
Good luck.

user3728064
  • 158
  • 1
  • 11
  • Thanks for your answer. But don't i am deciding which impl to injecting by changing the qualifier annotation? What change between doing what you did or injecting manually a new instance of the object that i want by new operator? furthermore, like your way i would be creating beans that i will not use? in this case, i am only using mySQLDao or oracleDao, but never both, i am right ? – jscherman Feb 27 '15 at 01:58
  • 1
    Yes you're right it wasn't an optimized solution. I edited the answer to fit exactly your needs. – user3728064 Feb 27 '15 at 09:34