Spring框架(二):spring IOC的入门

一、spring IOC的简介

1、程序的耦合

在软件工程中,耦合指的就是就是对象之间的依赖性。对象之间的耦合越高,维护成本越高。因此对象的设计应使类和构件之间的耦合最小。软件设计中通常用耦合度和内聚度作为衡量模块独立程度的标准。划分模块的一个准则就是高内聚低耦合。

2、spring IOC的概念

IOC(Inversion Of Controll)控制反转,即不需要通过new的形式创建对象,通过Spring的配置文件,使用工厂设计模式动态创建。让spring管理对象,方便代码的维护。

二、spring IOC入门

1、创建maven工程并导入坐标

2、创建实体类UserInfo

package com.wedu.spring01.entity;

import java.util.Date;

/**
 * 用户信息实体
 */
public class UserInfo {

    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private Date regtime;
    private String siteaddress;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getRegtime() {
        return regtime;
    }

    public void setRegtime(Date regtime) {
        this.regtime = regtime;
    }

    public String getSiteaddress() {
        return siteaddress;
    }

    public void setSiteaddress(String siteaddress) {
        this.siteaddress = siteaddress;
    }
}

 3、创建配置文件bean.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来管理-->
    <bean id="userInfo" class="com.wedu.spring01.entity.UserInfo"/>
</beans>

4、创建测试类UserInfoTest进行测试

package com.wedu.spring01.entity;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * spring ioc入门
 */
public class UserInfoTest {

    @Test
    public void createUserInfoTest() {
        //1、获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取Bean对象
        UserInfo userInfo = (UserInfo)ac.getBean("userInfo");
        System.out.println(userInfo);

    }
}

三、spring IOC的API说明

1、获取spring的Ioc核心容器的实现类

  • ClassPathXmlApplicationContext

        可以加载类路径下的配置文件,要求配置文件必须在类路径下。创建核心容器最常用的方法之一。

  • FileSystemXmlApplicationContext

       可以加载磁盘任意路径下的配置文件(必须有访问权限)

ApplicationContext ac = new FileSystemXmlApplicationContext("E:\\project\\idea_workspace\\spring\\spring01\\src\\main\\resources\\bean.xml");
  • AnnotationConfigApplicationContext

       用于读取注解创建容器的

2、获取spring的Ioc核心容器接口

  • ApplicationContext:继承于BeanFactory,它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。常用于单例对象
  • BeanFactory:在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。常用于多例对象使用。
发布了134 篇原创文章 · 获赞 10 · 访问量 7360

猜你喜欢

转载自blog.csdn.net/yu1755128147/article/details/103539938