Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring configuration

Tags:

java

spring

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.

like image 286
user1461001 Avatar asked Aug 12 '26 02:08

user1461001


1 Answers

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.

like image 167
Tomasz Nurkiewicz Avatar answered Aug 14 '26 16:08

Tomasz Nurkiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!