Summary of Spring Learning

Keywords: Spring xml Attribute JDBC

IOC of spring

I. Introduction to spring

The Spring framework is a layered architecture consisting of seven well-defined modules. The Spring module is built on top of the core container, which defines how bean s are created, configured, and managed.


Each module (or component) that makes up the Spring framework can exist independently or be implemented in conjunction with one or more other modules. The functions of each module are as follows:

  • Core container: Core container provides the basic functions of Spring framework. The main component of the core container is BeanFactory, which is the implementation of the factory pattern. BeanFactory uses the Inversion of Control (IOC) pattern to separate application configuration and dependency specifications from the actual application code.
  • Spring context: The Spring context is a configuration file that provides context information to the Spring framework. Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, validation and scheduling functions.
  • Spring DAO: The JDBC DAO abstraction layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of exception codes that need to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exceptions follow the general DAO exception hierarchy.
  • Spring ORM: The Spring framework inserts several ORM frameworks to provide ORM object relational tools, including JDO, Hibernate and iBatis SQL Map. All of these follow Spring's generic transaction and DAO exception hierarchy.
  • Spring Web module: The Web context module is built on the application context module, which provides the context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies processing multiple requests and binding request parameters to domain objects.
  • Spring MVC Framework: The MVC Framework is a full-featured MVC implementation for building Web applications. Through the policy interface, the MVC framework becomes highly configurable and contains a large number of view technologies, including JSP, Velocity, Tiles, iText and POI.

2. spring configuration bean s

How to inject pojo attributes:

  1. Attribute injection (set injection)
  2. Constructor Injection
  3. Plant injection (not commonly used)

Create an address class

package entity;

public class Address {

    private String province;
    private String city;
    private String county;
    private String street;

    public Address() {
    }
    // Constructor, spring needs to be injected through constructor
    public Address(String province, String city, String county, String street) {
    super();
    this.province = province;
    this.city = city;
    this.county = county;
    this.street = street;
    }

    public String getProvince() {
    return province;
    }

    // When the set method configures bean s, the attribute injection needs to be done
    public void setProvince(String province) {
    this.province = province;
    }

    public String getCity() {
    return city;
    }

    public void setCity(String city) {
    this.city = city;
    }

    public String getCounty() {
    return county;
    }

    public void setCounty(String county) {
    this.county = county;
    }

    public String getStreet() {
    return street;
    }

    public void setStreet(String street) {
    this.street = street;
    }

    @Override
    public String toString() {
    return "Address [province=" + province + ", city=" + city + ", county=" + county + ", street=" + street + "]";
    }

}

spring configuration file 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" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Attribute injection -->
    <bean id="address1" class="entity.Address">
        <!-- name Corresponding Address Attributes in classes,value Assigning attributes -->
        <property name="province" value="Jiangsu Province" />
        <property name="city" value="Huaian City" />
        <property name="county" value="Xuyi County" />
        <property name="street" value="Hongwu Road" />
    </bean>
    <!-- Constructor Injection -->
    <bean id="address2" class="entity.Address">
        <constructor-arg value="Jiangsu Province" />
        <constructor-arg value="Nanjing City" />
        <constructor-arg value="yuhuatai district" />
        <constructor-arg value="Camellia Road" />
    </bean>
</beans>

Under test

package test;

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

public class Test {

    public static void main(String[] args) {
    // Obtain ioc containers
    ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
    // According to the application Context. XML configuration, get the bean with the corresponding id
    Address address1 = (Address) act.getBean("address1");
    Address address2 = (Address) act.getBean("address2");
    System.out.println(address1);
    System.out.println(address2);
    }
}

Result

Posted by shadowwebs on Wed, 27 Mar 2019 05:51:28 -0700