Overview and use IOC containers Spring - Spring interpretation of official documents (a)

Overview and use IOC containers Spring - Spring interpretation of official documents (a)

What is Spring?

spring This word has different meanings in different contexts. Spring Framework itself may refer to, but is used to represent more Spring entire family of products.

design concept

Learning framework must know its design concept, Spring Framework has the following philosophy:

  • Spring gives you more choices at all levels of the architecture types, and allows you to make decisions as late as possible. For example, you can change the configuration after completion of the project to switch providers persistence layer.
  • Spring has great flexibility, it does not care how you complete the work, which is as much as possible to support different application requirements.
  • Spring has strong backward compatibility. Spring update are well designed, but it requires the JDK version is carefully selected, it makes you very easy to update your product.
  • Spring API's design team is very cautious, which makes these API is completely generic versions of many species, but also can be used for several years.
  • Spring's own code is very simple, and very easy to read javadoc, it is a very small write-only meaningful source projects, but also between its package, no circular dependencies.

Inversion of Control container (IOC Container)

IOC Introduction

IOC is also known as injection dependency (dependency injection (DI)), the programmer to establish relationships between the various objects by setting the configuration parameters, parameters means a factory method, so that, when the programmer creates the bean, the corresponding dependency is automatically injected into the bean container, programmers do not need to manually create new objects that when the bean. This makes it easier to manage dependency. (Referred to as inversion of control is no longer dependent on the programmer to manually injected into the respective bean, but was automatically injected through the container)

org.springframework.beansAnd org.springframework.contextthe two packages are Spring IOC to the container base. Interface BeanFactoryprovides advanced configuration mechanism capable of managing any type of object. ApplicationContextIs BeanFactorya sub-interface, it is more out of these features:

  • Easier integration with Spring's AOP
  • Message resource handling (for international)
  • Event Publishing
  • Specific Context (eg, a web application WebApplicationContext)

All in all, BeanFactoryprovides basic functions, and ApplicationContextprovides specific advanced features, is BeanFacotrya superset. (Usually used only when integrated with other frameworks BeanFactory)

Container Overview

org.springframework.context.ApplicationContextSpring IOC interface represents a container, which is responsible for instantiating the bean assembled configuration element by reading data, configuration. Metadata is written in a way, there are three: XML, Java annotations or Java code. You can build complex dependencies between objects through it.

Spring provides a lot of ApplicationContextimplementation, in stand-alone applications, the most common are ClassPathXmlApplicationContextand FileSystemXmlApplicationContext. XML is the most common format to write metadata. You can also configure make it by writing a small amount of XML metadata can support reading from notes or code.

In most scenarios, the user need not explicitly instantiate one or more IOC container.

Write metadata to configure and use

The contents of the above, we can know the Spring IOC container is to understand the relationship between the various objects via metadata. The most traditional way to write metadata using XML configuration files. (Of course this is not the only way, and now more people will use the code to write notes or metadata). Of course, you can also be configured with AOP objects outside the IOC administration. (Which will appear in subsequent portions of the content.)

Now, we look at the fundamental basis of the presentation of XML metadata:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">
        <!-- id 属性用于指定该 bean 的名字,而 class 属性需要写入一个完整的 classname 来确定为那个类的对象,比如 top.dragondove.demo.bean.Hero -->
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

When you write, we instantiate the container and get the contents of the container, the following is an example.

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
Hero dove = context.getBean("dove", Hero.class);
System.out.println(dove);

These are simple to use Spring container, the next section will explain more metadata configuration and content configuration. The content of the complete code listed below, as initialization Spring projects can directly use Spring Boot (using the official website of the Spring the Boot Initializr can be, you can rely on the whole not chosen):

// src/main/java/top/dragondove/demo/bean/Hero.java
package top.dragondove.demo.bean;

// 普通的 javabean
public class Hero {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    @Override
    public String toString() {
        return "Name: " + name + ", Age: " + age;
    }
}
<!-- src/main/resources/applicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 配置 bean 的元数据 -->
    <bean id="dove" class="top.dragondove.demo.bean.Hero">
        <property name="name" value="Dove" />
        <property name="age" value="18" />
    </bean>
</beans>
// src/main/java/top/dragondove/demo/DemoApplication.java
package top.dragondove.demo;

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

import top.dragondove.demo.bean.Hero;

public class DemoApplication {

    private static ApplicationContext context;

    public static void main(String[] args) {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hero dove = context.getBean("dove", Hero.class); // 获取容器中 id 为 dove 的对象并确定其类型为 Hero 类型。
        System.out.println(dove);
    }

}

It DemoApplication runs in the main method to see the effect.

Guess you like

Origin www.cnblogs.com/dragondove/p/11415798.html