一、IOC的本质
控制反转(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IOC的一种方法,有人认为DI只是IOC的另一种说法。在没有ioc的程序中,使用面向对象编程,对象的创建和对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转将对象的创建转移到第三方,有大佬认为控制反转就是:获得依赖对象的方式反转了。
控制反转是一种通过描述(XML或注解)通过第三方生产或获取特定对象的方式。在spring中实现控制反转的是IOC容器,其实现方式是依赖注入(Denpendency Injection,简写为 DI)
二、第一个Spring项目
1.创建一个Hello实体类
public class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Hello{" +
"name='" + name + '\'' +
'}';
}
}
2.创建测试类MyTest
public class MyTest {
public static void main(String[] args) {
//获取Spring上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
//对象都在Spring管理了,要使用,直接取
Hello hello = (Hello) context.getBean( "hello" );
System.out.println( hello.toString() );
}
}
3.在resources下创建一个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-3.0.xsd">
<!-- 使用Spring来创建对象,在Spring中这些都叫Bean
bean = 对象 new Hello();
id = 变量名
class = new 的对象
property相当于给对象中的属性设值
value具体的值,基本数据类型
-->
<bean id="hello" class="com.rxj.pojo.Hello">
<property name="name" value="Spring" />
</bean>
</beans>
总结:所谓IOC就是对象由Spring来创建、管理、装配!
三、IOC创建对象的方式
- 使用无参构造创建对象,这是IOC默认的
- 使用有参构造创建对象
1.下标赋值
<!-- 下标赋值--> <bean id="hello" class="com.rxj.pojo.Hello"> <constructor-arg index="0" value="来一沓Java"/> </bean>
2.通过类型创建
<!--通过类型创建,不建议使用,因为当它的属性有多个相同类型时会出现问题--> <bean id="hello" class="com.rxj.pojo.Hello"> <constructor-arg type="java.lang.String" value="来一沓Java"/> </bean>
3.通过参数名来设置
<!--直接通过参数名来设置--> <bean id="hello" class="com.rxj.pojo.Hello"> <constructor-arg name="name" value="来一沓Java"/> </bean>
总结:在配置文件加载时候,容器中管理的对象就已经初始化了!
四、Spring配置
1.别名
<!-- 如果添加了别名,可以使用别名获取到这个对象-->
<alias name="hello" alias="newhello"/>
2.Bean的配置
<!--
Bean的配置:
id:bean的唯一标识符,相当于对象名
class:bean对象所对应的全限定名
name:别名,且可以取多个别名
-->
<bean id="hello" class="com.rxj.pojo.Hello" name="hello2,hello3">
<constructor-arg name="name" value="来一沓Java"/>
</bean>
3.导入(import)
import一般用于团队开发,可以将多个配置文件导入,合并为一个
新建一个xml文件applicationContext,导入之前的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-3.0.xsd">
<import resource="Beans.xml"/>
</beans>
五、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 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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString( books ) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
applicationContext.xml
<bean id="address" class="com.rxj.pojo.Address">
<property name="address" value="上海"/>
</bean>
<bean id="student" class="com.rxj.pojo.Student">
<!-- 普通值注入,直接使用value即可-->
<property name="name" value="来一沓Java"/>
<!-- bean注入,ref-->
<property name="address" ref="address"/>
<!-- 数组-->
<property name="books">
<array>
<value>java编程</value>
<value>c语言编程</value>
<value>c++编程</value>
</array>
</property>
<!-- List-->
<property name="hobbys">
<list>
<value>打游戏</value>
<value>学习</value>
<value>看电影</value>
</list>
</property>
<!-- map-->
<property name="card">
<map>
<entry key="学号" value="1234567890"/>
<entry key="电话" value="01234567891"/>
</map>
</property>
<!-- set-->
<property name="games">
<set>
<value>王者农药</value>
<value>和平吃鸡</value>
</set>
</property>
<!-- null注入-->
<property name="wife">
<null/>
</property>
<!-- Properties-->
<property name="info">
<props>
<prop key="邮箱">123456@163.com</prop>
<prop key="性别">男</prop>
<prop key="姓名">筱筱</prop>
</props>
</property>
</bean>
六、c命名和p命名注入
p命名注入
导入头文件约束和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-3.0.xsd">
<!-- p命名空间注入,可以直接注入属性的值propert-->
<bean id="user" class="com.rxj.pojo.User" p:name="来一沓Java" p:age="16"/>
</beans>
测试
@Test
public void testP(){
ApplicationContext context = new ClassPathXmlApplicationContext( "UserBeans.xml" );
User user = context.getBean( "user", User.class );
System.out.println( user );
}
c命名注入
导入头文件约束和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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- c命名空间注入,通过构造器注入construct-args-->
<bean id="user" class="com.rxj.pojo.User" c:name="来一沓Java" c:age="16"/>
</beans>
测试
@Test
public void testC(){
ApplicationContext context = new ClassPathXmlApplicationContext( "UserBeans.xml" );
User user = context.getBean( "user", User.class );
System.out.println( user );
}
注意:c命名和p命名不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"