Getting Started with Spring study notes 1.1 -

Spring Framework

Development tools: IDEA Professional, JDK1.8 and above, Maven tools

Container core components:

Beans: Bean told it is an object of management, mainly includes the configuration of the relationship between objects, as well as some object instantiation operation;

Core: contains the lowest level of development support, such as: injection-dependent relationship, access to a resource file, the data type conversion;

Context: provides a complete container context, in this context can be processed lifecycle or affairs;

Expression Language module: use SpELl achieve operational expression language to enhance String functions.

Programming section:

AOP: Spring is the soul of the whole where, using the program to address all facets of assisted operations;

Aspect: syntax supports programming section;

Instrumentation: mainly used to detect dynamic processes running in the JVM code;

Data Access Module:

JDBC: among the Java, JDBC only for the operation of the database to an operational form, there is provided ORMapping Spring framework, which will use the native JDBC half completed;

The ORM: ORMapping frame processing operation can be easily combined: a common component JDO, Hibernate, iBatis MyBatis and the like;

OXM: it provides an object for mutual conversion between XML documents;

JMS: provides support for messaging services;

Transactions: In the data access module processing services to support operations;

Web support module:

MVC: Spring provides its own implementation of MVC (is to achieve the best kind);

Struts: Spring easily support Struts2.x management;

Servlet: Servlet own copy processing of MVC classes;

Hosted a Spring completed the entire project can be developed independently and can accommodate integrated to other frameworks.

Spring avoids the problems caused by the coupling of new keywords, Spring does not require explicit reference transfer relationship, directly through the configuration is complete, Spring programming = Factory Design Pattern + Proxy design pattern.

Inversion of Control IOC (Inversion Of Control):

Is a packaging technology type, such that all objects instantiated processing operation is no longer required new new keyword, the control means is inverted to reverse the created rights object to the Spring, the effect is to achieve a decoupling procedure ;

The principle underlying: factory design pattern reflection + + XML configuration file. For persistence DAO (data access object, data access object), to create an interface, and then create an interface corresponding implementation classes.

Create a project:

First, create a project with IDEA, as shown below:

Select Spring back support package on the line, and then there's Maven repository path, select the path that they have installed on the line.

As shown below to create resources folder, right click and select MakeAs-> Resources Root, and then create applicationContext.xml.

 

Enter the following code:

<?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">
    
</beans>

After you create the Service folder, create IService interface, providing a dosomething (); method, and then write a class that implements the interface IServiceImp:

@Override
public void dosomething() {
    System.out.println("This is My first Spring project");
}

 After you create a test class test, using ApplicationContext ctx object calls applicationContext.xml create documents, access Iservice implementation class,

Then call a method which, while adding bean applicationContext.xml file object, specify a class path

<bean id="IService" class="com.project.service.IServiceImpl"/>

demo:

import com.project.service.IService;
import com.project.service.IServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        IService service = ctx.getBean("IService", IServiceImpl.class);
        service.dosomething();
    }
}

operation result:

 

Dependency injection (DI): reference setting operation to determine the relationship between the data classes, and using the relationship of the configuration file.

  1. Injection constructor: operation of the automatic instantiation object is performed if the program is loaded Spring container disposed in the file can be automatically applicationContext.xml by default. However, when the automatic initialization is invoked constructor with no arguments class, by reflection if the class must provide a constructor with no arguments than there is provided a reference constructor easier.

But which simplifies the reflection Spring, Spring and dynamic characteristics may explicitly pass a parameter directly call the constructor.

Demo: define a class:

public class Detp {
    private int DetpId;
    private String DetpName;
    public Detp(int DetpId,String DetpName){
        this.DetpId = DetpId;
        this.DetpName = DetpName;
    }

    @Override
    public String toString() {
        return "部门编号: "+DetpId+"  部门名称: "+DetpName;
    }
}

By applicationContext.xml constructor parameter assignment configuration, using the index location specified parameters:

<bean id="dept" class="com.project.detp.Detp">
    <constructor-arg index="1" value="后端部"></constructor-arg>
    <constructor-arg index="0" value="1"></constructor-arg>
</bean>

Write test classes by loading the configuration file, locate the object id dept of output:

public class testDetp {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Detp detp = ctx.getBean("dept",Detp.class);
        System.out.println(detp);
    }
}

Enter the result:

Use setter injection 

By setting property setter & getter class method

In conventional operation must be first class instantiation Dept, invoking the setter of setting, using a Spring dynamic content provided by property settings for each attribute value corresponding to the applicationContext.xml

<bean id="dept" class="com.project.detp.Detp">
    <property name="detpId" value="1"/>
    <property name="detpName" value="开发部"/>
</bean>

    Use setter injection powerful is that you can refer to other types of objects Bean.

    Writing staff category, set three properties, setter & getter:

public class Employee {
    private int emp_id;
    private String emp_name;
    private Detp detp;
    public int getEmp_id() {
        return emp_id;
    }
    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }
    public String getEmp_name() {
        return emp_name;
    }
    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }
    public Detp getDetp() {
        return detp;
    }
    public void setDetp(Detp detp) {
        this.detp = detp;
    }
    @Override
    public String toString() {
        return "雇员编号: "+this.emp_id+",员工姓名: "+this.emp_name+",所属部门:"+this.detp;

    }
}

    The operator then determine the relationship between the properties through a configuration file:

<bean id="emp" class="com.project.detp.Employee">
    <property name="emp_id" value="15801"/>
    <property name="emp_name" value="Vlad Craste"/>
    <!--ref引用其他Bean对象的内容-->
    <property name="detp" ref="dept"/>
</bean>

Staff write the test class:

public class TestEmp {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Employee employee = ctx.getBean("emp",Employee.class);
        System.out.println(employee);
    }
}

Click to run the test class:

 

Published 58 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37504771/article/details/103115063