**1、Spring Ioc**创建对象的方式
构造方法创建对象:
1、使用无参构造创建对象,是默认实现;
2、若使用有参构造创建对象,则:
补充一个,导入junit包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
- 使用下标赋值,index赋值0,代表构造方法第一个参数
<bean id="user" class="com.hua.pojo.User">
<constructor-arg index="0" value="张三"></constructor-arg>
</bean>
- 使用类型赋值,但是类型不能重复,不建议使用
<bean id="user" class="com.hua.pojo.User">
<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
</bean>
- 直接通过参数名赋值
<bean id="user" class="com.hua.pojo.User">
<constructor-arg name="name" value="张三"></constructor-arg>
</bean>
注:在配置文件加载的时候,容器中管理的对象就已经被初始化了。
**2、Spring的配置**
别名
alias可以通过别名取到name的值=张三
<bean id="user" class="com.hua.pojo.User">
<property name="name" value="张三"/>
</bean>
<!--别名-->
<alias name="user" alias="aUser"></alias>
Bean的配置
<!--id:Bean的唯一标识符,也就是相当于对象名
class:Bean对象对应的全限定名:包名+类名
name:别名,而且可以取多个别名,比如:name="name1,name2"
-->
<bean id="user" class="com.hua.pojo.User" name="user1,user2">
<property name="name" value="张三"/>
</bean>
Import
<import resource="beans.xml"></import>
可以将多个配置文件,导入合并为一个配置文件
**3、DI依赖注入**
1、 构造器注入
2 、set注入(重点)
-
依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器注入
复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wifo;
private Properties info;
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Sprig这些都成为Bena-->
<bean id="student" class="com.hua.pojo.Student" name="user1,user2">
<!--第一种,普通值注入,value-->
<property name="name" value="张三"/>
</bean>
</beans>
测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Student student=(Student) context.getBean("student");
System.out.println(student.getName());
}
}
完善XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.hua.pojo.Address">
<property name="address" value="王五"/>
</bean>
<!--使用Spring来创建对象,在Sprig这些都成为Bena-->
<bean id="student" class="com.hua.pojo.Student" name="user1,user2">
<!--普通值注入,value-->
<property name="name" value="张三"/>
<!--Bean注入,ref-->
<property name="address" ref="address"></property>
<!--数组注入,ref-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>三国志</value>
<value>红楼梦</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>跑步</value>
<value>跳舞</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="123456789"></entry>
<entry key="银行卡" value="987654321"></entry>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
</set>
</property>
<!--null-->
<property name="wifo">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">001</prop>
<prop key="姓名">李四</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
3、 拓展方式注入
P命名空间注入
创建user类
package com.hua.pojo;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建XML,在XML中插入一条
xmlns:p=“http://www.springframework.org/schema/p”
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.hua.pojo.User" p:name="张三2" p:age="22"/>
</beans>
测试
@Test
public void test2(){
ApplicationContext context=new ClassPathXmlApplicationContext("userBeans.xml");
User user=context.getBean("user",User.class);
System.out.println(user.toString());
}
C命名空间注入
需要在实体类中定义构造方法
增加语句
xmlns:c=“http://www.springframework.org/schema/c”
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.hua.pojo.User" p:name="张三2" p:age="22"/>
<!--C命名空间注入,通过构造器注入,construct-args-->
<bean id="user2" class="com.hua.pojo.User" c:name="张三3" c:age="20"/>
</beans>