spring注解 @primary - Go语言中文社区

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

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_33304273/article/details/81222738
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 13:31:46
  • 阅读 ( 601 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢