Spring default posting with profile

I have two beans. Both implement the distribution function. One only works when deployed to an application server. Another is used for testing.

We have a profile for each developer and environment. I want to conduct bean testing only with actual testing. Another bean should be used when not tested. How can I archive this?

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

Approaches to the solution:

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

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

-> Exception for bean for found wiring.

Exit profile:

@Component
public class OnlineMail implements Mailing {}

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

Adding all profiles:

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

, , "dev <WindowsLogin> "; , bean, beans, .

- @Profile ( "! localtest" ) .

- , " , bean"?

+5
3

- .

- .

@Component
public class OnlineMail implements Mailing {}

@Primary, OnlineMail .

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

:

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

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

@ActiveProfiles ( "localtest" ) , "production" DEFAULT.

, Spring ActiveProfilesResolver SPR-10338 - ( "dev1", "dev2" ..).

+2

Spring Bean @Profile :

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!";
    }
}

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);

    }
}

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());
        }
    }

-: https://github.com/m2land/InjectByProfile

0

All Articles