Java Framework Spring Dependency Injection

Source: Internet
Author: User

One: Introduction

Scenario: When we layered the program: The Web layer, the business layer, the persistence layer, there will be dependencies between each layer. For example: The business layer and the persistence layer, the code of the business layer when calling the persistence layer, the traditional way: New persistence layer class.

In order to make the call, this will lead to a high degree of coupling, and the other layer will need to change the code when modifying one layer of code. Not conducive to maintenance. This relationship is called "dependency".

How to solve?

Solve:

Spring provides us with dependency injection, which means that when an object of a class is injected, the class that accompanies him is injected.

Code:

1) Traditional way:

1 Package Jd.com.service;2 3 import Jd.com.dao.UserDaoImpl;4 import org.junit.Test;5 6  Public classUserserviceimpl implements UserService {7 @Test8 @Override9      Public voidSave () {TenSystem. out. println ("The business layer invokes the persistence layer. "); One         //Traditional Way AUserdaoimpl userdao=NewUserdaoimpl (); - Userdao.save (); -     } the}

This approach results in a high degree of coupling between each layer.

2) Set mode dependent injection (Class):

Set mode: The dependent class needs to be set to a field, and a set method is provided.

Private  Userdaoimpl Userdao;

public void Setuserdao (Userdaoimpl Userdao) {
This.userdao = Userdao;
}

To be relied upon to:

1 Package Jd.com.dao; 2 3  Public class Userdaoimpl  implements userservice{4    @Override5public       void  Save () {6         System.  out. println (" persistence layer saves data. "); 7     }8 }

Class to Invoke:

1 Package Jd.com.service;2 3 import Jd.com.dao.UserDaoImpl;4 import org.junit.Test;5 6  Public classUserserviceimpl implements UserService {7     //Set Mode8 9     PrivateUserdaoimpl Userdao;Ten  One      Public voidSetuserdao (Userdaoimpl Userdao) { A          This. Userdao =Userdao; -     } -  the      Public voidSave () { -System. out. println ("The business layer invokes the persistence layer. "); - Userdao.save (); -     } +}

Test class:

1 Package Jd.com.service;2 3 import org.junit.Test;4 import Org.springframework.context.ApplicationContext;5 import Org.springframework.context.support.ClassPathXmlApplicationContext;6 7  Public classTestdemo {8 @Test9      Public  voidTestdemo () {TenApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); OneUserService userservice= (userservice) Ac.getbean ("Userserv"); A Userservice.save (); -  -     } the}

Configuration file configuration:

1<bean id="DAO"  class="Jd.com.dao.UserDaoImpl"/>2<!--If the number of classes is required to use a ref value for the ID name of the class that is dependent---3<bean id="Userserv" class="Jd.com.service.UserServiceImpl">4<property name="Userdao" ref="DAO"/>5</bean>

3) Set method (set field):

field injection is similar to the above but this way the configuration file is not the same.

Configuration file:

1<bean id="Userserv" class="Jd.com.service.UserServiceImpl">2<property name="Userdao" ref="DAO"/>3<!--name is the attribute key value is the property value-->4<property name="OOP"Value="Java"/>5</bean>

Code: Set Method Required

1      Public     String oop; 2 3      Public void Setoop (String oop) {4         this. oop = oop; 5     }
1 @Test2      Public voidTest () {3ApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml");4Userserviceimpl userservice= (Userserviceimpl) Ac.getbean ("Userserv");5System. out. println (userservice.oop);6}

4) Construction Method Dependency Injection:

Dependency classes: Property settings are made in the constructor method.

1 Package Jd.com.service;2 3 import Jd.com.dao.UserDaoImpl;4 5 6  Public classUserserviceimpl implements UserService {7 8 9      PublicString Ko;Ten      PublicString OK; One     PrivateUserdaoimpl Userdao; A  -  -      PublicUserserviceimpl (Userdaoimpl userdao, String ko, string ok) { the          This. userdao=Userdao; -          This. ko=Ko; -          This. ok=OK; -     } +  -  +  A      Public voidSave () { atSystem. out. println ("The business layer invokes the persistence layer. "); - Userdao.save (); -     } -}

Test code:

1 @Test2      Public  voidtest2 () {3ApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml");4Userserviceimpl us= (Userserviceimpl) Ac.getbean ("Userserv");5 Us.save ();6System. out. println (Us.ok);7System. out. println (Us.ko);8     }9}

Profile configuration: Note that the tags are: 1, constructor-arg 2, index, and name cannot be mixed. 3, attributes are other classes need to be aware of the use of ref= "class profile ID"

1<bean id="Userserv" class="Jd.com.service.UserServiceImpl">2<constructor-arg name="Userdao" ref="DAO"/>3<constructor-arg name="Ko"Value="python"/>4<constructor-arg name="OK"Value="Java"/>5</bean>6<bean id="DAO"  class="Jd.com.dao.UserDaoImpl"/>

Java Framework Spring Dependency Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.