简体   繁体   中英

Annotation @Profile() doesn't work in Spring (not Boot) project

Annotation @Profile doesn't work or works wrongly. I want to get 7 beans: first-sixth and firstConfig. In package app.a I created three classes.

package app.a;
import lombok.*;
@Data
public class First {
    private Second second;
    private Third third;
}

package app.a;
import lombok.*;
import org.springframework.stereotype.*;
@Data
@Component
public class Second {
}

package app.a;
import lombok.*;
import org.springframework.stereotype.*;
@Data
@Component
public class Third {
}

In package app.firstConfig I created config class "FirstConfig", annotated with @Configuration.

package app.firstConfig;

import app.a.*;
import app.b.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.*;
import org.springframework.context.annotation.*;

@Configuration
@Profile("one")
@ComponentScan(basePackages = "app.*")
public class FirstConfig {

    @Bean
    public First first(Second second, Third third) {
        First first = new First();
        first.setSecond(second);
        first.setThird(third);
        return first;
    }
}

In package app.c I created two classes.

package app.c;

public class Eight {
}

package app.c;

import lombok.*;

@Data
public class Seventh {
    private Eight eight;
}

And in package app.secondConfig I created configure class with annotation @Configuration.

package app.secondConfig;

import app.c.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.*;
import org.springframework.context.annotation.*;

@Configuration
@Profile("two")
public class SecondConfig {

    @Autowired
    private ApplicationContext applicationContext;
    
    @Bean
    public Seventh seventh(){
        Seventh seventh = new Seventh();
        seventh.setEight(applicationContext.getBean(Eight.class));
        return seventh;
    }
}

My main class:

package app;

import app.firstConfig.*;
import org.springframework.context.*;
import org.springframework.context.annotation.*;

import java.util.*;

public class Main {

    static ApplicationContext applicationContext = new AnnotationConfigApplicationContext(FirstConfig.class);

    public static void main(String[] args) {      Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);
    }
}

In EditConfigurations of IntelijIdea I wrote "-Dspring.profiles.active=one". But in result I got these beans:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory

Process finished with exit code 0

But if I delete annotation @Profile("one") in FirstConfig I get right result.

package app.firstConfig;

import app.a.*;
import app.b.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.*;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan(basePackages = "app.*")
public class FirstConfig {

    @Bean
    public First first(Second second, Third third) {
        First first = new First();
        first.setSecond(second);
        first.setThird(third);
        return first;
    }
}

Result:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
firstConfig
second
third
fifth
sixth
first
fourth

What I forgot to do or configurate??? How to use annotation @Profile() rightly???

Seems like setting active profile does not work properly. You can add it to your propeties file as well and if you look at the top of the spring log output it usually tells you which profile has been activated (within the first 10 lines). If the default profile is loaded and you've added the line into config, try setting it as an environment variable. If it works its likely the properties file you've specified is not being picked up. Try it and update the question with any log output your seeing in relation to the active profile loaded.

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