spring的IoC(Inversion of Control)2

The first part: Spring's IoC (Inversion of Control)
is a metaphorical image below IoC, that is, decoupling.
Insert picture description here
IoCIt is Springthe core content of the framework. It is implemented in a variety of ways IoC. You can use XMLconfiguration or annotations. In Springthe inversion control is implementation dependent injection ( Dependency Injection, DI).
So why not come and experience it

1. XMLRealizeIoC

Path:
Insert picture description here
Hello.javaContent:

package com.weizu;

public class Hello {
    
    
    private String name;

    public Hello() {
    
    
    }

    public String getName() {
    
    
        return name;
    }

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

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

Then define the beans.xmlcontent:

<?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="hello" class="com.weizu.Hello">
        <property name="name" value="Xiao wang."/>
    </bean>

</beans>

Finally, use the test:

import com.weizu.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {
    
    

    @Test
    public void Test(){
    
    
        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.getName());
    }
}

Test result:
Insert picture description here
Corresponding official document: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-instantiation
Above we have learned how to use XMLto load Beanobjects, then we can be the last article of improvement, with Springfor automatic hosting.
That is beans.xml, specify Daothe data source loaded by the layer in the file .

2. Case 2

Project structure
Insert picture description here
UserDao.java:

public interface UserDao {
    
    
    void getUser();
}

UserMySQLImpl .java:

public class UserMySQLImpl implements UserDao{
    
    
    public void getUser() {
    
    
        System.out.println("MySQL impl.");
    }
}

UserOracleImpl.java:

public class UserOracleImpl implements UserDao{
    
    
    public void getUser() {
    
    
        System.out.println("Oracle impl.");
    }
}

UserService.java:

public interface UserService {
    
    
    void getUserService();
}

UserServiceImpl.java

public class UserServiceImpl implements UserService{
    
    

    private UserDao userDao;

    // set注入
    public void setUserDao(UserDao dao){
    
    
        this.userDao = dao;
    }

    public void getUserService() {
    
    
        userDao.getUser();
    }
}

Then, we define the beans.xmlfile to define the daolayer data source that needs to be loaded :

<?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="mySqlDao" class="com.dao.UserMySQLImpl"/>
    <bean id="oracleDao" class="com.dao.UserOracleImpl"/>

    <bean id="userService" class="com.service.UserServiceImpl">
        <property name="UserDao" ref="mySqlDao"/>
    </bean>
</beans>

Then, test in the test:

public class myTest {
    
    

    @Test
    public void Test(){
    
    
        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.getUserService();
    }
}

Result:
Insert picture description here
That is, you can xmlperform simple data source object to specify required by the Springdecision how to load. No longer need to be used newto create objects, which can reduce the coupling degree of the code. Remember: ApplicationContextand ClassPathXmlApplicationContext.
Just modify the configuration file, without modifying the program.
Object of Springthe allocation and management come.


Video address: https://www.bilibili.com/video/BV1WE411d7Dv?p=5&spm_id_from=pageDriver

Guess you like

Origin blog.csdn.net/qq_26460841/article/details/115033703