No. 2 December learning content

First, the emergence of the historical background of Spring

1.1 Why Spring?

Spring did not come out before, is the EJB era, what EJB is, I have not experienced, in a nutshell is a heavyweight box
frame. Rod Johnson edited in 2002 "Expert One ON One the J2EE Design and
Development", a book that criticized the system when the Java EE framework, said its bloated, inefficient, from the current
reality, the same year launched the "Expert one-on-one J2EE Development without EJB ", for
a variety of heavy bloated structure of EJB were analyzed one by one and negative, respectively, in a concise and practical way to replace it, Humphreys
said classic.
Since 2003, elaborated by the "Expert one on one J2EE design and development" in
a lightweight Java development framework concept and prototype part derived from -Spring will rise; its purpose is to degeneracy
of JavaEE enterprise application development , which after several versions of the change.

A brief history of the development of 1.2Spring

The first stage: XML configuration
in Spring1.x age, are using XML configuration Bean, as the project expanded, we have an XML file into multiple
configuration files, then switch between development classes and configuration files frequently
first two stages: configuration notes
in Spring2.x era, brought with JDK1.5 annotation support, Spring provides a statement Bean annotations (such as
@Service etc.), greatly reducing the amount of configuration. At this time there is a debate Spring circle: annotations and XML configuration
which is better configured exactly? Our final choice is the basic configuration of the application using XML, service configuration with annotations.
The third stage: Java configuration
from Spring3.x start, Spring provides the ability to configure JAVA. Spring4.x and Spring Boot have pushed
the recommended use of JAVA configuration. The use of JAVA configuration can make you more understanding you configured Bean

Two, spring Overview

Spring is a layered Java SE / EE lightweight open source application framework for full-stack to the IOC (Inverse Of
Control: Inversion of Control) and AOP (Aspect Oriented Programming: Oriented Programming) as a core, provide
for the display layer Spring MVC and Spring JDBC persistence layer and business layer transaction management and other enterprise
-class application technology, but also the integration of the open source world many well-known third-party frameworks and libraries, has become the most use
of open-source Java EE enterprise application framework.

Three, spring strengths

Decoupling convenient simplify the development
IOC containers provided by Spring, dependencies between objects may be referred Spring controlled to avoid excessive coupling hardcoded program caused. Users no longer have singleton class attributes file analyzing these and other needs of the underlying code is written, can concentrate on the upper application.
AOP programming support
through Spring's AOP functionality, facilitate aspect-oriented programming, many are not easy to achieve using traditional OOP functionality can easily cope by AOP.
Support for declarative transactions
can be freed from our monotonous boredom of transaction management code, to manage transactions through a declarative way and flexible, improve development efficiency and quality.
To facilitate the testing procedure
can be carried out using non-container-dependent programmatically almost all testing, testing no longer is an expensive operation, but readily do things.
Easy integration of a variety of excellent framework
Spring can reduce the difficulty of using various frameworks, providing direct support for a variety of excellent frameworks (Struts, Hibernate, Hessian, Quartz, etc.).
JavaEE-API is used to reduce the difficulty of
Spring's JavaEE-API (such as JDBC, JavaMail, remote calls, etc.) a thin encapsulating layer, the difficulty of making use of the API greatly reduced.
Java source code is a classic example of learning
Spring source code design exquisite, clear structure, with ingenuity alone, always embodies the masters of the flexible use of Java design patterns and profound knowledge of Java technology. Its source code is not intended to be examples of best practice in Java technology.

Four, IOC DI inversion of control and dependency injection

Simply put, the original by our instantiated objects, handed over to spring framework to instantiate This is an example of the right to object would be reversed..
IOC's idea is: a high degree of substitution coupled with the way the frame form new constructor () to create objects so you can go when you need to create, destroy when not needed, unified management, resource conservation, and improve efficiency.
DI idea is: Spring Framework operational attributes for us to complete the assignment.

Five, Spring's IOC

5.1 What is the IOC?

IOC: called inversion control. The process to create a series of life-cycle of an object (single cases of multiple cases of non-lazy lazy loading load), initialization, destruction of property, such as the assignment to the Spring container to manage.

5.2 Glossary

1, ClassPathXmlApplicationContext: it is loading a configuration file from the root class
2, bean Tags:
Role: to let spring configuration object to create.
The default is the class constructor without parameters it calls. If there is no no-argument constructor can not be created.

5.3 Creating Spring Steps

1. The introduction of coordinates

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

2. Create a spring core files: applicationContext.xml
3. Create a bean tag
4. Load applicationContext.xml core file

Single and multi 5.4spring Example Example

<!--
	bean标签:将指定类对象的创建过程交给Spring容器来管理
	class属性:指定类的具体路径
	id属性:为bean标签去唯一标识符
	scope属性:指定Spring创建对象时的方式,singleton单例、prototype多例,默认为单例
-->
<bean id="smallDog" class="cn.java.iocl.Dog" scope="prototype"></bean>

5.5spring in lazy loading with non-lazy loading

Non-lazy loading (ground load, the Spring default): When you start Spring container, regardless of whether the object immediately create
all the objects.
Lazy Load: etc. When used, let the Spring container to create
advantages and disadvantages:
(1), lazy loading more save memory space, non-lazy loading consume more memory space.
(2) non-lazy loading errors are found early, lazy loading errors will be hidden.

Six, spring of DI

Definition: dependency injection. Assigns them the object


<!--主要有两种方式
	1.通过set方法
-->
<!--admin-->
  <bean id="admin" class="cn.kgc.domain.Admin"></bean>
  <!--通过管理setXXX属性的方式-->
  <bean id="db" class="cn.kgc.domain.DB">
    <!--DI:依赖注入-->
    <!--
    property标签:表示注入的属性
    name:表示实体中的属性名
    value :表示给实体中的属性注入的值
    spring的依赖注入:就是去调用了javabean的setXXX方法。==
javabean的规范
    ref属性:ref="bean标签的id属性名":引入其他的bean标签的映射
    -->
    <!--注入String类型-->
    <property name="driver" value="com.mysql.jdbc.Driver">
</property>
    <!--注入Integer类型-->
    <property name="port" value="3306"></property>
    <!--注入实体类-->
    <property name="admin" ref="admin"></property>
    <!--注入list集合-->
    <property name="list">
       <list>
        <value>list集合</value>
        <value>true</value>
        <value>3.14</value>
        <!--引入实体类-->
        <ref bean="admin"></ref>
      </list>
    </property>
    <!--注入set集合-->
    <property name="set">
      <set>
        <value>set集合</value>
        <value>false</value>
        <value>3.15</value>
        <ref bean="admin"></ref>
      </set>
    </property>
    <!--注入map集合-->
    <property name="map">
      <map>
        <entry key="name" value="郭德纲"></entry>
        <entry key="gender" value=""></entry>
      </map>
    </property>
    <!--注入properties属性-->
    <property name="properties">
      <props>
        <prop
key="drivername">com.mysql.jdbc.Driver</prop>
        <prop key="url">jdbc:mysql:///person_info</prop>
        <prop key="username">root</prop>
        <prop key="userpassword">sa123</prop>
      </props>
    </property>
  </bean>
  <!--
	特点:通过set方法赋值时,首先通过无参构造方法创建对象,然后通过属性对应的set方法给对象赋值
	2.通过有参构造器
	-->
	<!--
  constructor-arg标签:一个标签只能描述一个有参构造方法中的形参
    index属性:有参构造器中的参数下标
    name属性:形参属性名
七、常见面试题
    type属性:形参属性类型
    value属性:形参属性的值
-->
<!--通过管理构造设置属性的方式-->
  <!--再重新创建一个DB类的对象-->
  <bean id="smallDB" class="cn.kgc.domain.DB">
    <constructor-arg index="0" name="driver" value="com.mysql.jdbc.Driver" type="java.lang.String">
</constructor-arg>
    <constructor-arg index="1" name="port" value="3306"
type="java.lang.Integer"></constructor-arg>
    <constructor-arg index="2" name="price" value="3.14"
type="java.lang.Float"></constructor-arg>
    <constructor-arg index="3" name="admin" ref="admin"
type="cn.kgc.domain.Admin"></constructor-arg>
  </bean>
  <!--特点:通过有参构造方法给对象的属性赋值时,首先调用有参构造方法创建对象,然后通过
同一个有参构造方法给对象的属性赋值  -->
Published 28 original articles · won praise 16 · views 590

Guess you like

Origin blog.csdn.net/qq_37881565/article/details/103357219