当前位置: 移动技术网 > IT编程>开发语言>Java > spring框架学习(一)——IOC/DI

spring框架学习(一)——IOC/DI

2019年11月05日  | 移动技术网IT编程  | 我要评论

什么是spring框架:

  spring是一个基于ioc和aop的结构j2ee系统的框架:

  ioc 反转控制 是spring的基础,inversion of control,简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由spring创建对象;

  di 依赖注入 dependency inject, 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

原理分析:

  以获取对象的方式来进行比较

    传统的方式:
      通过new 关键字主动创建一个对象。
    ioc方式:
      对象的生命周期由spring来管理,直接从spring那里去获取一个对象。 ioc是反转
控制 

      (inversion of control)的缩写,就像控制权从本来在自己手里,交给了spring。

  打个比喻:
    传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上

         花椒,酱油,烤制,经过各种工序之后,才可以食用。
    用 ioc:相当于去馆子(spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管

        吃就行了。

                  

 

 

接下来通过运行testspring演示如何用spring获取一个对象,并打印其name以及使用ioc的方式,对product对象,注入一个category对象

(创建项目以及导包过程已省略,关于spring项目中需要的jar包介绍,)

准备pojo category 、product,用来演示ioc和di:

package com.spring.pojo;
 
public class category {
 
    public int getid() {
        return id;
    }
    public void setid(int id) {
        this.id = id;
    }
    public string getname() {
        return name;
    }
    public void setname(string name) {
        this.name = name;
    }
    private int id;
    private string name;
}

 

package com.spring.pojo;
 
public class product {
 
    private int id;
    private string name;
    private category category;
    public int getid() {
        return id;
    }
    public void setid(int id) {
        this.id = id;
    }
    public string getname() {
        return name;
    }
    public void setname(string name) {
        this.name = name;
    }
    public category getcategory() {
        return category;
    }
    public void setcategory(category category) {
        this.category = category;
    }
}

 

创建applicationcontext.xml:

    在src目录下新建applicationcontext.xml文件;
    applicationcontext.xml是spring的核心配置文件,通过关键字p即可获取
product对象

    (ioc过程)该对象获取的时候,即被注入了字符串"product 1“到name属性中(di过程)

    在创建product的时候注入一个category对象,注意,这里要使用ref来注入另一个对象(di过程)

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemalocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <bean name="c" class="com.spring.pojo.category">
        <property name="name" value="category 1" />
    </bean>
   <bean name="p" class="com.how2java.pojo.product">
       <property name="name" value="product1" />
            <!--使用ref注入category对象-->
       <property name="category" ref="c" />
    </bean>
</beans>

 

testspring:
    测试代码,演示通过spring获取product对象,以及该对象被注入的name和category属性。如图所示,可以打印
    出通过spring拿到的product对象的name属性和category对象的name属性。
          
package com.spring.test;
 
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
 
import com.spring.pojo.product;
 
public class testspring {
 
    public static void main(string[] args) {
        //读取配置文件
        applicationcontext context = new classpathxmlapplicationcontext(
              new string[] { "applicationcontext.xml" });
        //通过关键字“c”,从spring中获取category对象
        product p = (product) context.getbean("p");
      //p.getname()获取p对象中属性name的值,p.getcategory().getname()获取category的name值 system.out.println(p.getname());
     system.out.println(p.getcategory().getname());
}
}

 注:读取配置文件:

//方式一:(只能读取一个配置文件)
applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml");
//方式二:(可读取一个或多个配置文件)
applicationcontext context = new classpathxmlapplicationcontext(
                new string[] { "applicationcontext.xml" });

 

原文地址:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网