码迷,mamicode.com
首页 > 编程语言 > 详细

Java 系列之spring学习--依赖注入(二)

时间:2018-02-25 13:09:36      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:配置   3.2   xmlns   图片   out   项目结构   修改   body   java   

一、依赖注入的三种方式

  接口注入,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学习--依赖注入(二)

标签:配置   3.2   xmlns   图片   out   项目结构   修改   body   java   

原文地址:https://www.cnblogs.com/WJ--NET/p/8268323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!