Java 系列之spring学习--依赖注入(二)
一、依赖注入的三种方式
接口注入,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学习--依赖注入(二)的更多相关文章
- SpringBoot系列: 理解 Spring 的依赖注入(二)
==============================Spring 容器中 Bean 的名称==============================声明 bean 有两个方式, 一个是 @B ...
- SpringBoot系列: 理解 Spring 的依赖注入(一)
==============================Spring 的依赖注入==============================对于 Spring 程序, Spring 框架为我们提供 ...
- java框架篇---spring IOC依赖注入
spring依赖注入的方式有4种 构造方法注入 属性注入 工厂注入 注解注入 下面通过一个实例统一讲解: User.java package com.bjsxt.model; public class ...
- Spring学习-依赖注入
Spring是基于IOC与AOP的框架,而其中的IOC(Inversion of Control)即反转控制是Spring的基础. 在以前学过的知识中,一个新的对象全部为自己手动new出来的,而在Sp ...
- Spring学习--依赖注入的方式
Spring 依赖注入: 属性注入(最常使用) 构造函数注入 工厂方法注入(很少使用,不推荐) 属性注入:通过 setter 方法注入 Bean 的属性值或依赖的对象 , 使用<property ...
- Java 系列之spring学习--spring搭建(一)
一.新建maven项目 二.引入spring jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...
- Java 系列之spring学习--注解(三)
一.注解 使用注解之前要开启自动扫描功能 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- Java 系列之spring学习--springmvc搭建(四)
一.建立java web 项目 二.添加jar包 spring jar包下载地址http://repo.spring.io/release/org/springframework/spring/ 2. ...
- Java 系列之spring学习--springmvc注解方式(五)
一.springmvc注解方式 注解方式使用的更多,更加灵活.在上一篇的博客的基础上修改springmvc-servlet.xml配置文件. <?xml version="1.0&qu ...
随机推荐
- Dijkstra的双栈算术表达式求值算法 C++实现
#include<iostream> #include<string> using namespace std; template<typename T> clas ...
- 【sqli-labs】 less7 GET - Dump into outfile - String (导出文件GET字符型注入)
使用此漏洞需要知道WEB程序的根路径并且MYSQL的secure-file-priv配置应为可写入该路径 添加单引号,发现错误被屏蔽 对应的SQL语句应为 ')) ... 字段还是3个 在数据库中执行 ...
- 【转】虚拟化(三):vsphere套件的安装注意及使用
vsphere套件里面主要的组件有esxi.vcenter server .vsphere client和vsphere web client. vcenter做为vsphere套件的核心管理成员之一 ...
- Linux常见英文报错中文翻译
Linux常见英文报错中文翻译(菜鸟必知) 1.command not found 命令没有找到 2.No such file or directory 没有这个文件或目录 3.Permission ...
- 简单说基于JWT和appkey、sercurtyKey的SSO、身份认证方案
环境介绍, 一个大的系统由多个子系统组成.典型地,假设有一个平台,其上接入了多个应用.则有几个常见的问题需要处理, 1.SSO(包括单个应用退出时,需要处理为整个系统退出): 2.平台跳转到应用.及应 ...
- 51nod1289 大鱼吃小鱼
有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右).问足够长的时间之后 ...
- js动态渲染链接outline为随机颜色
[].forEach.call($("*"),function(a){ a.style.outline="1px solid #"+(~~(Math.rando ...
- elementUI 图片上传限制上传图片的宽高
文件上传,需当上传的文件类型为图片的时候,需要限制图片的宽高. 此处采用了new Promise异步加载的方式,等图片上传加载完成后, 页面代码: <el-form-item label=&qu ...
- QT5.5移植全攻略【转】
一.编译1.到www.qt.io下载源码,qt-everywhere-opensource-src-5.5.0 2.设置编译器或者说平台.编译器是通过xplatform参数指定的,xplatform后 ...
- 转载 - Tarjan算法(求SCC)
出处:http://blog.csdn.net/xinghongduo/article/details/6195337 说到以Tarjan命名的算法,我们经常提到的有3个,其中就包括本文所介绍的求强连 ...