DI依赖注入
概念:
依赖注入(Dependency Injection,DI)。
依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 。
注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 。
分类
- 构造器注入
创建Person类
/*
创建Person类
构造器注入
*/
public class Person {
//注入对象
private String name;
private int age;
private String sex;
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
在这里插入代码片
application.xml配置
<!--构造器注入-->
<bean id="person" class="com.yoda.pojo.Person">
<constructor-arg name="name" value="huahua"/>
<constructor-arg name="age" value = "20"/>
<constructor-arg name="sex" value="male"/>
</bean>
测试
/*
测试构造器注入
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Person person = (Person)context.getBean("person");
System.out.println(person);
}
}
- set注入(重点)
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
测试pojo类 :
Address.java
在这里插入代码片//set注入
//引用对象
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Student.java
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String dog; //空指针
private Properties info;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setDog(String dog) {
this.dog = dog;
}
public void setInfo(Properties info) {
this.info = info;
}
public void show(){
System.out.println("name="+ name
+ ",address="+ address.getAddress()
+ ",books="
);
for (String book:books){
System.out.print("<<"+book+">>\t");
}
System.out.println("\n爱好:"+hobbies);
System.out.println("card:"+card);
System.out.println("games:"+games);
System.out.println("dog:"+dog);
System.out.println("info:"+info);
}
}
- 常量注入
<bean id="stu" class="com.yoda.pojo.Student">
<!--第一种:(普通值)常量注入 value-->
<property name="name" value="香茗"/>
</bean>
测试
/*
测试set注入
*/
public class MyTest2 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Student student =(Student) context.getBean("stu");
System.out.println(student.getName());
}
}
- Bean注入
<!--第二种:bean注入 这里的值是引用 ref-->
<property name="address" ref="add"/>
- 数组注入
<!--数组注入-->
<property name="books">
<array>
<value>海底两万里</value>
<value>老人与海</value>
<value>海边的卡夫卡</value>
</array>
</property>
- List注入
<!--list注入-->
<property name="hobbies">
<list>
<value>看书</value>
<value>听音乐</value>
<value>写小说</value>
</list>
</property>
- Map注入
<!--map注入-->
<property name="card">
<map>
<entry key="身份证" value="23342343242342"/>
<entry key="银行卡" value="23432423423423"/>
</map>
</property>
- set注入
<!--set注入-->
<property name="games">
<set>
<value>音乐</value>
<value>竞技</value>
<value>格斗</value>
</set>
</property>
- Null注入
<!--null注入-->
<property name="dog">
<null/>
</property>
- properties注入
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">232323</prop>
<prop key="性别">女</prop>
<prop key="能力">强者</prop>
</props>
</property>
测试
/*
测试set注入
*/
public class MyTest2 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Student student =(Student) context.getBean("stu");
student.show();
}
}
p命名和c命名注入(扩展)
User.java
/*
注意:这里没有有参构造器
*/
public class User {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- P命名空间注入 : 需要在头文件中加入约束文件
<!--P(属性: properties)命名空间 , 属性依然要设置set方法
p空间注入 需导入约束 : xmlns:p="http://www.springframework.org/schema/p"
-->
<bean id="user" class="com.yoda.pojo.User" p:name="huahau" p:age="20"/>
- C命名空间注入 : 需要在头文件中加入约束文件
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法
c空间注入 导入约束 : xmlns:c="http://www.springframework.org/schema/c"
-->
<bean id="user2" class="com.yoda.pojo.User" c:name="huahau" c:age="20"/>
注意点:需要写入有参构造器,否则报红!
测试
/*
p/c命名注入
*/
public class MyTest3 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
}