user1461001
user1461001

Reputation: 703

Spring configuration

Let's say we have a bean definition in spring configuration

<bean id="scanningIMAPClient" class="com.acme.email.incoming.ScanningIMAPClient" />

What I really want is the scanningIMAPClient to be of type com.acme.email.incoming.GenericIMAPClient if the configured email server is a normal IMAP server and com.acme.email.incoming.GmailIMAPClient incase it is a GMAIL server, (since gmail behaves in slightly different way) GmailIMAPClient is a subclass of GenericIMAPClient.

How can I accomplish that in spring configuration?

There is a properties file which contains configuration of the email server.

Upvotes: 3

Views: 134

Answers (2)

ebaxt
ebaxt

Reputation: 8417

You can use programatic configuration:

@Configuration
public class AppConfig {

    @Bean(name="scanningIMAPClient")
    public GenericIMAPClient helloWorld() {
        ...check config and return desired type
    }

}

More info here.

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

It's simple with Java configuration:

@Value("${serverAddress}")
private String serverAddress;

@Bean
public GenericIMAPClient scanningIMAPClient() {
    if(serverAddress.equals("gmail.com"))
        return new GmailIMAPClient();
    else
        return new GenericIMAPClient();
}

You can emulate this behaviour with custom FactoryBean.

Upvotes: 1

Related Questions