春季豆默认优先级

huangapple 未分类评论53阅读模式
英文:

spring bean default Priority

问题

以下是您提供的代码的翻译部分:

  1. import javax.annotation.Priority;
  2. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.Ordered;
  6. import org.springframework.stereotype.Component;
  7. public class SpringPriorityMain {
  8. public static void main(String[] args) {
  9. try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class)) {
  10. Bean bean = ctx.getBean(Bean.class);
  11. System.out.println();
  12. System.out.println(bean.getName());
  13. System.out.println();
  14. }
  15. }
  16. public static interface Bean {
  17. String getName();
  18. }
  19. @Component
  20. // @Priority(0)
  21. public static class BeanImplBusiness implements Bean {
  22. @Override
  23. public String getName() {
  24. return "Business";
  25. }
  26. }
  27. @Component
  28. @Priority(Ordered.LOWEST_PRECEDENCE)
  29. public static class BeanImplTest implements Bean {
  30. @Override
  31. public String getName() {
  32. return "Test";
  33. }
  34. }
  35. @Configuration
  36. @ComponentScan("ro.catta.spring")
  37. public static class SpringConfig {
  38. }
  39. }

Maven项目的POM文件内容如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>ro.catta.poc</groupId>
  6. <artifactId>spring-priority</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <properties>
  9. <maven.compiler.source>1.8</maven.compiler.source>
  10. <maven.compiler.target>1.8</maven.compiler.target>
  11. <mvn.spring.version>4.3.20.RELEASE</mvn.spring.version>
  12. <!-- <mvn.spring.version>5.2.5.RELEASE</mvn.spring.version> -->
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-core</artifactId>
  18. <version>${mvn.spring.version}</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-context</artifactId>
  23. <version>${mvn.spring.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-beans</artifactId>
  28. <version>${mvn.spring.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>javax</groupId>
  32. <artifactId>javaee-api</artifactId>
  33. <version>7.0</version>
  34. <scope>provided</scope>
  35. </dependency>
  36. </dependencies>
  37. </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?

  1. import javax.annotation.Priority;
  2. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.Ordered;
  6. import org.springframework.stereotype.Component;
  7. public class SpringPriorityMain {
  8. public static void main(String[] args) {
  9. try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class)) {
  10. Bean bean = ctx.getBean(Bean.class);
  11. System.out.println();
  12. System.out.println(bean.getName());
  13. System.out.println();
  14. }
  15. }
  16. public static interface Bean {
  17. String getName();
  18. }
  19. @Component
  20. // @Priority(0)
  21. public static class BeanImplBusiness implements Bean {
  22. @Override
  23. public String getName() {
  24. return &quot;Business&quot;;
  25. }
  26. }
  27. @Component
  28. @Priority(Ordered.LOWEST_PRECEDENCE)
  29. public static class BeanImplTest implements Bean {
  30. @Override
  31. public String getName() {
  32. return &quot;Test&quot;;
  33. }
  34. }
  35. @Configuration
  36. @ComponentScan(&quot;ro.catta.spring&quot;)
  37. public static class SpringConfig {
  38. }
  39. }

The sample project is a maven project, here is the pom file to see exactly the dependencies

  1. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  4. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  5. &lt;groupId&gt;ro.catta.poc&lt;/groupId&gt;
  6. &lt;artifactId&gt;spring-priority&lt;/artifactId&gt;
  7. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  8. &lt;properties&gt;
  9. &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
  10. &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
  11. &lt;mvn.spring.version&gt;4.3.20.RELEASE&lt;/mvn.spring.version&gt;
  12. &lt;!-- &lt;mvn.spring.version&gt;5.2.5.RELEASE&lt;/mvn.spring.version&gt; --&gt;
  13. &lt;/properties&gt;
  14. &lt;dependencies&gt;
  15. &lt;dependency&gt;
  16. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  17. &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
  18. &lt;version&gt;${mvn.spring.version}&lt;/version&gt;
  19. &lt;/dependency&gt;
  20. &lt;dependency&gt;
  21. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  22. &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
  23. &lt;version&gt;${mvn.spring.version}&lt;/version&gt;
  24. &lt;/dependency&gt;
  25. &lt;dependency&gt;
  26. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  27. &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
  28. &lt;version&gt;${mvn.spring.version}&lt;/version&gt;
  29. &lt;/dependency&gt;
  30. &lt;dependency&gt;
  31. &lt;groupId&gt;javax&lt;/groupId&gt;
  32. &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
  33. &lt;version&gt;7.0&lt;/version&gt;
  34. &lt;scope&gt;provided&lt;/scope&gt;
  35. &lt;/dependency&gt;
  36. &lt;/dependencies&gt;
  37. &lt;/project&gt;

huangapple
  • 本文由 发表于 2020年4月5日 16:25:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61039887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定