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 ...
随机推荐
- C#写上位机中写曲线图的知识点(VS2019-WPF)
最近在写一个上位机,为了与下位机软件串口进行通信以及一些数据的形象显示,做到曲线图这一块的时候出现了一些问题,我用的是VS2019(个人不能吐槽VS2019新版本,因为平时也很少用VS,下载的时候就直 ...
- mysql主从机制的部署与应用
部署mysql主从复制 Mysql master ip: 192.168.30.25 一主两从 Mysql slave ip: 192.168.30.24 Mysql slave ip:192.1 ...
- BZOJ 4712: 洪水 挖坑待补
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- vue-属性传值 props
props属性传值 1.传具体的值 string(字符串) number(数值) boolean(布尔) 2.传一个引用 array(数组) object(对象) ----传引用----- 代码 ...
- vue scss 安装
1.开始在vue项目中使用sass,在命令行输入一下命令进行安装(使用git命令行要用shift+insert 进行粘贴否则粘贴不上) cnpm install node-sass --save-de ...
- Lua的五种变量类型、局部变量、全局变量、lua运算符、流程控制if语句_学习笔记02
Lua的五种变量类型.局部变量.全局变量 .lua运算符 .流程控制if语句 Lua代码的注释方式: --当行注释 --[[ 多行注释 ]]-- Lua的5种变量类型: 1.null 表示 ...
- 分别用for循环,while do-while以及递归方法实现n的阶乘!
分别用for循环,while do-while以及递归方法实现n的阶乘! 源码: package book;import java.util.Scanner;public class Access { ...
- 关于swift 底部工具栏图标锯齿模糊问题。
今天在底部工具栏添加图片时发现图片模糊而且有锯齿,开始一直以为是美工给的图片有问题,后来发现是要设置两种图片: 比如 index.png(默认30 * 30),indexSelected(选中后的图 ...
- oculus network error ovr53225466
最近调试oculus,搬运代码到win10平台,发现最近FB对oculus的服务程序进行了更新,必须要登陆账号才能进行调试. 于是安装oculusclient,但是登陆的过程中出现了问题,如果不用代理 ...
- Travel Card
Travel Card time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...