Introduction to the Spring Framework Learning IOC

IOC(Inversion of Control, Control Reverse)

IOC also has an individual name, Dependency Injection
Role: Dependent classes in a class do not need to go to new themselves, and are created and managed by the IOC container.

I. Before IOC

The FXNewsProvider class needs to instantiate two dependent classes (IFXNewsListener, IFXNewsPersister) before it can call the getAndPersisterNews method. Previously, it was instantiated in the constructor of the class. If you use Dow Jones news, provide the corresponding implementations of Dow Jones NewsListener and Dow Jones NewsPersister, as follows:

public class FXNewsProvider{
   private IFXNewsListener newsListener;
   private IFXNewsPersister newPersister;
   //Before Ioc, dependent classes were created and managed by themselves
   public FXNewsProvider(){
      newsListener = new DowJonesNewsListener();
      newPersister = new DowJonesNewsPersister();
   }
   public void getAndPersistertNews(){
       String[] newsIds = newListener.getAvailableNewsIds();
       if(ArrayUtils.isEmpty(newsIds){
          return;
       }
       for(String newsId:newsIds){
           FXNewsBean newsBean = newsListener.getNewsByPK(newsId);
           newPersistener.persistNews(newsBean);
           newsListener.postProceeIfNexessary(newsId);
       }
   }
}

2. Construction Method Injection

Injected objects can declare parameter lists of dependent objects through their construction methods

public FXNewsProvider(IFXNewsListener newsListener, IFXNewsPersister newsPersister){
      this.newsListener = newsListener;
      this.newPersister = newsPersister;
   }

3. setter Method Injection

For JavaBean objects, properties are accessed through setXXX() and getXXX() methods, collectively referred to as setter methods, which allow the current object to inject the corresponding dependent object settings into the object as long as the setter method is added to the corresponding properties of its dependent object.

public class FXNewsProvider{
   private IFXNewsListener newsListener;
   private IFXNewsPersister newPersister;
   public IFXNewsListener getNewsListener(){
       return newsListener;
   }
   public void setNewsListener(IFXNewsListener newsListener){
       this.newsListener = newsListener;
   }
   public IFXNewsPersister getNewsPersister(){
       return newsPersister;
   }
   public void setNewsPersister(IFXNewsPersister newsPersister){
       this.newsPersister = newsPersister;
   }
}

4. Interface Injection (Not Common)

IOC Service Provider Manages Dependencies Between Objects

Business objects can declare their dependencies through ioc, but ultimately they need to be bound together through a role or service.

Loc Service Provider has two responsibilities:

  • Building Management of Business Objects
  • Dependent binding between business objects

5. Annotation-based Dependent Injection (Key)

5.1 Autowired matches by type

@Autowired lets the container know which dependencies need to be injected into the current class. (private, protected, or public)

public class FXNewsProwider{
    @Autowired 
    private IFXNewsListner newsListener;
    @Autowired 
    private IFXNewsPersister newPersistener;
    ...
}

Procedure: Simply labeling @Autowired on the class definition does not make Spring's IOC container smart enough to complete dependent injection. This requires the container configuration file to tell the container where to find the corresponding dependency and complete the injection.
To inject dependencies into the instances defined by each bean defined by the container, iterate through them, and then through reflection, check @Autowired at various possible locations on the corresponding class for each bean definition. If so, get the corresponding object from the current container-managed object and set it to the one labeled by @Autowired. The process described in blue is used by Spring's IoC container.BeanPostProcessor custom implementations do not require us to implement them ourselves, such as:

<beans>
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <bean id="newsProvider"    class ="..FXNewsProvider"/>
    <bean id="djNewsListener"  class="..DowJonesNewListerner"/>
    <bean id="djNewsPersister" class="..DowJonesNewsPersister"/>
</beans>

5.2@ Qualifier matches by name

Currently, the dependency of the @Autowired label can only be found to correspond to one instance in the container, but if two or more instances of the same type of object occur, it cannot be resolved, and @Qualifier is the result.

<beans>
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <bean id="newsProvider"    class ="..FXNewsProvider"/>
    <bean id="djNewsListener"  class="..DowJonesNewListerner"/>
    <bean id="reutersNewsListner" class="..RetersNewsListenr"/>
    <bean id="djNewsPersister" class="..DowJonesNewsPersister"/>
</beans>
public class FXNewsProwider{
    @Autowired 
    @Qualifier("reutersNewsListner")
    private IFXNewsListner newsListener;
    @Autowired 
    private IFXNewsPersister newPersistener;
    ...
}

It can also be labeled on method parameters

public class FXNewsProvider{
    ...
    @Autowired
    public void setUp(@Qualifier("retuerNewsListner") IFXNewsListener newsListener){
         this.newsListener = newsListener;
    }
}

Tags: Java Spring

Posted on Mon, 20 Sep 2021 13:43:20 -0400 by widget