使用或不使用Spring Beans有什么区别? [英] what is the difference between using or not Spring Beans?

查看:245
本文介绍了使用或不使用Spring Beans有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能我会得到很多downvotes,但是对于我来说,无论是否使用bean都是如此令人困惑。让我们假设这个例子

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...
    }

}

因此,为了注入ICurrency impl注入,我认为我可以通过两种方式实现:

So, to inject the ICurrency impl injection i think that i can do it by two ways:

方式1:没有Spring bean

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

}

其中USDollarCurrency是ICurrency接口实现

Where USDollarCurrency is an ICurrency interface implementation

方式2:使用Spring bean

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






我真的不知道了解使用Spring的bean有什么好处。我读了一些东西,但我最常发现的是关于使用DI的好处,据我所知,两种方式都注入了CurrencyProcessor所需的依赖关系,改变的是我创建和使用objets的方式,我错了吗?所以具体来说,我的问题是:
1.在这种情况下使用Beans有什么好处?
2.为什么我要使用Spring而不是像第一种方式手动操作?
3.谈论性能,哪种情况更好?


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?

推荐答案

假设您有2个DAO类Oracle,MySQL的seconde以及这两个类都在实现DAO接口。您可以在Spring配置文件中将实现定义为bean。在业务类中,您有一个类型为DAO的属性,而在spring配置文件中,您选择要注入或使用spring注释的Oracle或MySQL的实际类型 @Autowired

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

这减少了耦合,很容易从Oracle转移到MySQL。

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 
}

在Spring配置文件(XML文件)中,您使用以下命令:

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

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

只需更改bean的类属性即可更改整个实现,而无需更改业务上课!
祝你好运。

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

这篇关于使用或不使用Spring Beans有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆