Spring复习:(59)@Qualifier注解

@Qualifier注解用来指定自动注入时合作bean需要满足的qualifier的名称,如果一个bean没有指定qualifier属性,则用它的id为qualifier,来给别的bean提供注入。
示例

package cn.edu.tju.domain;

public class MovieCatalog {
}

package cn.edu.tju.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class MovieRecommender {
    @Autowired
    @Qualifier("secondary")
    private MovieCatalog movieCatalog;

}




    
    
        
    
    

    





package cn.edu.tju.test;

import cn.edu.tju.domain.People;
import cn.edu.tju.domain.Person;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class Test09 {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new
                ClassPathXmlApplicationContext("spring9.xml");

    }
}

##########################################################

package cn.edu.tju.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class MovieRecommender2 {

    private  MovieCatalog movieCatalog;

    public MovieCatalog getMovieCatalog() {
        return movieCatalog;
    }

    public void setMovieCatalog(MovieCatalog movieCatalog) {
        this.movieCatalog = movieCatalog;
    }

    @Autowired
    public void prepare(@Qualifier("secondary") MovieCatalog movieCatalog) {
        this.movieCatalog = movieCatalog;
    }

}

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