Crazy God said Spring02: get started quickly hellosppring

Posted by cdherold on Mon, 07 Mar 2022 13:59:29 +0100

HelloSpring

1. Create spring-02-hellospring

2. Add Hello entity class

public class Hello {

    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

3. Write the spring configuration file beans 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">

<!--   use sring To create objects, in spring These are called Bean-->

<!--    Type variable name=new type();-->
<!--    HelLo hello = new HelLo();-->
<!--    id=Variable name cLass =new Object of;-->
<!--    property This is equivalent to setting a value for an attribute in an object! -->

    <bean id="hello" class="com.gx.pojo.Hello">
        <property name="str" value="Spring" />
    </bean>
</beans>

4. Testing

public class MyTest {

    public static void main(String[] args) {
        //Get the context object of spring
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //Our objects are now managed in spring. If we want to use them, just press the inside and take them out!
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
} 

Thinking?

Who created the Hello object?

  • The hello object is created by Spring

How are the properties of the Hello object set?

  • The properties of the hello object are set by the Spring container

This process is called control reversal

Control: who controls the creation of objects? The objects of traditional applications are created by the program itself. After using Spring, the objects are created by Spring

Inversion: the program itself does not create an object, but becomes a passive receiving object

Dependency injection: it uses the set method to inject

IOC is a programming idea, from active programming to passive reception

You can browse the underlying source code through newClassPathXmlApplicationContext

In the module spring-01-ioc1, add a configuration file beans 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="mysqlImpl" class="com.gx.dao.UserDaoMysqlImpl" />
    <bean id="oracleImpl" class="com.gx.dao.UserDaoOracleImpl" />

    <bean id="UserServiceImpl" class="com.gx.service.UserServiceImpl">
<!--      ref:quote Spring Created object in container
          value: Specific value, basic data type!
-->
        <property name="userDao" ref="mysqlImpl"/>
    </bean>
</beans>

test

public class MyTest {
    public static void main(String[] args) {

        //Get the ApplicationContext and get the Spring container
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

       //The container is in hand. I have it in the world. I can get what I need directly
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");

        userServiceImpl.getUser();
    }
}

OK, now, we don't need to change it in the program. To realize different operations, we only need to modify it in the xml configuration file. The so-called IOC is done in one sentence: objects are created, managed and assembled by Spring!

How IOC creates objects

New sub module spring-03-ioc2

Add User entity class

public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User() {
        System.out.println("User No parameter structure!");
    }

    public void show(){
        System.out.println("name"+name);
    }
}

Create a new configuration file beans xml

1. Use parameterless construction to create objects. Default!

2. Suppose we want to create an object using a parametric construct

  1. Subscript assignment
<!--  First, subscript assignment-->
   <bean id="user" class="com.gx.dao.User">
       <constructor-arg index="0" value="shen" />
   </bean>
  1. type
 <!--The second is created by type, which is not recommended-->
    <bean id="user" class="com.gx.dao.User">
        <constructor-arg type="java.lang.String" value="shen" />
    </bean>
  1. Parameter name
 <!--Third, it is set directly through the parameter name-->
    <bean id="user" class="com.gx.dao.User">
        <constructor-arg name="name" value="shen" />
    </bean>

Summary: when the configuration file is loaded, the objects managed in the container have been initialized!

Spring configuration

1. Alias

<!--    Alias. If an alias is added, we can also use the alias to get this object-->
    <alias name="user" alias="sdfglj" />

2. bean configuration

<!--    id : bean Unique identifier of the, That is equivalent to the object name we learn 
      class: bean The fully qualified name corresponding to the object: Package name+Class name 
      name :It's also an alias,and name Multiple aliases can be taken at the same time
-->
<bean id="userT" class="com.gx.dao.UserT" name="user2 u2,u3;u4">
     <property name="name" value="sezi" />
</bean>

3,import

This import is generally used for team development. It can import and merge multiple configuration files into one

Suppose that there are multiple developers in the project. These three people copy different classes for development. Different classes need to be registered in different beans. We can use import to import everyone's beans XML merged into a total!

  • Zhang San

  • Li Si

  • Wang Wu

applicationContext.xml

<import resource="beans.xml" />
<import resource="bean2.xml" />
<import resource="bean3.xml" />

When using, just use the general configuration directly

Topics: Java Spring intellij-idea