Java开发两年,连Spring的依赖注入的方式都搞不清楚,你工作可能有点悬

Java开发两年,连Spring的依赖注入的方式都搞不清楚,你工作可能有点悬,第1张

Java开发两年,连Spring的依赖注入的方式都搞不清楚,你工作可能有点悬

public int addMoney(int money) {
System.out.println(“向账户中加钱:” + money);
return 0;
}

@Override
public void saveAccount(Account account) {
System.out.println(“saveAccount方法执行了”);
}
}

测试


@Test
public void test8() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“classpath:bean.xml”);;

AccountService accountService = (AccountService) applicationContext.getBean(“accountService”);

System.out.println(accountService.toString());
}

优点:在获取bean对象时,注入数据是必须的 *** 作,否则对象无法创建成功。 缺点:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据也必须提供。

setter方法注入

在bean标签内部使用property标签进行配置。 property标签的属性:

  • name:用于指定注入时所调用的set方法名称
  • value:用于提供基本类型和String类型的数据
  • ref:用于指定其他的bean类型数据

这里面我们注入了基本类型、包装类型、日期类型数据。 AccountServiceImpl 类

public class AccountServiceImpl implements AccountService {

private String name;
private Integer age;
private Date birthday;

public String getName() {
return name;
}

public void setName(String name) {
System.out.println(“给name设置值”);
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
System.out.println(“给age设置值”);
this.age = age;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
System.out.println(“给birthday设置值”);
this.birthday = birthday;
}

@Override
public String toString() {
return “AccountServiceImpl{” +
“name=’” + name + ‘’’ +
“, age=” + age +
“, birthday=” + birthday +
‘}’;
}

public AccountServiceImpl(String name, Integer age, Date birthday) {
System.out.println(“含参的构造方法被调用了”);
this.name = name;
this.age = age;
this.birthday = birthday;
}

public AccountServiceImpl() {
System.out.println(“构造方法调用”);
}

@Override
public int addMoney(int money) {
System.out.println(“向账户中加钱:” + money);
return 0;
}

@Override
public void saveAccount(Account account) {
System.out.println(“saveAccount方法执行了”);
}
}

bean.xml

测试


@Test
public void test9() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“classpath:bean.xml”);;

AccountService accountService = (AccountService) applicationContext.getBean(“accountService”);

System.out.println(accountService.toString());
}

运行测试以后,可以在控制台看到以下内容:

优势:创建对象时没有明确的限制,可以直接使用默认构造函数。 缺点:如果又某个成员必须有值,则获取对象有可能是set方法没有执行。

对集合类型数据进行注入

AccountService2Impl 类

public class AccountService2Impl implements AccountService2 {

private String[] myStrs;

private List myList;

private Set mySet;

private Map myMap;

private Properties myProps;

public String[] getMyStrs() {
return myStrs;
}

public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}

public List getMyList() {
return myList;
}

public void setMyList(List myList) {
this.myList = myList;
}

public Set getMySet() {
return mySet;
}

public void setMySet(Set mySet) {
this.mySet = mySet;
}

public Map getMyMap() {
return myMap;
}

public void setMyMap(Map myMap) {
this.myMap = myMap;
}

public Properties getMyProps() {
return myProps;
}

public void setMyProps(Properties myProps) {
this.myProps = myProps;
}

@Override
public String toString() {
return “AccountService2Impl{” +
“myStrs=” + Arrays.toString(myStrs) +
“, myList=” + myList +
“, mySet=” + mySet +
“, myMap=” + myMap +
“, myProps=” + myProps +
‘}’;
}

}

bean.xml

AAA BBB CCC list1 list2 list3 set1 set2 set3 柯森 23 value3

测试


@Test
public void test10() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“classpath:bean.xml”);

AccountService2 accountService2 = (AccountService2) applicationContext.getBean(“accountService2”);

System.out.println(accountService2.toString());
}

运行测试以后,可以看到在控制台打印输出了以下内容

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

这说明我们注入集合类型数据成功了。

注解注入

用于注入数据的注解

bean.xml文件

AccountService4Impl 类

@Component
public class AccountService4Impl implements AccountService3 {

@Autowired
private AccountDao accountDao;

@Override
public void addMoney(int money) {
System.out.println(“向账户中加钱…AccountService3Impl”);
}
}

假设此时只有一个AccountDao的实现类,并且这个类也加上了@Repository注解,那么我们这样注入是可以成功的,但是如果容器中存在多个AccountDao的实现类,此时仅仅使用AccountDao是不能完成数据注入的,需要配合@Qualifier注解使用注入数据。

假设现有如下两个实现类,那我们应该怎么写才能成功注入数据?

@Component
public class AccountService4Impl implements AccountService3 {

//错误写法,默认会去容器中查找名称为accountDao的bean
//@Autowired
//private AccountDao accountDao;

//正确写法
//@Autowired
//private AccountDao accountDao1

//正确写法
//@Autowired
//private AccountDao accountDao1;

//正确写法
@Autowired
@Qualifier(“accountDao1”)
private AccountDao accountDao;

@Override
public void addMoney(int money) {
System.out.println(“向账户中加钱…AccountService3Impl”);
}

}

错误写法,默认会去容器中查找名称为accountDao的bean
//@Autowired
//private AccountDao accountDao;

//正确写法
//@Autowired
//private AccountDao accountDao1

//正确写法
//@Autowired
//private AccountDao accountDao1;

//正确写法
@Autowired
@Qualifier(“accountDao1”)
private AccountDao accountDao;

@Override
public void addMoney(int money) {
System.out.println(“向账户中加钱…AccountService3Impl”);
}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5574802.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存