spring Spring basis of the IOC (b)

  IOC is a container, in the spring, it will consider all java resources are javaBean, the target container is to manage the relationship between them and the Bean, Spring IOC loaded inside a variety of Bean, it can be understood as a variety of Java resources, including the creation javaBean, events, actions, etc., they are managed by the container IOC. In addition, between the various javaBean certainly there is a certain relationship, such as the class is dependent on the teacher and students, assuming the teacher, students are javaBean, they apparently form a dependency, teachers and students and education relationship is education, which can be used SpringIOC container management. Just SpringIOC managed object and its dependencies, not artificially created using the initiative, but by SpringIOC description created by themselves. That is to rely on Spring describe the task of creating objects and dependencies.

  Now we can give a simple example to describe this problem, such as a user purchases a Snapdragon 855 phone, go to the phone shop and asked the salesman said I want to buy a Snapdragon 855 phone, you help me to recommend it At this time there are two phones to choose from, plus a millet 7 and 9, specifically which one to buy it? code show as below,

= The User the User new new the User ();
  // bought a millet phone 
Phone Phone = new new Xiaomi (); 
user.setPhone (Phone); 
user.usePhone ();

Buy millet phone, the mobile phone users, and millet produced a bundle plate, so there will be a downside: If the user for the other mobile phone brands, users they have to re setPhone (mobile phone brand) ;. Such an interface and its implementation class Phone XiaoMi the coupling, if not with millet day, but use a plus, then the code should be modified to

= The User the User new new the User ();
  // bought a cell phone plus a 
Phone Phone = new new OnePlus (); 
user.setPhone (Phone); 
user.usePhone ();

What if other better 855 phone, is it or to modify the code? If the system is a big must constantly modified, its reliability will be a great challenge, but SpringIOC can solve this problem Winking smile.

  Use SpringIOC, we need new ways to create an object, but use configuration, and then let SpringIoc container to find their own configuration by configuring their own.

   <bean id="xiaomi" class="XiaoMi"/>
   <bean id="onePlus" class="OnePlus"/>
   <bean id="user" class="User">
        <property name="phone" ref="xiaomi"/>
   </bean>
   <!--如果要买一加手机,只需要将ref改为onePlus-->
   <property name="phone" ref="onePlus"/>


Summary: do not need to look for resources, as long as want SpringIoc container description of the required resources, SpringIoc he would find the resources you need, this is the concept of SpringIoc. This put the dependencies between decoupled Bean, clear structure is easier to write programs. In addition, SpringIOC also provides life-cycle management of JavaBean, you can delay loading, you can define some of the acts in the life cycle, easier and more efficient management and use of java resources.

Guess you like

Origin www.cnblogs.com/jamal/p/10961092.html