Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot-错误的配置文件和错误的bean选择,尽管spring.profiles.active=local正确_Java_Spring Boot - Fatal编程技术网

Java SpringBoot-错误的配置文件和错误的bean选择,尽管spring.profiles.active=local正确

Java SpringBoot-错误的配置文件和错误的bean选择,尽管spring.profiles.active=local正确,java,spring-boot,Java,Spring Boot,关于Spring配置文件问题的小问题 目前,在我的application.properties中,我有此设置 spring.profiles.active=local 我也确信没有任何超控 在我的代码中,我有两个bean,我希望Spring根据当前配置文件正确选择 @Bean(name = SESSION) @Profile({LOCAL, INTEGRATION}) protected CqlSession cqlSession() { System.out

关于Spring配置文件问题的小问题

目前,在我的application.properties中,我有此设置

spring.profiles.active=local
我也确信没有任何超控

在我的代码中,我有两个bean,我希望Spring根据当前配置文件正确选择

 @Bean(name = SESSION)
    @Profile({LOCAL, INTEGRATION})
    protected CqlSession cqlSession() {
        System.out.println("I would like to be chosen when profiles are local or integration");
        return CqlSession.builder().addContactEndPoint(new DefaultEndPoint(new InetSocketAddress(contactPoints, port))).withKeyspace(keyspace).withLocalDatacenter(datacenter).withAuthCredentials(username, passPhrase).build();
    }

    @Bean(name = SESSION)
    protected CqlSession cqlSession(Environment env, SSLFactory sslFactory) {
        System.out.println("current profile: " + Arrays.toString(env.getActiveProfiles()));
        System.out.println("Profile seems to be local, but yet, it chose me");
        return CqlSession.builder().addContactEndPoint(new DefaultEndPoint(new InetSocketAddress(contactPoints, port))).withKeyspace(keyspace).withLocalDatacenter(datacenter).withAuthCredentials(username, passPhrase).withSslContext(sslFactory.getSslContext()).build();
    }
查看此代码片段,我希望当我的本地概要文件处于活动状态时,选择第一个bean(没有SSL的bean)

然而,在每一次运行中,都会打印出这些内容

current profile: [local]
Profile seems to be local, but yet, it chose me
这对我来说非常奇怪,因为运行时知道当前概要文件是本地的,但是,决定使用未使用概要文件本地注释的bean

我可以知道是什么问题,以及如何让它选择正确的bean,一个本地配置文件吗


谢谢你

这个
@Profile
注释

如果省略@Profile注释,则无论哪个(如果有的话)概要文件处于活动状态,都将进行注册。注意:对于@Bean方法上的@Profile,可能会应用一个特殊的场景:对于相同Java方法名称的重载@Bean方法(类似于构造函数重载),需要在所有重载方法上一致地声明@Profile条件。如果条件不一致,则只有重载方法中第一个声明上的条件才起作用@因此,概要文件不能用于选择具有特定参数签名的重载方法而不是另一个重载方法;同一bean的所有工厂方法之间的解析在创建时遵循Spring的构造函数解析算法。如果您想定义具有不同配置文件条件的替代bean,请使用指向相同bean名称的不同Java方法名称;请参阅@Configuration的javadoc中的ProfileDatabaseConfig


我认为这意味着您的第一个bean可以注册为事件,尽管它没有配置文件注释。所以春天将在创造的时候决定。我认为答案可能是用
@Profile(“!local”)
注释启用ssl的bean,并可能更改bean方法名。

的确,非常感谢