- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章JavaWeb Spring依赖注入深入学习由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、依赖注入(DI) 。
依赖注入听起来很高深的样子,其实白话就是:给属性赋值。一共有两种方法,第一是以构造器参数的形式,另外一种就是以setting方法的形式.
1 构造器注入 。
1 使用构造器注入 。
使用xml的注入方式 。
A. 通过参数的顺序 。
<constructor-arg index="0"><value>张三</value></constructor-arg> <constructor-arg index="1"><value>56</value></constructor-arg> 。
B. 通过参数的类型 。
<constructor-arg type="java.lang.Integer"><value>56</value></constructor-arg> <constructor-arg type="java.lang.String"><value>张三</value></constructor-arg> 。
具体实例 。
假如现在要对一个Person类注入参数,Student是一个另外一个类.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public
class
Person {
private
String pid;
private
String name;
private
Student student;
public
Person(String pid, Student student){
this
.pid= pid;
this
.student = student;
}
public
Person(String pid, String name){
this
.pid = pid;
this
.name = name;
}
}
|
配置applicationContext.xml,假如不进行参数配置,则报错,找不到相应的构造器。配置了相应的参数,则应在类中声明相应的构造函数.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<
bean
id
=
"person"
class
=
"com.itheima10.spring.di.xml.constructor.Person"
>
<!--
不配参数,将会采取默认的构造器
constructor-arg person类中某一个构造器的某一个参数
index 为参数的角标
type 参数的类型
value 如果为基础属性,则用这个赋值
ref 引用类型赋值
-->
<
constructor-arg
index
=
"0"
type
=
"java.lang.String"
value
=
"aaa"
></
constructor-arg
>
<
constructor-arg
index
=
"1"
ref
=
"student"
></
constructor-arg
>
</
bean
>
<
bean
id
=
"person1"
class
=
"com.itheima10.spring.di.xml.constructor.Person"
>
<
property
name
=
"pid"
value
=
"1"
></
property
>
</
bean
>
<
bean
id
=
"student"
class
=
"com.itheima10.spring.di.xml.constructor.Student"
></
bean
>
</
beans
>
|
编写测试类DIXMLConstructorTest ,进行断点调试,将会发现根据配置的参数,进入的构造函数是Person(String pid, Student student) 。
1
2
3
4
5
6
7
8
|
public
class
DIXMLConstructorTest {
@Test
public
void
test1(){
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
Person person = (Person) context.getBean(
"person"
);
}
}
|
2 使用属性setter方法进行注入 。
使用xml的注入方式:
A. 简单Bean的注入 简单Bean包括两种类型:包装类型和String 。
1
2
3
4
5
|
<
bean
id
=
"personService"
class
=
"com.itcast.bean.impl.PersonServiceImpl"
>
<!-- 基本类型,string类型 -->
<
property
name
=
"age"
value
=
"20"
></
property
>
<
property
name
=
"name"
value
=
"张无忌"
></
property
>
</
bean
>
|
B. 引用其他Bean 。
1
2
3
4
|
<
bean
id
=
"person"
class
=
"com.itcast.bean.Person"
/>
<
bean
id
=
"personService"
class
=
"com.itcast.bean.impl.PersonServiceImpl"
>
<
property
name
=
"person"
ref
=
"person"
/>
</
bean
>
|
1.1 装配list集合 。
1
2
3
4
5
6
7
|
<
property
name
=
"lists"
>
<
list
>
<
value
>list1</
value
>
<
value
>list2</
value
>
<
ref
bean
=
"person"
/>
</
list
>
</
property
>
|
1.2 装配set集合 。
1
2
3
4
5
6
7
|
<
property
name
=
"sets"
>
<
set
>
<
value
>list1</
value
>
<
value
>list2</
value
>
<
ref
bean
=
"person"
/>
</
set
>
</
property
>
|
1.3 装配map 。
1
2
3
4
5
6
7
8
9
10
|
<
property
name
=
"maps"
>
<
map
>
<
entry
key
=
"01"
>
<
value
>map01</
value
>
</
entry
>
<
entry
key
=
"02"
>
<
value
>map02</
value
>
</
entry
>
</
map
>
</
property
>
|
map中的<entry>的数值和<list>以及<set>的一样,可以使任何有效的属性元素,需要注意的是key值必须是String的.
1.4 装配Properties 。
1
2
3
4
5
6
|
<
property
name
=
"props"
>
<
props
>
<
prop
key
=
"01"
>prop1</
prop
>
<
prop
key
=
"02"
>prop2</
prop
>
</
props
>
</
property
>
|
具体实例 。
1.创建两个对象Person和Student 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package
xgp.spring.demo;
import
java.util.List;
import
java.util.Map;
import
java.util.Properties;
import
java.util.Set;
public
class
Person {
private
String pid;
private
String name;
private
Student student;
private
List lists;
private
Set sets;
private
Map map;
private
Properties properties;
private
Object[] objects;
public
Person(){
System.out.println(
"new person"
);
}
//省略getter和setter方法
}
|
1
2
3
4
5
6
7
8
9
10
11
|
package
xgp.spring.demo;
public
class
Student {
public
Student(){
System.out.println(
"new student"
);
}
public
void
say(){
System.out.println(
"student"
);
}
}
|
配置applicationContext.xml文件 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--
把person和student放入到spring容器中
property 用来描述Person类的属性
value 如果是一般属性,则用value赋值
ref 如果该属性是引用类型,用ref赋值
-->
<
bean
id
=
"person"
class
=
"com.itheima10.spring.di.xml.setter.Person"
init-method
=
"init"
lazy-init
=
"true"
>
<
property
name
=
"pid"
value
=
"1"
></
property
>
<
property
name
=
"name"
value
=
"王二麻子"
></
property
>
<
property
name
=
"student"
ref
=
"student"
></
property
>
<
property
name
=
"lists"
>
<
list
>
<
value
>list1</
value
>
<
value
>list2</
value
>
<
ref
bean
=
"student"
/>
</
list
>
</
property
>
<
property
name
=
"sets"
>
<
set
>
<
value
>set1</
value
>
<
value
>set2</
value
>
<
ref
bean
=
"student"
/>
</
set
>
</
property
>
<
property
name
=
"map"
>
<
map
>
<
entry
key
=
"entry1"
>
<
value
>map1</
value
>
</
entry
>
<
entry
key
=
"entry2"
>
<
ref
bean
=
"student"
/>
</
entry
>
</
map
>
</
property
>
<
property
name
=
"properties"
>
<
props
>
<!--
不需要引用类型
-->
<
prop
key
=
"prop1"
>prop1</
prop
>
<
prop
key
=
"prop2"
>prop2</
prop
>
</
props
>
</
property
>
<
property
name
=
"objects"
>
<
list
>
<
value
>aa</
value
>
<
value
>bb</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
id
=
"student"
class
=
"com.itheima10.spring.di.xml.setter.Student"
></
bean
>
</
beans
>
|
编写测试类DIXMLSetterTest 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package
xgp.spring.test;
import
org.junit.Test;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
xgp.spring.demo.Person;
public
class
DIXMLSetterTest {
/**
* spring 容器做的事情:
* 1、spring容器做了什么?(1)启动spring容器
* (2)为person和student两个bean创建对象
* (3)解析property的name属性,拼接setter方法,解析property的
* value或者ref属性,给setter方法传递参数,利用反射技术给对象赋值。
* (4)从spring容器中,把对象提取出来,对象调用方法。
* 2、spring容器执行顺序是什么?
*/
@Test
public
void
test1(){
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
Person person = (Person) context.getBean(
"person"
);
System.out.println(person.getPid());
System.out.println(person.getName());
System.out.println(person.getLists());
System.out.println(person.getSets());
System.out.println(person.getMap());
System.out.println(person.getObjects().length);
}
}
/*1
王五
[list1, list2, xgp.spring.demo.Student@76a9b9c]
[set1, set2, xgp.spring.demo.Student@76a9b9c]
{entry1=map1, entry2=map2}
2*/
|
spring容器的执行顺序 。
1.都是默认设置 。
2.设置student(lazy-init=true) 。
3.设置person(lazy-init=true) 。
总结 可以采用两种方法注入参数,构造器要写对应的构造函数,setter要生成相应的setter方法,并编写默认的构造器.
2.5 IOC与DI的意义 。
学了这些,发现有什么意义?下面写个文档管理系统例子来说明,需求见下图 。
1.编写Document 接口 。
1
2
3
4
|
public
interface
Document {
public
void
read();
public
void
write();
}
|
2、编写实现类WordDocument ,ExcelDocument ,PDFDocument 。
1
2
3
4
5
6
7
8
9
|
public
class
WordDocument
implements
Document{
public
void
read() {
System.out.println(
"word read"
);
}
public
void
write() {
System.out.println(
"word write"
);
}
}
|
3、编写文档管理 系统 DocumentManager 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public
class
DocumentManager {
private
Document document;
public
void
setDocument(Document document) {
this
.document = document;
}
public
DocumentManager(){
}
public
DocumentManager(Document document) {
super
();
this
.document = document;
}
public
void
read(){
this
.document.read();
}
public
void
write(){
this
.document.write();
}
}
|
4、编写测试类DocumentTest 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
/**
* 利用ioc和di能做到完全的面向接口编程
*
*/
public
class
DocumentTest {
/**
* Document document = new WordDocument();
* 这行代码是不完全的面向接口编程,因为等号的右边出现了具体的类
*/
@Test
public
void
testDocument_NOSPRING(){
Document document =
new
WordDocument();
DocumentManager documentManager =
new
DocumentManager(document);
documentManager.read();
documentManager.write();
}
/**
* 在代码端不知道Document是由谁来实现的,这个是由spring的配置文件决定的
* <bean id="documentManager"
class="com.itheima10.spring.iocdi.document.DocumentManager">
<!--
document为一个接口
-->
<property name="document">
<!--
wordDocument是一个实现类,赋值给了document接口
-->
<ref bean="pdfDocument"/>
</property>
</bean>
*/
@Test
public
void
testDocument_Spring(){
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
DocumentManager documentManager =(DocumentManager)context.getBean(
"documentManager"
);
documentManager.read();
documentManager.write();
}
}
|
从上面可以看出不适用spring和适用spring的区别 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!--
documentManager,wordDocument,excelDocument,pdfDocument放入到spring容器中
-->
<
bean
id
=
"wordDocument"
class
=
"com.itheima10.spring.iocdi.document.WordDocument"
></
bean
>
<
bean
id
=
"excelDocument"
class
=
"com.itheima10.spring.iocdi.document.ExcelDocument"
></
bean
>
<
bean
id
=
"pdfDocument"
class
=
"com.itheima10.spring.iocdi.document.PDFDocument"
></
bean
>
<
bean
id
=
"documentManager"
class
=
"com.itheima10.spring.iocdi.document.DocumentManager"
>
<!--
document为一个接口
-->
<
property
name
=
"document"
>
<!--
wordDocument是一个实现类,赋值给了document接口
-->
<
ref
bean
=
"pdfDocument"
/>
</
property
>
</
bean
>
|
使用spring只需要在applicationContext中配置相应的<ref bean="">对象,而不需要关注具体的实现类,实现完全的面向接口编程,这也是为什么spring能够和这么多工具集成的原因.
2.6 mvc实例–模拟structs2 。
需求描述 。
建立工程目录 。
编码: 1、创建Dao层 建立PersonDao接口和实现类PersonDaoImpl 。
1
2
3
4
5
6
7
8
9
10
|
public
interface
PersonDao {
public
void
savePerson();
}
public
class
PersonDaoImpl
implements
PersonDao {
@Override
public
void
savePerson() {
System.out.println(
" save person"
);
}
}
|
2、建立service层,PersonService接口与PersonServiceImpl实现类 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
interface
PersonService {
public
void
savePerson();
}
public
class
PersonServiceImpl
implements
PersonService{
private
PersonDao personDao;
public
void
setPersonDao(PersonDao personDao) {
this
.personDao = personDao;
}
@Override
public
void
savePerson() {
this
.personDao.savePerson();
}
}
|
3、建立Action,PersonAction类 。
1
2
3
4
5
6
7
8
9
10
11
|
public
class
PersonAction {
private
PersonService personService;
public
void
setPersonService(PersonService personService) {
this
.personService = personService;
}
public
void
savePerson(){
this
.personService.savePerson();
}
}
|
4、配置applicationContext.xml 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--
把service,dao,action层的类放入到spring容器中
-->
<
bean
id
=
"personDao"
class
=
"xgp.spring.demo.PersonDaoImpl"
></
bean
>
<
bean
id
=
"personService"
class
=
"xgp.spring.demo.PersonServiceImpl"
>
<
property
name
=
"personDao"
>
<
ref
bean
=
"personDao"
/>
</
property
>
</
bean
>
<
bean
id
=
"personAction"
class
=
"xgp.spring.demo.PersonAction"
>
<
property
name
=
"personService"
>
<
ref
bean
=
"personService"
/>
</
property
>
</
bean
>
|
5、编写测试类testMVC 。
1
2
3
4
5
6
7
8
9
|
public
class
MVCTest {
@Test
public
void
testMVC(){
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
PersonAction personAction = (PersonAction)context.getBean(
"personAction"
);
personAction.savePerson();
//save person
}
}
|
上述实例很清楚的展现出了spring的面向接口编程,service层只需调用dao层的接口,而不需要关注于dao层的实现类,action也只需调用service的接口,而不需要关注service的实现类.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/i10630226/article/details/50507100 。
最后此篇关于JavaWeb Spring依赖注入深入学习的文章就讲到这里了,如果你想了解更多关于JavaWeb Spring依赖注入深入学习的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我在 gobject 上阅读了一个维基百科页面,上面写着, Depending only on GLib and libc, GObject is a cornerstone of GNOME and
如何注册一个依赖属性,其值是使用另一个依赖属性的值计算的? 由于 .NET 属性包装器在运行时被 WPF 绕过,因此不应在 getter 和 setter 中包含逻辑。解决方案通常是使用 Proper
我一直在尝试将 ActionbarSherlock maven 依赖项添加到我的项目中 com.actionbarsherlock library 4.2.0 在我的 po
http://tutorials.jenkov.com/ood/understanding-dependencies.html#whatis说(强调我的): Whenever a class A us
我对所有这些魔法有点不清楚。 据我了解,依赖属性是从 DependencyObject 继承的,因此存储值: 如果分配了值(在本地字典中),则在实例本身中 或者如果未指定值,则从指向父元素的链接中获取
我刚刚更新了在 ASP.NET Framework 4.5.2 版上运行的 MVC Web 应用程序。我正在使用 Twilio 发送 SMS 消息: var twilio = new TwilioRe
我刚刚发现了一件令人生畏的事情。 spring 依赖坐标有两个版本。 项目依赖于 spring mvc 和 spring flow。有两组并行的依赖项。 Spring MVC 具有以下方案的依赖项
我正在尝试包含 的 maven 依赖项 org.jacorb jacorb 2.3.1 依赖已解决,但它导致另一个依赖 picocontainer 出现问题: [ERROR
我正在尝试在 Haskell 项目中包含特定版本的库。该库是住宿加早餐型的(用于 martix 操作),但我需要特定的 0.4.3 版本,该版本修复了乘法实现的错误。 所以,我的 stack.yaml
有谁知道如何制作依赖的 UIPickerView.例如,当我选择组件一的第 2 行时,组件二的标题会发生变化吗? 我在互联网上查找过,没有真正的答案,我尝试过使用 if 和 switch 语句,但它们
我正在编写一个用于验收测试的项目,由于各种原因,这依赖于另一个打包为 WAR 的项目。我已成功使用 maven-dependency-plugin 解压 WAR,但无法让我的项目包含解压的 WEB-I
或多或少我在 session 上大量构建我的网站(特别是重定向用户等),我很好奇这是否是一种危险的做法。禁用浏览器 cookie 保存的用户的大致比例是多少?我愿意接受任何建议:) 谢谢 最佳答案 s
开始玩 Scala futures,我被依赖的 futures 困住了。 让我们举个例子。我搜索地点并获得 Future[Seq[Place]]。对于这些地点中的每一个,我搜索最近的地铁站(该服务返回
或多或少我在 session 上大量构建我的网站(特别是重定向用户等),我很好奇这是否是一种危险的做法。禁用浏览器 cookie 保存的用户的大致比例是多少?我愿意接受任何建议:) 谢谢 最佳答案 s
我有一个二进制文件,需要一些 *.so 文件才能执行。现在,当我尝试在一些旧机器上执行它时,它会显示 /lib/libc.so.6: version `GLIBC_2.4' not found 如何将
我尝试使用 Dygraph 来表示图表,我在 https://github.com/danvk/dygraphs 中找到了代码,但是它有太多的依赖文件,我觉得很烦人。是否有一个文件可以容纳所有必需的
我正在处理一个 javascript 文件,该文件 a) 声明一个具有函数的对象,并且 b) 使用它期望在外部声明的散列调用该对象的 init 函数。我的 Jasmine 规范提示它找不到哈希,因为它
最近我一直在学习 Angular 并且进展顺利,但是关于依赖注入(inject)的一些事情我仍然不清楚。 是否有任何理由在我的 app.js 文件中声明我的应用程序的其他部分(服务、 Controll
考虑一个名为 foo 的表,它有 id (PRIMARY & AUTO_INCREMENT) 列。我正在向该表中插入一行,挑战从此时开始。 $db->query("INSERT INTO `foo`
我正在使用级联下拉 jquery 插件。 (https://github.com/dnasir/jquery-cascading-dropdown) 我有两个下拉菜单。 “客户端”和“站点”。 根据您
我是一名优秀的程序员,十分优秀!