Spring to build analysis

spring Introduction

Spring is a lightweight in 2003, the rise of Java development framework, founder Rod Johnson, it was meant to address the complexity of enterprise application development created, with the development of the times, spring's developed a lot of projects, and more and more developers to start using these items. Spring advantages as follows:

  • Decoupling convenience, ease of maintenance development (low coupling high cohesion), objects can be handed Spring dependencies management.
  • IOC (Inversion of Control) control inverted, creating objects is done by spring, and the created object is injected to the user.
  • AOP (Aspect Orient Programming) programming support Oriented Programming may be some log, and other transaction operations are extracted from the business logic code out of the business logic code like this the more pure, and can enhance the reusability of the transaction log and .
  • Declarative transaction support, just by configuration management to complete the transaction without the need for manual programming.
  • Easy integration of a variety of excellent framework, which provides a lot of good internal framework: direct support (such as Struts, Hibernate, MyBatis, etc.).
  • Non-invasive, does not appear in the spring of api business logic code, if the project does not use spring day, then can be easily ported to other frameworks.

The first spring program

Add dependent jar package
we use here in the spring 5.x version, the version you want to use, then you need to ensure that the jdk is 8 or more.

To use spring framework, then you need to add the relevant jar package, add the following to rely on your pom.xml file:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.4.RELEASE</version>
</dependency>

After adding the above dependence, maven will spring a series of related jar package to your local maven repository, junit here will be used, so do not delete junit dependency.

Create the interface and implementation class

Create an interface and the interface implementation class:

package com.monkey1024.service;

public interface StudentService {

    void study();
}

 

Implementing Classes:

package com.monkey1024.service.impl;

import com.monkey1024.service.StudentService;

public class StudentServiceImpl implements StudentService {

    @Override
    public void study() {
        System.out.println("好好学习天天向上");
    }
}

 

Add the spring configuration file
to add the spring configuration file in the resources directory maven project, the file name can be named, here named: applicationContext.xml
inside need to add some xsd, copy the following can be.

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="studentService" class="com.monkey1024.service.impl.StudentServiceImpl"/>

</beans>

Add bean label in the spring configuration file:

  • id: This attribute is a unique identifier of Bean, java program by id access to the Bean.
  • class: Specifies a class that Bean belongs here can only be a class, not an interface.

Create a test class to test
you want to use StudentService words, developers need to manually objects in the interface implementation class is created by the new keyword. Although the interface can be decoupled programs, but in fact in the code there are new StudentServiceImpl statement, this place is still some coupling.

After using the spring, object code acquired by StudentServiceImpl spring, this way to remove the previous code coupled.

package com.monkey1024.test;

import com.monkey1024.service.StudentService;
import com.monkey1024.service.impl.StudentServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {

    /**
     * 以前的写法:手动创建对象
     */
    @Test
    public void oldType(){
        StudentService studentService = new StudentServiceImpl();
        studentService.study();
    }

    /**
     * After use spring wording: spring directly obtained by the object 
     * / 
    @Test 
    public  void springType () { 
        the ApplicationContext context = new new the ClassPathXmlApplicationContext ( "the applicationContext.xml" );
         // get the object from the spring 
        StudentService studentService = (StudentService) context. the getBean ( "studentService" ); 
        studentService.study (); 
    } 
}

 

ioc Profile

Inversion of Control (IoC, Inversion of Control), is an idea. Refers to the right to create objects of operating a container (eg spring), created to assemble and manage objects through the container, the inversion of control is actually a reversal, control over these objects control program by itself to reverse the outer container.

ioc is an idea, there are some implementations, one of the more commonly used one is the dependency injection (Dependency Injection, referred to as DI) , dependency injection means the program is running, if you need to call another object assistance, without code created by the caller, but depends on the exterior of the container, passed to the program created by the outer container, dependency injection in a manner so that the profile of the organization together, coupled together rather than hard-coded way between the Spring Bean.
In the above procedure it is actually used dependency injection obtained in this way StudentServiceImpl object.

ioc Another implementation is dependent lookup, (the Lookup Dependency, DL) , not discussed here.

 

ApplicationContext interface
in the program interface to get us through the ApplicationContext objects spring to inject to the program, here ApplicationContext acts as a spring IOC container role, ApplicationContext using the reflection mode of creation bean object, and which register after reading the profile bean all to create the object. We can create a container class that implements the two interfaces:

  • Class Path Xml Application Context

If the spring configuration file in the class path of the project, can be used to create a container class

  • File System Xm lApplication Context

If the profile is not in spring class path can be used to create the container

BeanFactory interface
, we can also use the BeanFactory interface acts as a spring IOC container role, BeanFactory ApplicationContext is the parent interface . And ApplicationContext different is the BeanFactory will not create objects inside the bean after reading the configuration file, but will create when in use.

@Test
public void springType2(){

    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
    reader.loadBeanDefinitions(new FileSystemResource("F:\\monkey1024\\ssm\\learnspring\\src\\main\\resources\\applicationContext.xml"));
    //当使用该bean的时候才会创建其对象
    //StudentService studentService = (StudentService)factory.getBean("studentService");
    //studentService.study();
}

With ApplicationContext or BeanFactory?
ApplicationContext provides more functionality than BeanFactory, in practical work, except in special circumstances, for example, you need to start the program memory or some small resource-constrained, this time you can choose to use BeanFactory, this time managing transactions, AOP feature They will fail. Therefore, in most cases, we recommend the use of ApplicationContext as a container.

 

Guess you like

Origin www.cnblogs.com/lucky1024/p/11136530.html