一:基于xml的DI(Dependency Injection)
注入类型:
定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car
Student
public class Student { private String name; private String age; private Car car; //无参构造 public Student() { //System.out.println("Student.Student()"); } //带参构造 public Student(String name, String age, Car car) { this.name = name; this.age = age; this.car = car; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", car=" + car + "]"; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Car:
public class Car { private String color; private String brand; //无参构造 public Car() { } //带参构造 public Car(String color, String brand) { super(); this.color = color; this.brand = brand; } @Override public String toString() { return "Car [color=" + color + ", brand=" + brand + "]"; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
1.设值注入(set方法注入):本质上是调用了Bean的setXXX()进行值的注入。分为普通属性和域属性
实现效果:
2.构造注入
实现效果:
3.命名空间p注入
使用前要先要在Spring配置文件中引入p命名空间
实现效果:
二、集合属性注入[List、Set、Map]
public class MyCollection { private List<String> list; private Set<String> set; private Map<String,String> map; public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; }
配置文件:
List与Set同理:
Map双列集合:
三:基于注解的DI
@Component[不分层的情况下]
@Repository() [Dao层]
@Service() [Biz层]
@Controller() [Action类]
注:在项目中添加Spring AOP相关的JAR文件以及xsd约束文件。
由于是基于注解的DI,所以无需再Spring配置文件中进行节点配置,只需配置包扫描器即可!
配置包扫描器用途:
该包下以及子包中的类才可以被Spring扫描,去寻找被注解的类和属性,让Spring容器管理赋值
Student类:
指定@Component中的value即可在测试类中的getBean()中植入即可。
@Value为该属性赋值
@Component(value="stu") public class Student { @Value("呵呵") private String name; @Value("13") private String age; /* * JDK注解 @Resource(name="car2") */ /* * Spring注解 */ @Autowired @Qualifier(value="car2") private Car car; @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", car=" + car + "]"; }
Car类:
@Component(value="car2") public class Car { @Value("黑色") private String color; @Value("奥迪") private String brand; @Override public String toString() { return "Car [color=" + color + ", brand=" + brand + "]"; }
人生最重要的一点是,永远不要迷失自己。
---加油