Spring中的IOC

来源:互联网 发布:吸血臭虫 知乎 编辑:程序博客网 时间:2024/06/09 18:41

一:何为IOC

         Spring中主要有两个结构,一个是IOC还有一个是AOP,今天我们讲的主要是IOC。IOC是什么?从字面意思来理解他就是控制反转(Inversion of Control),进一步理解就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的,这样控制权就由应用转移到了外部容器,控制权的转移就是所谓的反转。用一句话来总结就是以前我们用代码实现的创建对象以及依赖等我们都用Spring中的配置文件来实现了。


二:搭建Spring框架

       1、前提:在线或离线引入Spring TOOL(建议在线下载必须联网络,如何在线下载--help/Eclipese Marketplace...在里面搜索spring然后找到有sts的图标点击下载,点击下一步就可以了,下载完后会提示重启软件点击yes就可以了)

          注:如果你重新进入软件发现项目报错的话,先点击右键refresh看还会不会报错,要是还在报错的话就点击右键选择Maven/Update project,把你报错的项目选中点击ok就可以了(这里的报错是指项目和Java Resource报错,但是没有显示那个具体的类报错

       2、在eclipse里面搭建spring框架

              2.1、导入jar包,也就是依赖(Spring官网:www.spring.io

                         <!-- 引入Spring依赖 -->
                          <dependency>
                              <groupId>org.springframework</groupId>
                              <artifactId>spring-context</artifactId>
                             <version>4.3.10.RELEASE</version>
                          </dependency>  


               2.2、applicationContext,xml配置文件,在Spring配置文件中配置bean

               2.3、bean标签的属性和子标签

                              属性: init-method:初始化默认调用的方法             lazy-init:懒加载

                              子标签:<property> name/value/ref

              2.4、设置值:

                          

              2.5、 加载配置文件:延迟加载(懒加载)、及时加载                         

            

三:案例讲解(代码实现)

             1、创建一个实体类

 package com.entity;

import java.util.List;
import java.util.Map;

public class Person {

private int pid;
private String pname;
private int page;
//集合注入
private List<String> hobbies;
//对象注入
private Card card;
//map集合注入
private Map<String, Card> map;

public Map<String, Card> getMap() {
return map;
}


public void setMap(Map<String, Card> map) {
this.map = map;
}


public Card getCard() {
return card;
}


public void setCard(Card card) {
this.card = card;
}


public List<String> getHobbies() {
return hobbies;
}


public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}




public Person() {
super();
System.out.println("person的构造方法");
}


public Person(String pname, int page) {
super();
this.pname = pname;
this.page = page;
}


public Person(int pid, String pname, int page) {
super();
this.pid = pid;
this.pname = pname;
this.page = page;
}


public int getPid() {
return pid;
}


public void setPid(int pid) {
this.pid = pid;
}


public String getPname() {
return pname;
}


public void setPname(String pname) {
this.pname = pname;
}


public int getPage() {
return page;
}


public void setPage(int page) {
this.page = page;
}


}


在创建一个需要通过对象来实现赋值的实体类

 package com.entity;

public class Card {
    private int cid;
    private int cnum;

public Card() {
super();
}
public Card(int cnum) {
super();
       this.cnum = cnum;
}
public Card(int cid ,int cnum) {
super();
this.cid = cid;
this.cnum = cnum;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public int getCnum() {
return cnum;
}
public void setCnum(int cnum) {
this.cnum = cnum;
}

}


2、创建Spring的配置文件,配置属性

 <?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">
    <!-- 配置Person对象 -->
     <bean id="person" class="com.entity.Person">
      <!-- 注入:设值 -->
         <!-- set注入 -->
         
            <property name="pid" value="5"></property>
            <property name="pname" value="张无忌"></property>
            <property name="page" value="18"></property> 
            <property name="hobbies">
               <list>  <!--集合的注入-->
                 <value><![CDATA[<张无忌>]]></value>  <!--特殊字符的注入-->
                 <value>打羽毛球</value> 
                 <value>听歌</value>   
               </list>
            </property>


            <!-- 注入对象 -->
            <property name="card" ref="card"></property>
            
            <!-- 注入map集合 -->
            <property name="map">
              <map>
                <entry key="23" value-ref="card"></entry>
               </map>
            
            </property>
         <!-- 构造注入:设值 -->
          <!--  <constructor-arg value="1"></constructor-arg>
           <constructor-arg value="张云"></constructor-arg>
           <constructor-arg value="29"></constructor-arg> -->
         
     </bean>
    
       
    <!--设值Card对象 --> 
     <bean id="card" class="com.entity.Card">
        <!-- 注入设:设值 -->
           <!-- set注入式 -->
            <property name="cid" value="001"></property>
            <property name="cnum" value="1103254689"></property>
         
     </bean>

</beans>

3、测试类进行代码测试

package com.zking.test;


import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;


import com.entity.CellPhone;
import com.entity.Person;


public class TestSpring {
         /*

         *  何为懒加载和及时加载?

         * 所谓的懒加载就是你需要用的时候才进行加载,而及时加载就是不管你需不需要它都会在最开始就把东西加载完成

         */        



@Test
public void tests(){
//spring
  //懒加载
// BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
// Person person=(Person) beanFactory.getBean("person");
//及时加载
 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
 Person person=(Person) applicationContext.getBean("person");
  System.out.println(person.getHobbies());
  System.out.println(person.getMap());
  System.out.println(person.getCard().getCnum());
 
}

}