02-spring frame - IoC inversion of control

  Inversion of Control (IoC, Inversion of Control), is a concept, an idea. Traditionally refers to the generation of the program
code directly to the manipulated object invoking the right container, and the assembly management object is achieved by the container.

  Inversion of control is transferred to the object of control from the program code itself reversed to the outside of the container.

Achieve the creation of objects by container, attribute assignment, dependency management.

  IoC is a concept, an idea, which is to achieve a variety of ways. Current popular implementation is dependent on
injection. Wide range of applications.

  Dependent: classA classB class contains instances, the method calls classB classA the completion, i.e. classA
of classB have dependencies.

Ioc implementation:
➢ dependent lookup: DL (Dependency Lookup), and the container provides the context to the callback interface assembly.
➢ dependency injection: DI (Dependency Injection), not the program code location query, the work by the container itself
is completed.

  DI DI refers to the program is running, if the call requires the assistance of another object, without having to create in the code
is the caller, but rather on the outer container, is passed to the program created by the outer container.

  Spring's dependency injection caller almost no requirements and callee, fully supports dependencies between objects
management.

  Spring Framework dependency injection (DI) to achieve IoC.

  Spring is a super large container plant, is responsible for creating, managing all the Java objects, these Java objects are said
to Bean. Spring container manages dependencies between container Bean, Spring use "dependency injection" approach
to managing dependencies between Bean. Use IoC and achieve decoupling between objects.

2.1 development tools ready

Development Tools: idea2017 above
dependency management: maven

Set the machine needs to be set maven repository:

 

The first of a 2.2 Spring

Example: 01-primay

2.2.1 Creating maven 

quickstart templates

 

2.2.2 introduced maven dependent pom.xml

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.16.RELEASE</version>
</dependency>
<!--插件-->
<plugins>
  <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.1</version>
     <configuration>
       <source>1.8</source>
       <target>1.8</target>
     </configuration>
  </plugin>
</plugins>

2.2.3  定义接口与实体类

public interface SomeService {
  void doSome();
}
public class SomeServiceImpl implements SomeService {
  public SomeServiceImpl() {
    super();
    System.out.println("SomeServiceImpl无参数构造方法");
  }
  @Override
  public void doSome() {
    System.out.println("====业务方法doSome()===");
  }
}

 2.2.4  创建 Spring 

  在 src/main/resources/目录现创建一个 xml 文件,文件名可以随意,但 Spring 建议的名
称为 applicationContext.xml。

  spring 配置中需要加入约束文件才能正常使用,约束文件是 xsd 扩展名。

  使用约束文件的语法在:xsd-configuration.html。这个 xsd-configuration.html 文件位置是
在%SPRING_HOME%\docs\spring-framework-reference\html\xsd-configuration.html

  SPRING_HOME:是从 spring 官网下载的资源 spring-framework-4.3.16.RELEASE-dist.zip 解
压后的目录。

 

  注意,Spring 配置文件中使用的约束文件为 xsd 文件。若 Eclipse 中没有自动提示功能,
则需要将约束要查找的域名地址指向本地的 xsd 文件。相应的 xsd 文件在 Spring 框架解压目
录下的 schema 目录的相应子目录中。

  约束文件:xsd(xml schema definition) xml 文档结构定义。

  作用:验证 xml 文档的逻辑结构是否正确。
  1.定义一个 xml 文档中都有什么元素
  2.定义一个 xml 文档中都有什么属性
  3.定义一个 xml 文档中元素可以有哪些子元素,以及元素的顺序。
  4.定义一个 xml 文档中元素和属性的数据类型。

  这里需要的是 spring-beans.xsd 约束文件,故需要在 beans 子目录中查找相应版本
的约束文件。

<bean />:用于定义一个实例对象。一个实例对应一个 bean 元素。
id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean 与 Bean 间的依
赖关系也是通过 id 属性关联的。

2.2.5 定义测试类

@Test
    public void test01(){
        System.out.println( "Hello Spring!" );
        //定义Spring的配置文件, 配置文件是在类路径的根目录之下
        String config = "applicationContext.xml";

        //创建Spring的容器对象.根据Spring配置文件的位置,使用接口的不同实现类
        //如果Spring的配置文件是在类路径(classpath),使用ClassPathXmlApplicationContext
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);

        //从容器中获取对象 使用getBean("<bean>的id")
        SomeService service = (SomeService) ctx.getBean("someService");

        //调用业务方法
        service.doSome();
    }

 

2.2.6  使用 spring  创建 非自定义类对象

spring 配置文件加入 java.util.Date 定义:
<bean id="myDate" class="java.util.Date" />

MyTest 测试类中:
调用 getBean(“myDate”); 获取日期类对象。

2.2.7  容器接口和实现类

ApplicationContext  接口 (容器)

ApplicationContext 用于加载 Spring 的配置文件,在程序中充当“容器”的角色。其实现
类有两个。

Guess you like

Origin www.cnblogs.com/Tpf386/p/10988696.html