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

211 views Asked by At

I am studying for Spring Core certification.

I know that in Spring I can configure the dependency injection using 3 way:

  1. Java Configuration
  2. Classes annotations
  3. XML Configuration

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:

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().

2) This is the implementation of the previous AccountRepository class:

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.

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.

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.

In the same way when a JdbcAccountRepository is created it is injected a DataSource object into its constructor.

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. Is it a class provided by Spring and I have only to set its properties values?

2) When are created the bean definied into the ApplicationConfig class? 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. Is it right or am I missing something?

Tnx

1

There are 1 answers

0
Khalid On BEST ANSWER

1) Into the ApplicationConfig class I also declare the DataSource bean but I don't implement this class. Is it a class provided by Spring and I have only to set its properties values?

Yes. There are a number of DataSource implementations provided by Spring. For example: DriverManagerDataSource, SingleConnectionDataSource, and more.

2) When are created the bean definied into the ApplicationConfig class? 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. 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). You can change this behavior, however, by using the @Lazy annotation where the bean will only be created when explicitly requested.

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