Initial Spring Framework

                                        Spring-- first day

Introduction to the Spring Framework

1. Recognizes framework

 spring framework are included in the spring-framework-4.1.6.RELEASE-dist.jar package, decompress it to obtain three documents:

  1.   docs document (spring framework help documentation, including api and official documentation)   
  2. libs document (various jar package, consisting mainly of java framework)
  3. schema document (various .xsd file, .xsd file is a DTD (xml grammar checker) upgraded version, it is mainly used in spring framework configuration files (applicationcontext file))             

          3.1 xml grammar checker: for fast programming, give you tips completion etc.

          3.2 Scalability: Each time the introduction of a .xsd file, on the introduction of a xml namespace (xmlns), a little more concise than the DTD.

Know from official documentation docs in:

spring framework of the following components,

1. The bottom of the test, the test is provided with spring

2. Then Core Comtainer core container, referring to the basic framework composed of spring, that is, if we want to use spring framework must be introduced Beans, Core, Cntext, SpEl. These four jar package.

    Beans: object container.

    Core: spring core

    Context: context object

    SpEl: spring expression

The above two functions is its other, corresponds to a respective jar package, such as the need to use AOP techniques incorporated spring-aop-4.1.6.RELEASE.jar package

 Function and role 2.Spring framework

   spring's main features include:

  1. IOC / DI: Inversion of Control / Dependency Injection (Summary: is to use a container, we need to manage all objects instantiated)
  2. AOP: Aspect Oriented Programming (now in order to solve the problem of programming the longitudinal, lateral introduction of programming, prior to a process (cutting point) function needs to be implemented (pre-notification), the method needs to be implemented after the function (post-notification) so it simplifies the tedious extent of the main business of the business function code needs to do more of a turn, it is the agent of the underlying implementation design pattern, but a dynamic agent because the agent mode (star - brokers) also happens to make a star concentrate more on the show, without having to think about it in terms of life, aspects of travel problems)
  3. Declarative transaction: it is based on the notification, it changes the structure of some of the aop (notice ---> cut-off point) becomes a * (notice -> Services -> cut-off point), where the transaction is usually back to the database rolling submission. The data is inserted into the database, success is saved, failure to roll back, exactly in line with the lateral aop programming, so the spring framework to help us write the transaction processing module, we just need to declare on which method to call.

Framework to build 3.spring

         1. Import jar package (at least 5), in addition to the four core packages mentioned above, but also introducing a commons-logging-1.1.3.jar package log

         2. In the src New under 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
  <!-- id表示获取到对象标识
    	 class 创建哪个类的对象
     -->
    <bean id="peo" class="com.pojo.People"/>
</beans>

         3. Writing Test Methods

package com.test;

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

import com.pojo.People;


public class Test {
	public static void main(String[] args) {
//		People peo = new People();
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people = ac.getBean("peo",People.class);
		System.out.println(people);
		
//		String[] names = ac.getBeanDefinitionNames();
//		for (String string : names) {
//			System.out.println(string);
//		}
	}
}

4.IOC / DI 

Read various documents, post, however ever more abstract, personal feel, that is, between classes plus a container, the container to create objects and management class. This container is achieved by decoupling. Decoupling between the programmer and the classes. When the container is not, a class object reference another class, need new to instantiate an object of another class. With it We then create a configuration file applicationcontext file in the container object when a class reference to another class of objects, we do not need to instantiate an object, just to get an object from the container.

5.spring create objects in three ways

        1. Create via the constructor

              No-argument constructor

             There arg constructor

        2. Examples of plant

            

        3. Static Factory

5. Bean property assignment

      1. constructor setter

      2. The set value of the injected (through class attribute set method)

6.IOC 和 IN

    IOC and DI is the same.

    IOC: Inversion of Control refers to the original programmer to write code to create and manage objects task to Bean container

    DI: dependency injection, when an object needs to refer to a class of another class, the subject of another class assignment process to the A

          

 

 

Published 14 original articles · won praise 8 · views 4731

Guess you like

Origin blog.csdn.net/qq_41223538/article/details/103948137