Spring framework learning (2) IOC understanding

Content from: IOC Understanding

 

Coupling, expressed in java as the relationship between classes, strong coupling means strong dependency between classes;

Intrusive: the intrusion of the framework into the code;

It is highly coupled and intrusive in traditional java development. In a project, a class generally depends on many other classes to complete its own operations. We often use the object of the new class to call its methods, which causes the dependency between the two classes to be too strong. Change one place. , often involving many classes and a lot of code. It can be said that in this context, spring came into being, a lightweight framework to solve the complexity of traditional enterprise development; using ordinary javaBeans instead of EJB technology. It can manage the dependencies between objects and objects. We do not need to build objects by ourselves, and transfer this part of the work to the container for completion. It is a framework with low coupling, no aggressiveness to code, and no dependence on servers. 
And this container, the IOC.

Understand the IOC idea

The purpose of using ioc to create objects is to form dependencies between objects in a "passive" way. In the traditional development process, whether it is a new or an ordinary factory, the target object needs to actively create and actively find the dependent objects it needs. The target object will scatter his energy on unnecessary non-business logic. IOC injects the created object into the target object through DI (dependency injection).

How exactly is Spring IOC implemented?

The key points of the spring IOC container: 
* The managed object must be defined in the spring configuration file 
* The constructor or setter method must be defined to let spring inject the object 

1. Configure applicationContext.xml

<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
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    <bean id="userDao4MySqlImpl" class="com.bjsxt.spring.dao.UserDao4MySqlImpl"/>
    <bean id="userDao4OracleImpl" class="com.bjsxt.spring.dao.UserDao4OracleImpl"/>
    <bean id="userManager" class="com.bjsxt.spring.manager.UserManagerImpl">
        <!-- 构造方法注入 
        <constructor-arg ref="userDao4OracleImpl"/>
         -->
         <!-- setter方法注入 -->
         <property name="userDao" ref="userDao4OracleImpl"/>
    </bean>
</beans>

2. The injected class:

package com.bjsxt.spring.dao;

public interface UserDao {

    public void save(String username, String password);
}
package com.bjsxt.spring.dao;

public class UserDao4MySqlImpl implements UserDao {

    public void save(String username, String password) {
        System.out.println("--------UserDao4MySqlImpl.save()-------");
    }
}
package com.bjsxt.spring.dao;

public class UserDao4OracleImpl implements UserDao {

    public void save(String username, String password) {
        System.out.println("--------UserDao4OracleImpl.save()-------");
    }
}

 3. The injected class:

package com.bjsxt.spring.manager;

public interface UserManager {
    public void save(String username, String password);
}
package com.bjsxt.spring.manager;

import com.bjsxt.spring.dao.UserDao;

public class UserManagerImpl implements UserManager {
    /**
     * Two ways: If you need to inject objects in this class, first create object properties,
     * When writing a constructor or a setter method.
     *
     */
    private UserDao userDao;

/*  public UserManagerImpl(UserDao userDao) {
        this.userDao = userDao;
    } */

    public void save(String username, String password) {
        this.userDao.save(username, password);
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

4. Test class:

package com.bjsxt.spring.client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.spring.manager.UserManager;

public  class Customer {

    public  static  void main(String[] args) {
 /*   The traditional relationship between classes is established through new objects
 * UserManager userManager = new UserManagerImpl(new UserDao4OracleImpl());
        UserManager userManager = new UserManagerImpl(new UserDao4MySqlImpl());
        userManager.save("张三", "123");*/
/**
 * The IOC idea parses the xml file through the factory class and creates objects in a "reflection" way:
 */
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserManager userManager = (UserManager)factory.getBean("userManager");
        userManager.save("张三", "123");
/**
 * The actual execution process of the IOC idea, which is why setter methods or constructors are needed:        
 */
//      UserManagerImpl userManager = new UserManagerImpl();
//      userManager.setUserDao(new UserDao4MySqlImpl());
//      userManager.save("张三", "123");
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126547&siteId=291194637