1. 程式人生 > >Java 系列之spring學習--依賴註入(二)

Java 系列之spring學習--依賴註入(二)

ssp app odin throw 函數 ans 圖片 .com 配置

一、依賴註入的三種方式

  接口註入,set註入,構造函數註入

二、構造函數註入

  2.1、測試類
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、編輯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-mvc.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.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="test01" class="test.test01">
    <constructor-arg index="0">
   			<value>dirk</value>
    </constructor-arg>
     <constructor-arg index="1">
   			<value>dirk2</value>
    </constructor-arg>
    </bean> 
   
</beans>
   2.3、測試 
package test;

public class test01 {

	public String msg=null;
	public String msg1=null;
	public test01(String msg,String msg1)
	{
		System.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();
	}

}

技術分享圖片

三、set註入  

  3.1測試類
package test;

public class test02 {
	public String msg;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}
 3.2、配置文件修改 
<?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-mvc.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.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>
    	</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、測試
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());	
	}

}

技術分享圖片

四、實例

  4.1、項目結構

技術分享圖片

  4.2、數據層接口

技術分享圖片

  4.2、數據層是實現類

技術分享圖片

  4.4、業務層接口

技術分享圖片

  4.5、業務層實現類

技術分享圖片

  4.5、調用業務層

技術分享圖片

  4.6、配置文件

技術分享圖片

  4.7、結果

技術分享圖片

Java 系列之spring學習--依賴註入(二)