春季豆默认优先级

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

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 &quot;Business&quot;;
        }
    }

    @Component
    @Priority(Ordered.LOWEST_PRECEDENCE)
    public static class BeanImplTest implements Bean {

        @Override
        public String getName() {
            return &quot;Test&quot;;
        }
    }

    @Configuration
    @ComponentScan(&quot;ro.catta.spring&quot;)
    public static class SpringConfig {

    }

}

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

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

	&lt;groupId&gt;ro.catta.poc&lt;/groupId&gt;
	&lt;artifactId&gt;spring-priority&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;

	&lt;properties&gt;
        &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
		&lt;mvn.spring.version&gt;4.3.20.RELEASE&lt;/mvn.spring.version&gt;
		&lt;!-- &lt;mvn.spring.version&gt;5.2.5.RELEASE&lt;/mvn.spring.version&gt; --&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-core&lt;/artifactId&gt;
			&lt;version&gt;${mvn.spring.version}&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-context&lt;/artifactId&gt;
			&lt;version&gt;${mvn.spring.version}&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework&lt;/groupId&gt;
			&lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
			&lt;version&gt;${mvn.spring.version}&lt;/version&gt;
		&lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;javax&lt;/groupId&gt;
            &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
            &lt;version&gt;7.0&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;
	&lt;/dependencies&gt;
&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:

确定