IOC(控制反转)
解释
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是Spring来创建的。
反转:程序本身不创建对象,而变成被动的接受对象
依赖注入(DI)
解释
依赖:bean对象的创建依赖于容器!
注入:bean对象中的所有属性,由容器来注入!
在配置文件加载的时候,容器中管理的对象就已经初始化了!且属于 单例模式
无参创建(设值 (set) 注入)
实体类Student
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 wife;
private Properties info;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public Address getAddress() {return address;}
public void setAddress(Address address) {this.address = address;}
public String[] getBooks() {return books;}
public void setBooks(String[] books) {this.books = books;}
public List<String> getHobbys() {return hobbys;}
public void setHobbys(List<String> hobbys) {this.hobbys = hobbys;}
public Map<String, String> getCard() {return card;}
public void setCard(Map<String, String> card) {this.card = card;}
public Set<String> getGames() {return games;}
public void setGames(Set<String> games) {this.games = games;}
public String getWife() {return wife;}
public void setWife(String wife) {this.wife = wife;}
public Properties getInfo() {return info;}
public void setInfo(Properties info) {this.info = info;}
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
实体类Address
public class Address {
private String address;
public String getAddress() {return address;}
public void setAddress(String address) {this.address = address;}
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
applicationContext.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.cf.pojo.Address">
<property name="address" value="大连"/>
</bean>
<!--使用Spring来创建对象,在Spring这些都成为Bean
类型 变量名 = new 类型();
Hello hello = new Hello();
id = 变量名
class = new的对象
property 相当于给对象中的属性设置一个值!
-->
<bean id="student" class="com.cf.pojo.Student">
<!--普通值注入,value-->
<property name="name" value="眯帝"/>
<!--Bean注入,ref-->
<property name="address" ref="address"/>
<!--
ref :引用Spring容器中创建好的对象
value :具体的值,基本数据类型!
-->
<!--数组-->
<property name="books">
<array>
<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="1252335"/>
<entry key="会员卡" value="scx111"/>
</map>
</property>
<!--set-->
<property name="games">
<set>
<value>King</value>
<value>饥荒</value>
<value>三国杀</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">2003</prop>
<prop key="url">127.0.0.1</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
测试类
public static void main(String[] args) {
//获取Spring的上下文的对象!
//Spring容器,类似于婚介所网站!不管你用不用,它会将里面的全部实例化
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//我们的对象现在都在Spring中的管理了,我们要使用,直接去里面取出来就可以!
Student student = (Student)context.getBean("student");
System.out.println(student);
}
结果
Student{
name=‘眯帝’,
address=Address{address=‘大连’},
books=[人生失格, 战争与和平, 昆虫记],
hobbys=[桌游, 象棋, 影视],
card={身份证=1252335, 会员卡=scx111},
games=[King, 饥荒, 三国杀],
wife=‘null’,
info={password=root,
url=127.0.0.1, driver=2003, username=root}
}
OK,到了现在,我们彻底不用再程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的loC,一句话搞定:对象由Spring来创建,管理,装配
有参创建(构造注入)
实体类(User)
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String name) {
this.name = name;
}
public User() {}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
applicationContext.xml
三种赋值方式
1、下标赋值
<bean id="user" class="com.cf.pojo.User">
<constructor-arg index="0" value="眯帝"></constructor-arg>
</bean>
2、类型创建(不建议使用,存在同类型参数就会混乱)
<bean id="user" class="com.cf.pojo.User">
<constructor-arg type="java.lang.String" value="眯帝"></constructor-arg>
</bean>
3、通过参数名设置
<bean id="user" class="com.cf.pojo.User">
<constructor-arg name="name" value="眯帝"></constructor-arg>
</bean>
拓展范方式注入
我们可以使用p命令空间和c命令空间进行注入
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"
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命名空间注入,可以直接注入属性的值:properties-->
<bean id="user" class="com.cf.pojo.User" p:age="18" p:name="mid"/>
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.cf.pojo.User" c:age="19" c:name="mid"/>
</beans>
测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//p命令空间
User user = context.getBean("user",User.class);
System.out.println(user);
//c命令空间
User user2 = context.getBean("user2",User.class);
System.out.println(user2);
}
注意点:p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns :c=“http : //www.springframework.org/schema/c”
xmlns :p=“http : //www.springframework.org/schema/p”