(B) Spring Framework: Getting the spring IOC

A, spring IOC's Profile

1, the coupling procedure

In software engineering, coupling refers to is the dependencies between objects. The higher the coupling between objects, higher maintenance costs. Therefore, the design should minimize object coupling between classes and components. Software design and the degree of coupling is generally used as a measure of the cohesion degree of independence of the standard module. A division criteria module is coupled to highly cohesive.

2, spring IOC concept

The IOC (Inversion Of Controll) : Inversion of Control , i.e., no need to create new objects in the form of through Spring configuration file, created dynamically using the factory design pattern. Let spring managed objects, easy to maintain code.

Two, spring IOC entry

1, create a maven project and import coordinates

2, create the entity class 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, create a profile 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. Create a test class testing 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);

    }
}

Three, spring IOC's API instructions

1, to obtain the spring core Ioc container implementation class

  • ClassPathXmlApplicationContext

        The configuration file may be loaded class path, it requires the configuration file must be in the class path. Creating one of the core container of the most commonly used method.

  • FileSystemXmlApplicationContext

       Configuration files can be loaded at any disk path (must have access to)

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

       Create a container used to read notes

2, get spring of Ioc container interface to the core

  • ApplicationContext: inherited from BeanFactory, it is building a core container, create a policy object is taken by way of immediate loading. Commonly used in the singleton object
  • BeanFactory: when building core container, create a policy object is taken by way of lazy loading. Commonly used in cases of the object.
发布了134 篇原创文章 · 获赞 10 · 访问量 7360

Guess you like

Origin blog.csdn.net/yu1755128147/article/details/103539938