英文:
spring bean default Priority
问题
以下是您提供的代码的翻译部分:
import javax.annotation.Priority;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
public class SpringPriorityMain {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class)) {
Bean bean = ctx.getBean(Bean.class);
System.out.println();
System.out.println(bean.getName());
System.out.println();
}
}
public static interface Bean {
String getName();
}
@Component
// @Priority(0)
public static class BeanImplBusiness implements Bean {
@Override
public String getName() {
return "Business";
}
}
@Component
@Priority(Ordered.LOWEST_PRECEDENCE)
public static class BeanImplTest implements Bean {
@Override
public String getName() {
return "Test";
}
}
@Configuration
@ComponentScan("ro.catta.spring")
public static class SpringConfig {
}
}
Maven项目的POM文件内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ro.catta.poc</groupId>
<artifactId>spring-priority</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mvn.spring.version>4.3.20.RELEASE</mvn.spring.version>
<!-- <mvn.spring.version>5.2.5.RELEASE</mvn.spring.version> -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${mvn.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${mvn.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${mvn.spring.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
英文:
In my simplified code sample I have 2 spring beans implementing the same interface (in real life the 2 components reside in different jars). One component (BeanImplBusiness in my sample) must be annotated with only @Component
. The second one (BeanImplTest) can have any annotations.
The desired behavior is when both beans are in the classpath, spring to inject the "Business" bean in all places.
I try to achieve this using the javax.annotation.Priority
annotation but it doesn't work the way I expect. Let me explain:
- put @Priority annotation only in "Test" component: spring will inject only the "Test" bean no matter what value I give to @Priority. It looks like the "Business" bean doesn't exists.
- put @Priority annotation in both beans and the lowest precedence in "Test" bean (
@Priority(Ordered.LOWEST_PRECEDENCE)
) it works perfectly, spring inject the "Business" component.
Spring version is 4.3.20, but I try also with 5.2.5 and I have same results.
So, my question is how can I configure the spring bean to give a default "Priority" on Business bean even if I don't declare it with annotation. Any solution with annotations, xml is good.
Why do I need to specify Priority annotations on both components? Is this a bug?
import javax.annotation.Priority;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
public class SpringPriorityMain {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class)) {
Bean bean = ctx.getBean(Bean.class);
System.out.println();
System.out.println(bean.getName());
System.out.println();
}
}
public static interface Bean {
String getName();
}
@Component
// @Priority(0)
public static class BeanImplBusiness implements Bean {
@Override
public String getName() {
return "Business";
}
}
@Component
@Priority(Ordered.LOWEST_PRECEDENCE)
public static class BeanImplTest implements Bean {
@Override
public String getName() {
return "Test";
}
}
@Configuration
@ComponentScan("ro.catta.spring")
public static class SpringConfig {
}
}
The sample project is a maven project, here is the pom file to see exactly the dependencies
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ro.catta.poc</groupId>
<artifactId>spring-priority</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mvn.spring.version>4.3.20.RELEASE</mvn.spring.version>
<!-- <mvn.spring.version>5.2.5.RELEASE</mvn.spring.version> -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${mvn.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${mvn.spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${mvn.spring.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
专注分享java语言的经验与见解,让所有开发者获益!
评论