Crazy God said Spring02: get started quickly hellosppring

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


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;

    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +

3. Write the spring configuration file beans xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<!--   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" />

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");


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=""

    <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"/>


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");


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) { = name;

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

    public void show(){

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" />
  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" />
  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" />

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" />


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


<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