12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package com.sf.ioc.anno;
- import jakarta.annotation.Resource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- /**
- * 教室
- * 构造类和类之间的组合关系 区别于继承关系
- */
- @Component
- // <bean id="house">
- public class House {
- /**
- * 教室里有一把椅子
- */
- @Autowired
- private Chair chair;
- // 使用@Autowired 是先根据类型来匹配对象
- // 如果类型对应的对象有多个 可以结合 @Qualifier来声明名字
- // 使用@Resource 是先根据名字来匹配对象
- // Could not autowire. There is more than one bean of 'Plane' type.
- // @Autowired @Qualifier("desk")
- // @Autowired @Qualifier("box")
- // @Resource(name = "desk")
- // private Plane plane;
- @Resource
- private Plane desk;
- public House() {
- System.out.println("House 构造器");
- }
- public void setChair(Chair chair) {
- System.out.println("House setChair");
- this.chair = chair;
- }
- @Override
- public String toString() {
- return "House{" +
- "chair=" + chair +
- ", plane=" + desk +
- '}';
- }
- }
|