作者简介:zhz小白
公众号:小白的Java进阶之路
专业技能:
1、Java基础,并精通多线程的开发,熟悉JVM原理
2、熟悉Java基础,并精通多线程的开发,熟悉JVM原理,具备⼀定的线上调优经验
3、熟悉MySQL数据库调优,索引原理等,⽇志原理等,并且有出过⼀篇专栏
4、了解计算机⽹络,对TCP协议,滑动窗⼝原理等有⼀定了解
5、熟悉Spring,Spring MVC,Mybatis,阅读过部分Spring源码
6、熟悉SpringCloud Alibaba体系,阅读过Nacos,Sentinel,Seata,Dubbo,Feign,Gateway核⼼源码与设计,⼆次开发能⼒
7、熟悉消息队列(Kafka,RocketMQ)的原理与设计
8、熟悉分库分表ShardingSphere,具有真实⽣产的数据迁移经验
9、熟悉分布式缓存中间件Redis,对其的核⼼数据结构,部署架构,⾼并发问题解决⽅案有⼀定的积累
10、熟悉常⽤设计模式,并运⽤于实践⼯作中
11、了解ElasticSearch,对其核⼼的原理有⼀定的了解
12、了解K8s,Jekins,GitLab
13、了解VUE,GO
14、⽬前有正在利⽤闲暇时间做互游游戏,开发、运维、运营、推销等
本人著作git项目:https://gitee.com/zhouzhz/star-jersey-platform,有兴趣的可以私聊博主一起编写,或者给颗star
领域:对支付(FMS,FUND,PAY),订单(OMS),出行行业等有相关的开发领域
如果此文还不错的话,还请关注、点赞、收藏三连支持一下博主~
BookDao.java
package com.zhz.dao;
import org.springframework.stereotype.Repository;
/**
* @author zhouhengzhe
* @description: todo
* @date 2022/11/4 10:56
* @since v1
*/
@Repository
public class BookDao {
private String label;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
@Override
public String toString() {
return "BookDao{" +
"label='" + label + '\'' +
'}';
}
}
BookService.java
package com.zhz.service;
import com.zhz.dao.BookDao;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author zhouhengzhe
* @description: todo
* @date 2022/11/4 10:56
* @since v1
*/
@Service
public class BookService {
@Resource(name="bookDao1")
private BookDao bookDao;
@Resource(name="bookDao2")
private BookDao bookDao2;
@Override
public String toString() {
return "BookService{" +
"bookDao=" + bookDao +
"bookDao2=" + bookDao2 +
'}';
}
}
MainConfig.java
package com.zhz.config;
import com.zhz.bean.Person;
import com.zhz.condition.AppleCondition;
import com.zhz.condition.BananaCondition;
import com.zhz.condition.WatermelonCondition;
import com.zhz.dao.BookDao;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
/**
* @author zhouhengzhe
* @description: todo
* @date 2022/11/4 10:27
* @since v1
*/
@Configuration
@ComponentScan("com.zhz.service")
public class MainConfig {
@Bean("bookDao1")
public BookDao bookDao1() {
BookDao bookDao = new BookDao();
bookDao.setLabel("1");
return bookDao;
}
@Primary
@Bean("bookDao2")
public BookDao bookDao2() {
BookDao bookDao = new BookDao();
bookDao.setLabel("2");
return bookDao;
}
}
test方法:
@Test
public void test2(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
BookService bookService = applicationContext.getBean(BookService.class);
System.out.println(bookService);
}
依赖pom.xml:
<dependency>
<groupId>javax.injectgroupId>
<artifactId>javax.injectartifactId>
<version>1version>
dependency>
只需要把上面的测试代码的dao引用改成
package com.zhz.service;
import com.zhz.dao.BookDao;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author zhouhengzhe
* @description: todo
* @date 2022/11/4 10:56
* @since v1
*/
@Service
public class BookService {
@Inject
private BookDao bookDao;
@Override
public String toString() {
return "BookService{" +
"bookDao=" + bookDao +
'}';
}
}
然后看其运行结果,发现其跟@Autowired注解一样的,都是默认优先装配使用了@Primary注解标注的组件。。
不同点
相同点