spring复习:(61)自定义CustomAutowireConfigurer

一、自定义注解:

package cn.edu.tju.anno;

import java.lang.annotation.*;

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyQualifier {
    String value() default "";
}


二、定义bean,使用自定义注解;

package cn.edu.tju.anno;

import cn.edu.tju.domain.MovieCatalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MovieRecommender2 {
    @Autowired
    @MyQualifier("thrill")
    public MovieCatalog movieCatalog;
    // ...
}

三、配置文件中配置CustomAutowireConfigurer



    
    
        
    

    
        
            
                cn.edu.tju.anno.MyQualifier
            
        
    


四、主类

package cn.edu.tju;

import cn.edu.tju.anno.MovieRecommender;
import cn.edu.tju.anno.MovieRecommender2;
import cn.edu.tju.study.service.anno.domain.MyValueCalculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Start19 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("beans19.xml");
        MovieRecommender2 bean = applicationContext.getBean(MovieRecommender2.class);
        System.out.println(bean.movieCatalog);


    }
}


你可能感兴趣的:(Spring,spring,java,后端)