Spring Learning in the Java family--Dependency injection (II.)

Source: Internet
Author: User
Tags aop

One, three ways to rely on injection

interface injection, set injection, constructor injection

Ii. Constructor Injection 2.1, test class
Package Test;public class Test01 {public String msg=null;public test01 (String msg) {System.out.println (msg);} public void Prints () {System.out.println ("prints");}}
2.2. Edit Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-m Vc.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-co         Ntext.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd       Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--constructor injection--<bean id= "test01" class= "test.test01" > <constructor-arg index= "0" > <val Ue>dirk</value> </constructor-arg> <constructor-arg index= "1" > <value>dirk2</value> </constructor-arg> </bean> </beans>
2.3. Testing
Package Test;public class Test01 {public string Msg=null;public string Msg1=null;public test01 (String msg,string msg1) {Sy Stem.out.println (MSG+MSG1);} public void Prints () {System.out.println ("prints");}}
Package Test;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Aservlet extends HttpServlet { public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { ApplicationContext context=new classpathxmlapplicationcontext ("Applicationcontext.xml"); test02 test01= (test02) Context.getbean ("test02"); Test01.getmsg ();}}

Third, set injection   3.1 Test class
Package Test;public class test02 {public string Msg;public string getmsg () {return msg;} public void Setmsg (String msg) {this.msg = msg;}}
3.2. Configuration file Modification
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-m Vc.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-co         Ntext.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd         Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <bean id= "test02" class= "test.test02" > <property name= "msg" > <value>drik.wang</value&gt    ; </property> </bean></beanS> 
Package Test;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Aservlet extends HttpServlet { public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { ApplicationContext context=new classpathxmlapplicationcontext ("Applicationcontext.xml"); test02 test01= (test02) Context.getbean ("test02"); Test01.getmsg ();}}

3.3. Testing
Package Test;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Aservlet extends HttpServlet { public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { ApplicationContext context=new classpathxmlapplicationcontext ("Applicationcontext.xml"); test02 test02= (test02) Context.getbean ("test02"); System.out.println (Test02.getmsg ());}}

Iv. Example 4.1, project structure

4.2. Data layer Interface

4.2, the data layer is the implementation of the class

4.4, Business Layer Interface

4.5. Business Layer Implementation Class

4.5. Call the business layer

4.6. Configuration files

4.7. Results

Spring Learning in the Java family--Dependency injection (II.)

Related Article

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.