简体   繁体   English

注释 @Profile() 在 Spring(非 Boot)项目中不起作用

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

Annotation @Profile doesn't work or works wrongly.注释 @Profile 不起作用或工作错误。 I want to get 7 beans: first-sixth and firstConfig.我想得到 7 个 bean:first-sixth 和 firstConfig。 In package app.a I created three classes.在包 app.a 中,我创建了三个类。

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.在包 app.firstConfig 中,我创建了配置类“FirstConfig”,并使用 @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.在包 app.c 中,我创建了两个类。

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.在包 app.secondConfig 中,我创建了带有注释 @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".在 IntelijIdea 的 EditConfigurations 中,我写了“-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.但是,如果我在 FirstConfig 中删除注释 @Profile("one") 我会得到正确的结果。

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???如何正确使用注解@Profile()???

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).您也可以将其添加到您的属性文件中,如果您查看 spring 日志输出的顶部,它通常会告诉您哪个配置文件已被激活(在前 10 行内)。 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.尝试并使用您看到的与加载的活动配置文件相关的任何日志输出来更新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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