spring注解 @primary

@Primary:意思是在众多相同的bean中,优先使用用@Primary注解的bean。

1.在多数据源的时候,使用@Primary注解用于指定其中一个作为主数据源,即如果数据库操作没有指明使用哪个数据源的时候,默认使用主数据源,这个时候我们就使用到了@primary这个注解。

在此之前先认识一下@Qualifier、@Bean注解的意思:

@Qualifier:指定某个bean有没有资格进行注入。

@Bean:用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。

在项目中的使用:

@Configuration
@MapperScan(basePackages = "com.dunoinfo.jsqxt.estimate.algorithm.repository.oracle", sqlSessionTemplateRef = "oracleSemiSqlSessionTemplate")
public class DruidConfig {
    private Logger logger = LoggerFactory.getLogger(DruidConfig.class);

    @Bean(name = "oracleSemiDataSource")     //声明其为Bean实例
    @ConfigurationProperties("spring.datasource.druid")
    @Primary  //在同样的DataSource中,首先使用被标注的DataSource
    public DataSource dataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "oracleSemiSqlSessionFactory")
    @Primary
    public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleSemiDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/oracle/semi/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "oracleSemiSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSemiSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

2.当对同一个接口可能会有几种不同的实现类时,默认情况下只会采用其中的一种情况,使用@Primary注解来进行声明优先选择哪一个。

如下面的一个小例子:

先定义一个Person接口,有一个Study方法:

public interface Person {
    public void study(String subject);
}

再给此接口定义两个子类,其中一个加@Component注解进行声明,将其实例化到spring容器中;另一个子类不加注解:

@Component
public class StudentLi implements Person{
    @Override
    public void study(String subject) {
        System.out.println("I'm Li Lei. I'm learning "+subject);
    }
}
public class StudentHan implements Person{
    @Override
    public void study(String subject) {
        System.out.println("I'm Han Meimei. I'm learning "+subject);
    }
}

注入接口实现类进行测试:

@Controller
@RequestMapping(value = "/primary")
public class TestPrimary {
    @Autowired
    private Person person;

    @RequestMapping(value = "test", method = RequestMethod.GET)
    @ResponseBody
    public void Test(){
        person.study("Chinese");
    }
}

输出结果:I'm Li Lei. I'm learning Chinese

这个结果毫无疑问,因为StudentHan.java这个实现类没有加@Component注解,无法被加入到spring容器,下面对该实现类添加@Component注解,再进行测试。运行时程序出现异常,由于我们有两个实现类,spring无法确定注入哪一个。

 

在StudentHan.java实现类上添加@Primary注解进行声明,再进行测试。

@Primary
@Component
public class StudentHan implements Person{
    @Override
    public void study(String subject) {
        System.out.println("I'm Han Meimei. I'm learning "+subject);
    }
}

输出结果:I'm Han Meimei. I'm learning Chinese

 

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值