简体   繁体   English

关于Spring Application Context的Java配置(依赖注入)的一些疑问

[英]Some doubts about Java Configuration of the Spring Application Context (dependency injection)

I am studying for Spring Core certification. 我正在学习Spring Core认证。

I know that in Spring I can configure the dependency injection using 3 way: 我知道在Spring中,我可以使用3种方式配置依赖项注入

  1. Java Configuration Java配置
  2. Classes annotations 类注释
  3. XML Configuration XML配置

I have some doubt related to how to use the first kind of dependency injection configuration. 我对如何使用第一种依赖项注入配置有疑问。

For example I have something like this: 例如,我有这样的事情:

1) A class named TransferServiceImpl : 1)名为TransferServiceImpl的类:

public class TransferServiceImpl implements TransferService {

    public TransferServiceImpl(AccountRepository ar) {
        this.accountRepository = ar;
    }
    ...
    ...
    ...
}

This class contain the TransferServiceImpl() constructor that take an AccountRepository object as input paramether, so AccountRepository is a dependency that have to be injected into TransferServiceImpl() . 此类包含TransferServiceImpl()构造函数,该构造函数将AccountRepository对象作为输入参数,因此AccountRepository是必须注入到TransferServiceImpl()中的依赖项。

2) This is the implementation of the previous AccountRepository class: 2)这是以前的AccountRepository类的实现:

public class JdbcAccountRepository implements AccountRepository {
    public JdbcAccountRepository(DataSource ds) {
        this.dataSource = ds;
    }
    ...
    ...
    ...
}

So the constructor of this class thake a DataSource object that have to be injected into the JdbcAccountRepository class. 因此,此类的构造函数带有一个DataSource对象,该对象必须注入到JdbcAccountRepository类中。

Then I have a configurations class that contains the dependency injection configuration: 然后,我有一个包含依赖项注入配置的配置类

@Configuration
public class ApplicationConfig{

    @Bean public TransferService transferService(){
        return new TransferServiceImpl(accountRepository());
    }

    @Bean public AccountRepository accountRepository(){
        return JdbcAccountRepository(dataSoruce());
    }

    @Bean public DataSource dataSource(){
        DataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        ...................................
        ...................................
        ...................................
    }
}

So it seems to me that it work in the following way: 因此在我看来,它的工作方式如下:

I have my 2 implemented beans named TransferServiceImpl and JdbcAccountRepository and the configuration class named ApplicationConfig . 我有两个实现的bean,它们分别名为TransferServiceImplJdbcAccountRepository和名为ApplicationConfig配置类

Into the configuration class I say that when someone ask to the factory to create a TransferService object it automatically build its implementation TransferServiceImpl creating and injecting automatically a JdbcAccountRepository into the TransferServiceImpl constructor. 在配置类中,我说的是,当有人要求工厂创建TransferService对象时,它会自动构建其实现TransferServiceImpl,从而创建并自动将JdbcAccountRepository注入到TransferServiceImpl构造函数中。

In the same way when a JdbcAccountRepository is created it is injected a DataSource object into its constructor. 以相同的方式在创建JdbcAccountRepository时将其注入DataSource对象到其构造函数中。

Is it right? 这样对吗?

If it is right I have the following doubts: 如果是正确的话,我有以下疑问:

1) Into the ApplicationConfig class I also declare the DataSource bean but I don't implement this class. 1)在ApplicationConfig类中,我还声明了DataSource bean,但没有实现该类。 Is it a class provided by Spring and I have only to set its properties values? 它是Spring提供的类,我只需要设置其属性值?

2) When are created the bean definied into the ApplicationConfig class? 2)什么时候定义了ApplicationConfig类的bean被创建? At the application startup? 在应用程序启动时? I think that, in the previous example, if I annotate a constructor using @Bean it is created as singleton at the application startup. 我认为,在前面的示例中,如果我使用@Bean注释构造函数 ,则会在应用程序启动时将其创建为单例。 Is it right or am I missing something? 是对的还是我错过了什么?

Tnx TNX

1) Into the ApplicationConfig class I also declare the DataSource bean but I don't implement this class. 1)在ApplicationConfig类中,我还声明了DataSource bean,但没有实现该类。 Is it a class provided by Spring and I have only to set its properties values? 它是Spring提供的类,我只需要设置其属性值?

Yes. 是。 There are a number of DataSource implementations provided by Spring. Spring提供了许多DataSource实现。 For example: DriverManagerDataSource , SingleConnectionDataSource , and more. 例如: DriverManagerDataSourceSingleConnectionDataSource等。

2) When are created the bean definied into the ApplicationConfig class? 2)什么时候定义了ApplicationConfig类的bean被创建? At the application startup? 在应用程序启动时? I think that, in the previous example, if I annotate a constructor using @Bean it is created as singleton at the application startup. 我认为,在前面的示例中,如果我使用@Bean注释构造函数,则会在应用程序启动时将其创建为单例。 Is it right or am I missing something? 是对的还是我错过了什么?

By default beans are created when Spring container is instantiated (usually at startup if the app is wired as such). 默认情况下,实例化Spring容器时会创建Bean(通常是在启动时,如果应用程序是这样连接的)。 You can change this behavior, however, by using the @Lazy annotation where the bean will only be created when explicitly requested. 但是,可以使用@Lazy批注来更改此行为,其中仅在显式请求时才创建Bean。

@Bean @Lazy public TransferService transferService(){
    return new TransferServiceImpl(accountRepository());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM