Spring road learning 1

Spring Framework

Brief introduction

Spring framework is due to the complexity of software development and the creation of

  • Objective: To address the complexity of enterprise application development

  • Function: instead of using the basic JavaBean EJB, and provides more enterprise applications

  • Range: Any Java application

advantage:

Free open source framework (container)

Spring is a lightweight, non-invasive framework

Inversion of Control (IOC) and the facing section (AOP) of the container frame

Supports transaction processing, support for the integration framework

Official website: https: //spring.io/

GitHub:https://github.com/spring-projects/spring-framework


Maven

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>

spring seven modules

IOC theory

IoC full name Inversion of Control, i.e., "Inversion of Control", one of the core of Spring, and get a description of the specific object (xml or annotation) by way of a third party, Spring inversion control is implemented IoC container implementation is the DI ( Dependency Injection), i.e., dependency injection.

  1. Who controls who : After the traditional pattern of development, we are the object of a new direct way to create an object, which means you lean on your own direct control, but with the IOC container, directly by the IoC container to control. So "Who controls who" is, of course IoC container control object.
  2. What controls : control object.
  3. Why is reversed : no IoC when we are dependent on the initiative to create the object in its own object, which is forward. But after With IoC, injected directly dependent objects created by the IoC container after being injected into the object, the original dependent objects get into the passive acceptance of the initiative, it is reversed.
  4. What reversed : the acquisition is dependent on the object reversed.

IoC create objects

Create objects by 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="he" class="com.youzi.pojo.Hello">
        <property name="Str" value="Hello Spring!"/>
    </bean>
</beans>
public class Hello {
    private String Str;

    public Hello(String str) {
        Str = str;
    }

    public Hello() {
    }

    public String getStr() {
        return Str;
    }

    public void setStr(String str) {
        Str = str;
    }
}
...
@Test
public void test() {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Object he = context.getBean("he");
    System.out.println(he);   
}

He cited the way the assignment of other objects:

<bean id="userservice" class="com.youzi.service.UserServiceImpl">
    <property name="user" ref="user"/>
</bean>

No initialization parameters directly

<bean id="user" class="com.youzi.dao.UserImpl"/>

Above manner and calls the constructor with no arguments class corresponding to setter, i.e., it requires initialization parameters or object must have a corresponding setter, configuration parameters may be used if there is to be invoked <constructor-arg>by the type, the standard, the parameter name, reference pass this in several ways ginseng

Use the constructor

public Users(Hello hello, String num) {
    this.hello = hello;
    this.num = num;
}
<bean id="users" class="com.youzi.pojo.Users">
    <!--<constructor-arg type="java.lang.String" value="1"/>-->
    <!--<constructor-arg index="1" value="2"/>-->
    <constructor-arg name="num" value="3"/>
    <constructor-arg ref="he"/>
</bean>

spring basic configuration

Alias, another name for the object is created, you can create multiple (aliases can also bean is name created)

<bean id="he" class="com.youzi.pojo.Hello" name="hello3,hello4">
    <property name="Str" value="Hello Spring!"/>
</bean>

<alias name="he" alias="hello1"/>
<alias name="he" alias="hello2"/>

bean objects default singleton created by scope="prototype"modifying, such hello3, hello4 that the two different objects

Other imported xml

The above are just the basic configuration, there are many details to be configured after further study each configuration inside


Guess you like

Origin www.cnblogs.com/wangjr1994/p/12514704.html