Brief Introduction to spring Framework

Spring is a layered JavaSE/EE full-stack (one-stop) lightweight open source framework. It was created to address the complexity of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows you to choose which component to use, while providing an integrated framework for J2EE application development.

Spring framework originated from:
Rod Johnson launched Expert One-to-One J2EE Design and Development in 2002

Expert One-to-One J2EE Development without EJB launched in 2004

Spring appeared, there are some problems to solve EJB, because EJB is heavy, complex, and Spring is lightweight.

Spring benefits:
Facilitate decoupling and simplify development
AOP
Declarative transaction management
Convenient program testing and integration of junit (built-in excellent test framework)
Spring can integrate almost all the excellent frameworks in the industry (struts 2, hibernate, mybatis)
Built-in a large number of tool classes to simplify application development, such as: JdbcTemplate, javamail, RMI

Spring Core:
IOC (Inverse of Control) Control Inversion
AOP (Aspect Oriented Programming) Face-Oriented Programming

IOC and AOP:
The basic concept of inversion of control is not to create objects, but to describe how they are created. It does not connect directly to objects and services in the code, but describes in the configuration file which components need which services. Containers (in the Spring framework, IOC containers) are responsible for linking these together.
AOP is a programming technology that allows programmers to modularize crosscutting concerns or behavior that crosscuts typical boundaries of responsibilities, such as logging and transaction management.

IOC and DI
The concept of IoC inversion control is to hand over the control of creating HelloService objects manually in the program to the Spring Framework. In short, the control of creating HelloService objects is reversed to the Spring Framework.
Object creation rights are reversed to containers
DI: Dependency Injection Dependency Injection, which dynamically injects dependent objects into Bean components when the Spring framework is responsible for creating Bean objects
When the container provides the object, it provides the dependent object to you together.

spring Initial demo:

Minimum jar packages developed by spring
spring-beans
spring-core
spring-context
spring-expression
Spring development must rely on commons-logging log packages (which can be used independently or integrated with jdk logs or log4j logs)
Integrate log4j to copy jar to lib and build log4j.properties in src

Code implementation:
UserService interface:

package com.my.service;

public interface UserService {

    // Landing method
    public void login();

    // Setting Name
    public void setUsername(String name);

}

UserService Impl implementation class:

package com.my.service;

public class UserServiceImpl implements UserService{

    // username is the UserService Impl that relies on an object 
    private String username; 

    public UserServiceImpl() {
        System.out.println("user service Constructed...");
    }

    @Override
    public void login() {
        System.out.println(username + " User login");
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

Configuration file, placed under src:
log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p = "http://www.springframework.org/schema/p"
       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">
    <!-- 1. quick get start -->
    <!-- Provide object creation rights -->
    <!-- id attribute bean Unique identification, class Create Object Complete Class Name  -->
    <bean id="userService" class="com.my.service.UserServiceImpl">
        <!-- When creating an object, automatically inject its dependency properties  -->
        <property name="username" value="testSpring"></property>
    </bean>

</beans>

Test files:

package com.my.service;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {

    //No spring container
    @Test
    public void demo1() {
        UserService service = new UserServiceImpl();
        // Setting User Name (Manually Setting Dependencies)
        service.setUsername("test");
        service.login();

    }

    //Managing UserService Implementation Objects with Spring Containers
    @Test
    public void demo2(){
        // 1. Initialization of Spring Container Objects 
        //If the Spring configuration file is read in src -- using ClassPath Xml Application Context
//If the Spring configuration file is in WEB-INF -- read using FileSystem Xml Application Context

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 2. Obtain the required Bean from the container <bean id="userService". >
        // applicationContext objects are similar to factory objects 
        // In Ioc control object process, according to attribute dependency, automatic injection is completed.
        UserService service = (UserService) applicationContext.getBean("userService") ;// Get the managed Bean object from the spring container

        // 3. Invoking Business
        service.login();
    }

    //Use BeanFactory to get container objects
    @Test
    public void demo3(){
        // 1. Initialization container
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

        // 2. Obtaining the Target Object
        UserService service = (UserService) beanFactory.getBean("userService");

        // 3. Invoking Business
        service.login();
    }


}

Spring container object
BeanFactory, the Bean Management Factory, is Spring's top container interface
The ApplicationContext interface is only a sub-interface of the BeanFactory interface (after extension)

Difference: Bean objects are declared in the BeanFactory delayed loading configuration file. Bean objects are created only when getBean is loaded. Application Context completes object creation and dependency injection when the configuration file is loaded.
ApplicationContext provides internationalization, event transfer, and automatic assembly. More Functions
When developing an enterprise, the application context interface is basically used.

Keywords: Spring log4j xml Apache

Added by thedon on Thu, 27 Jun 2019 00:50:58 +0300