一、依赖注入的三种方式

  接口注入,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. ANN:DNN结构演进History—RNN

    前言: CNN在图像处理领域的极大成功源于CNN的二维递进映射结构,通过训练多层卷积核来进行特征提取函数训练,在二维图像的稀疏表达和语义关联分析方面有天生的结构优势.而涉及时序问题的逻辑序列分析-边长 ...

  2. 安卓JNI使用OpenCV

    OpenCV也有Java数据结构的包,不过计算速度还是很慢,非不得已不使用此种方式调用OpenCV.使用NDK编写底层OpenCv的调用代码,使用JNI对代码进行封装,可以稍微提高一点效率. 参考链接 ...

  3. 大型工程多个目录下的Makefile写法

    1.前言 目前从事于linux下程序开发,涉及到多个文件,多个目录,这时候编译文件的任务量比较大,需要写Makefile.关于Makefile的详细内容可以参考网上流传非常广泛的<跟我一起写Ma ...

  4. 【技术累积】【点】【Java】【12】几种常见编码(持续更新)

    问题描述 有这么一段代码: String question = new String(record.getQuestion().getBytes("iso-8859-1"), &q ...

  5. 【sqli-labs】 less37 POST- Bypass MYSQL_real_escape_string (POST型绕过MYSQL_real_escape_string的注入)

    POST版本的less36 uname=1&passwd=1%df' or 1#

  6. java中为什么不允许类多重继承,却允许接口多重继承

    首先看下面这一段代码:(底下有热心网友更正,jdk1.8之后情况确实有点变化,等改天有空继续更) interface a{ void b();}interface a1 extends a{ void ...

  7. 如何彻底卸载系统自带的IE浏览器

    IE浏览器是windows系统上自带的浏览器,有时我们想要用其他的浏览器,例如chrome,卸载IE浏览器,那么应该如何卸载呢?下面就以win7上的IE9为例,告诉大家如何卸载IE浏览器. 方法/步骤 ...

  8. 命令行下配置Windows 2003防火墙

    命令:netsh firewall 参数: ? // 显示命令列表 add // 添加防火墙配置 delete // 删除防火墙配置 dump // 显示一个配置脚本 help // 显示命令列表 r ...

  9. java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z解决办法

    2019-05-20 23:02:20.168 |-INFO  [http-nio-8001-exec-2] com.xxx.ecc.cloudbiz.service.payment.impl.Wei ...

  10. TortoiseGit生成PuttyKey与GitHub的SSH进行关联

    1.打开Puttygen 要到进度条满格为止,知道出现如下界面: 把上面的Key复制. 最后点击[Save private key]保存. 2.登录GitHub进行如下操作: Settings-> ...