How does Spring filter out objects that do not need to be injected

        This problem is often encountered in Spring. It is often necessary to inject third-party configuration information into the service, such as: search engine, message queue, etc.... But since the service is used as the bridge layer in the middle of each C side, it needs to be in the middle of the C-side. No amount of C-side is configured with corresponding configuration files or entity declarations. It may be that in these C-sides, the relevant functions are not used at all!... How to remove unnecessary dependencies gracefully?

        I summed up two methods, and I forgot to point out the shortcomings:

            1: Make the third-party dependencies a separate service-jar package, which can be referenced in the required projects, but the premise, the dependencies of the project structure itself, have a better plan for decoupling

            2: Filter the injected objects on the C side that does not need to use this function.

Here is a brief description of the second point:

            Due to the injection of objects in spring, we generally use the ioc method, and use the annotation @Autowired to annotate the objects that need to be injected, but in some C terminals, there may be no need to change the object at all, so there is naturally no injection of the object. At this time, when you start the project, you will find that an error is reported, and the injection object cannot be found. The error information reported by spring is quite accurate. Yes, we are limited to ignore the object, check @Autowired, the source code is as follows:

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

	/**
	 * Declares whether the annotated dependency is required.
	 * <p>Defaults to {@code true}.
	 */
	boolean required() default true;

}

required, is it necessary. We set it to false, that is, as follows:

    @Autowired(required=false)  
    private MQConfig config; //Refer to the unified parameter configuration class

This is to directly ignore the injected object. In another case, do we need to ignore the object injection in the entire implementation class? Or do we ignore the declaration of the entire class?

spring-context provides a variety of support

Excerpted from presentations on other sites:

The context:component-scan node is allowed to have two child nodes <context:include-filter> and <context:exclude-filter>. The type and expression of the filter tag are described as follows:

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation A target class that conforms to SomeAnnoation
assignable org.example.SomeClass Specify the full name of the class or interface
aspectj org.example..*Service+ AspectJ syntax
regex org\.example\.Default.* Controller Expression
custom org.example.MyTypeFilter Spring3 adds a custom Type, implements org.springframework.core.type.TypeFilter

In our example, the filter type is set to a regular expression, regex, note that in the regular. . represents all characters, and \. represents the real . character. Our regex represents classes that end in Dao or Service.

A simple configuration example spring-context.xml in my project is as follows:

<context:component-scan base-package="com.xyy">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    	<context:exclude-filter type="aspectj" expression="com.xyy.driver..*"/>
        <context:exclude-filter type="aspectj" expression="com.xyy.erp..*"/>
    	<context:exclude-filter type="assignable" expression="com.xyy.common.config.ESMQConfig"/>
        <context:exclude-filter type="assignable" expression="com.xyy.common.config.MQConfig"/>
   </context:component-scan>

Because the above is not necessary to inject the object

MQConfig also includes the injection of other properties, so the declaration of this object is ignored directly in spring's package scan...


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325192883&siteId=291194637