Spring concept and simple use of spring program

  • Spring is an open source and free framework (container)
  • Spring is a lightweight, non intrusive framework
  • Inversion of control (IOC), facing section (AOP)

Bottom line: Spring is a lightweight control inversion and aspect oriented framework.

IOC essence:

Control inversion is a design idea, and di (dependencies in dependency injection) is a method to realize IOC.
In programs without IOC, we use object-oriented programming. The creation of objects and the dependencies between objects are completely hard coded. In programs, the creation of objects is controlled by the program itself. After the control is reversed, the creation of objects is transferred to a third party. The so-called control reversal is that the way to obtain dependent objects is reversed.
When configuring in xml, the definition information and implementation of Beans are separated, and annotation can combine the two. The definition information of Beans is directly defined in the implementation class in the form of annotation, so as to achieve the purpose of zero configuration.

Inversion of control is a way to produce or obtain specific objects through description (xml or annotation) and through a third party. In Spring, the IOC container implements control inversion, and its implementation method is dependency injection.

Spring is easy to use:
Add Spring dependencies first:

<!--    Spring rely on-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->


There are Dao layer and business layer as follows:
Both UserDaoImpl and UserMysqlImpl implement the UserDao interface

package com.tt.dao;

public interface UserDao {
    void getUser();

Business layer interface:

package com.tt.service;

public interface UserService {
    void getUser();

Business layer implementation:
Write Beansxml in the resources directory:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

    <bean id="userDaoImpl" class="com.tt.dao.UserDaoImpl"/>
    <bean id="mysqlImpl" class="com.tt.dao.UserMysqlImpl"/>

    <bean id="userService" class="com.tt.service.UserServiceImpl">
                  ref: quote Spring Created object in container
                  value:Specific value, basic data type
        <property name="userDao" ref="mysqlImpl"/>


The ID in each tag corresponds to the instantiation object of the implementation class, and class is the corresponding entity class path.
In the property attribute of the tag of the business layer, there are two attributes: name and ref. the former refers to the interface of the implementation class. The latter specifies the class to be implemented.


To use the Spring container, you must first get the ApplicationContext object:

public class MyTest {
    public static void main(String[] args) {
        //Get the ApplicationContext object and get the Spring container
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Beans.xml");
        UserServiceImpl userService = (UserServiceImpl)applicationContext.getBean("userService");

After you get the context object, you can get what you need directly from it.
Here, get the userServiceImpl object in the container, execute the getUser object in the container, and query the results

Tags: Java Spring

Posted on Sat, 20 Nov 2021 03:52:51 -0500 by DedMousie