简体   繁体   中英

Spring default wiring with Profile

I've got two beans. Both implement the mailing function. One is only working when it is deployed to an application server. The other one is used for testing.

We have profile for each developer and environment. I want to wire the testing bean only when actually testing. The other bean should be used when not testing. How can I archive this?

@Component
@Profile("localtest")
public class OfflineMail implements Mailing {}

Solution approaches:

Using "default" I read this somewhere, but there seems to be no fall-back to "default" for a profile like "dev":

@Component
@Profile("default")
public class OnlineMail implements Mailing {}

-> Exception for no bean for wiring found.

Leaving the profile out:

@Component
public class OnlineMail implements Mailing {}

-> Throws a unique exception when running the "localtest" profile.

Adding all profiles:

@Component
@Profile("prod")
@Profile("integration")
@Profile("test")
@Profile("dev1")
@Profile("dev2")
@Profile("dev3")
...
public class OnlineMail implements Mailing {}

This is actually working, however our devs aren't numbered they use "dev<WindowsLogin>" and adding the profiles, may work for one bean, but one will get into trouble when using it for several beans as this definitely gets ugly.

Using something like @Profile("!localtest") doesn't seem to work as well.

Does anyone know a nicer way to get a "wire by default if no specific bean is found"?

I finally found an easy solution.

The online mail is just wired by default.

@Component
public class OnlineMail implements Mailing {}

Using the @Primary annotation the offline mail takes precedence over the OnlineMail and avoids the Unique exception.

@Component
@Profile("localtest")
@Primary
public class OfflineMail implements Mailing {}

Try this:

@Component
@Profile("production")
public class OnlineMail implements Mailing {}

@Component
@Profile("localtest")
public class OfflineMail implements Mailing {}

Then run tests using @ActiveProfiles("localtest") and run production enviroment using "production" as DEFAULT profile.

Also I hope in next version of Spring ActiveProfilesResolver will be introduced SPR-10338 - it may be helpfull for you (to avoid "dev1", "dev2" and so on).

Spring supports inject the Bean by @Profile very well:

interface Talkative {
    String talk();
}

@Component
@Profile("dev")
class Cat implements Talkative {
        public String talk() {
        return "Meow.";
    }
}

@Component
@Profile("prod")
class Dog implements Talkative {
    public String talk() {
        return "Woof!";
    }
}

Works in unit test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContex-test.xml"})
@ActiveProfiles(value = "dev")
public class InjectByDevProfileTest
{
    @Autowired
    Talkative talkative;

    @Test
    public void TestTalkative() {
        String result = talkative.talk();
        Assert.assertEquals("Meow.", result);

    }
}

Works in Main():

@Component public class Main {

        public static void main(String[] args) {
            // Enable a "dev" profile
            System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
            ApplicationContext context =
                    new ClassPathXmlApplicationContext("applicationContext.xml");
            Main p = context.getBean(Main.class);
            p.start(args);
        }

        @Autowired
        private Talkative talkative;

        private void start(String[] args) {
            System.out.println(talkative.talk());
        }
    }

Check this for the Demo code: https://github.com/m2land/InjectByProfile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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