一、依赖注入的三种方式

  接口注入,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学习--依赖注入(二)的更多相关文章

  1. SpringBoot系列: 理解 Spring 的依赖注入(二)

    ==============================Spring 容器中 Bean 的名称==============================声明 bean 有两个方式, 一个是 @B ...

  2. SpringBoot系列: 理解 Spring 的依赖注入(一)

    ==============================Spring 的依赖注入==============================对于 Spring 程序, Spring 框架为我们提供 ...

  3. java框架篇---spring IOC依赖注入

    spring依赖注入的方式有4种 构造方法注入 属性注入 工厂注入 注解注入 下面通过一个实例统一讲解: User.java package com.bjsxt.model; public class ...

  4. Spring学习-依赖注入

    Spring是基于IOC与AOP的框架,而其中的IOC(Inversion of Control)即反转控制是Spring的基础. 在以前学过的知识中,一个新的对象全部为自己手动new出来的,而在Sp ...

  5. Spring学习--依赖注入的方式

    Spring 依赖注入: 属性注入(最常使用) 构造函数注入 工厂方法注入(很少使用,不推荐) 属性注入:通过 setter 方法注入 Bean 的属性值或依赖的对象 , 使用<property ...

  6. Java 系列之spring学习--spring搭建(一)

    一.新建maven项目 二.引入spring jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

  7. Java 系列之spring学习--注解(三)

    一.注解 使用注解之前要开启自动扫描功能 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  8. Java 系列之spring学习--springmvc搭建(四)

    一.建立java web 项目 二.添加jar包 spring jar包下载地址http://repo.spring.io/release/org/springframework/spring/ 2. ...

  9. Java 系列之spring学习--springmvc注解方式(五)

    一.springmvc注解方式 注解方式使用的更多,更加灵活.在上一篇的博客的基础上修改springmvc-servlet.xml配置文件. <?xml version="1.0&qu ...

随机推荐

  1. Android测试写入文本Log

    写入本地SD卡: @SuppressLint("SdCardPath") public void writeFileSdcard(String fileName, String m ...

  2. 【JSP】中文乱码问题

     原作者http://www.cnblogs.com/xing901022/p/4354529.html 阅读目录 之前总是碰到JSP页面乱码的问题,每次都是现在网上搜,然后胡乱改,改完也不明白原因. ...

  3. mysql主从机制的部署与应用

    部署mysql主从复制 Mysql master ip: 192.168.30.25   一主两从 Mysql slave ip: 192.168.30.24 Mysql slave ip:192.1 ...

  4. 神奇的splay树

    神奇的splay树 总结 splay树是一种BST,其通过不断的splay操作维持树的平衡:其基本思想是将频率高的点(实际是每次查找的点)通过splay操作旋转到树根 核心操作: update(x): ...

  5. 15.5.5 【Task实现细节】围绕 await 表达式的控制

    任何 await 表达式均表示执行路径的一个分支.首先,被等待的异步操作得到一个awaiter,然后检查其 IsCompleted 属性.若返回 true ,即可立即获得结果并继续.否则,需进行以下处 ...

  6. Java 8 集合不完全一览

    JDK 8 List 名称 线程安全 数据结构 允许 null 默认初始容量 扩容策略 备注 ArrayList 不安全 数组 允许 10 1.5 * old LinkedList 不安全 双链表 允 ...

  7. PAT 1072. Gas Station

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  8. cannot find -lGL

    解决方法: 以下操作都在root权限下进行! 1.按照提示安装对应的库文件,fedora安装库件的格式:yum install libxxx(你要装的库),如果已经安装GL库,会显示已经安装 Ps:如 ...

  9. 1010针对一个binlog日志的分析

    针对一个BINLOG日志的分析 -- 当前binlog_format | ROW[root@109 mysql]# cat wang1010.txt/*!50530 SET @@SESSION.PSE ...

  10. 【ACM】hdu_zs1_1005_大明A+B _201307291603

    大明A+B Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)Total Submissi ...