Spring框架学习之第2节
传统的方法和使用spring的方法
使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行。
项目结构

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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 在容器文件中配置bean(service/dao/domain/action/数据源) -->
<!-- bean元素的作用是,当我们的spring框架加载的时候,spring就会去看这里面有没有bean,如果有这个bean spring就会自动创建这个bean对象,并且将其装在到内存里面去 -->
<!--
类似于:Userservice userService = new UserService();
id号是和对象的变量名对应的,如果id号为litao,则这句话的含义为
UserService litao = new UserService();
property为属性值,name为注入这个属性,等价于
userService.setName("小明");set方法必须写否则注入不进去 -->
<bean id="userService" class="com.service.UserService">
<!-- 这里体现了容器的特点,配置bean并注入属性,体现出注入的概念 -->
<property name="name">
<value>小明</value>
</property>
<!-- 依赖注入 -->
<!-- 在userService对象中引用 bybService这个bean,不是用value而是ref引用-->
<property name="byeService" ref="bybService"/>
</bean>
<!-- 两种设置value的方式都行 -->
<bean id="bybService" class="com.service.BybService">
<property name="name" value="小红" />
</bean> </beans>
UserService.java
package com.service;
public class UserService {
public String name;
private BybService byeService;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BybService getByeService() {
return byeService;
}
public void setByeService(BybService byeService) {
this.byeService = byeService;
}
public void sayHello(){
System.out.println("hello " + name);
byeService.sayBye();
}
}
BybService.java
package com.service;
public class BybService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayBye(){
System.out.println("bye " + name);
}
}
Test.java
package com.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.BybService;
import com.service.UserService; public class Test { public static void main(String[] args){
//我们先使用传统方法,来调用UserService的sayHello方法
// UserService userService = new UserService();
// userService.setName("小明");
// userService.sayHello(); //我现在来使用spring来完成上面的任务
//1.得到spring的applicationContext对象(容器对象)
//通过类路径加载文件,这个对象就对象applicationContext.xml文件
//这句话会导致spring文件被加载
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService)ac.getBean("userService");
us.sayHello(); //从ac(代表applicationContext这个容器)中取出
//BybService bybService = (BybService) ac.getBean("bybService");
//bybService.sayBye(); }
}
spring的运行原理图

我们再看spring
对以前案例小结
spring实际上是一种容器框架,可以配置各种bean(action/service/domain/dao,spring各个层都可以接管),并且可以维护bean与bean之间的关系,当我们需要使用某个bean的时候,我们可以getBean(id),使用即可。
ioc是什么?
答:ioc(inverse of crontrol)控制反转:所谓控制反转就是把创建对象(bean),和维护对象(bean)的关系的权利转移到spring的容器文件(applicationContext.xml),通过配置文件就可以把创建对象和维护对象关系搞定,而程序的本身不再维护了,程序要什么就取什么。
学习框架,最重要的就是学习各个配置,配置搞清楚了,配置就明白了。
DI是什么?
答:(dependency injection)依赖注入:实际上DI和IOC是同一个概念,spring的设计者认为DI更准确地表示spring的核心技术。
大的项目(税务,证券,石油,财务,政务系统,oa,erp)有很多模块是通用的,
比方说:分页,数据源可以将其做成一个bean,再比如安全机制,写日志,都将其做成一个bean,用的
时候将这个bean注入进去就行了,这样开发就像搭积木了,用的时候引用一下就行了。

使用了spring变成的力度变大了,开发效率肯定会提高。
(汇编 指令编程,c 语句编程,Java对象编程,spring 组件编程)
把ApplicationContext做成一个单例的了
修改后的项目
项目结构

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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 在容器文件中配置bean(service/dao/domain/action/数据源) -->
<!-- bean元素的作用是,当我们的spring框架加载的时候,spring就会去看这里面有没有bean,如果有这个bean spring就会自动创建这个bean对象,并且将其装在到内存里面去 -->
<!--
类似于:Userservice userService = new UserService();
id号是和对象的变量名对应的,如果id号为litao,则这句话的含义为
UserService litao = new UserService();
property为属性值,name为注入这个属性,等价于
userService.setName("小明");set方法必须写否则注入不进去 -->
<bean id="userService" class="com.service.UserService">
<!-- 这里体现了容器的特点,配置bean并注入属性,体现出注入的概念 -->
<property name="name">
<value>小明</value>
</property>
<!-- 依赖注入 -->
<!-- 在userService对象中引用 bybService这个bean,不是用value而是ref引用-->
<property name="byeService" ref="bybService"/>
</bean>
<!-- 两种设置value的方式都行 -->
<bean id="bybService" class="com.service.BybService">
<property name="name" value="小红" />
</bean> </beans>
UserService.java
package com.service;
public class UserService {
public String name;
private BybService byeService;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BybService getByeService() {
return byeService;
}
public void setByeService(BybService byeService) {
this.byeService = byeService;
}
public void sayHello(){
System.out.println("hello " + name);
byeService.sayBye();
}
}
BybService.java
package com.service;
public class BybService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayBye(){
System.out.println("bye " + name);
}
}
Test.java
package com.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.BybService;
import com.service.UserService;
import com.util.ApplicationContextUtil; import org.springframework.context.ApplicationContext; public class Test { public static void main(String[] args){ ((UserService)ApplicationContextUtil.getApplicationContext().getBean("userService")).sayHello(); }
}
ApplicationContextUtil.java
package com.util; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; //工具类,保证applicationContext只能有一个
final public class ApplicationContextUtil { private static ApplicationContext ac = null; private ApplicationContextUtil(){ } static{
ac = new ClassPathXmlApplicationContext("applicationContext.xml"); } public static ApplicationContext getApplicationContext()
{
return ac;
} }
Spring框架学习之第2节的更多相关文章
- Spring框架学习之第1节
spring快速入门 ① spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...
- Spring框架学习之第9节
aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...
- Spring框架学习之第8节
<bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...
- Spring框架学习之第3节
model层(业务层+dao层+持久层) spring开发提倡接口编程,配合di技术可以更好的达到层与层之间的解耦 举例: 现在我们体验一下spring的di配合接口编程,完成一个字母大小写转换的案例 ...
- Spring框架学习之第7节
配置Bean的细节 ☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大 ②如何给集合类型注入值 Java中主要的map,set,list / 数组 ...
- Spring框架学习之第6节
bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ① 实例化(当我们的程序加载 ...
- Spring框架学习之第5节
request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...
- Spring框架学习之第4节
从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...
- Spring框架学习一
Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...
随机推荐
- P1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪
呵呵呵呵呵,很水的DP,同时能够朴素枚举出来,这数据弱的 是 吃了尸米吧.. var n,i,j,k,l,ans:longint; begin readln(n); to trunc(sqrt(n)) ...
- Team Homework #2 Decide the roles of each team member ——IloveSE
大家好,我们是IloveSEers! 徐姗,我是一个性格开朗,但却认为计算机比较枯燥的女生.经过两年的学习,自己的编程能力,并不是很强,在这方便还需多多练习.对于软件工程这门课,我充满期待,因为我不仅 ...
- Excel插件类库的设计思路
一.插件功能:提供多种读取Excel的方式,如NPOI.Com.Aspose,调用接口一致,包括Excel文件路径,sheet名称.读取是否包含列头(即Excel第一行是否为列头行) 二.实现思路 2 ...
- 【BZOJ】【1055】【HAOI2008】玩具取名
区间DP/记忆化搜索 sigh……看了提示才想到是区间DP >_>我果然还是太弱 f[l][r][k]表示L到R这段区间能否合并成K,那么就是枚举拆分方案(从哪里断开)和组合方式(左半合成 ...
- 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏
文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...
- .NET设计模式(11):组合模式(Composite Pattern)(转)
概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦. 意图 将对 ...
- 提高Asp.Net应用程序性能的十大方法(译感)
译完了提高Asp.Net应用程序的十大方法这篇文章,仔细想其中提到的每一条,在这里结合我的项目来谈谈.第一条:返回多个结果集因为我的项目中所有对数据库的访问的sql语句都是通过调用存储过程实现的,所以 ...
- logback日志文件需要注意点
1.支持的jar包 logback-access-1.1.1.jarlogback-classic-1.1.1.jarlogback-core-1.1.1.jar 2.logback.xml文件,we ...
- MYSQL注入天书之后记
后记 对于工具的看法: 我之所以在每个例子中只写了几个示例,是因为我希望你能通过这一两个示例举一反三将其他的列出来.如果让我来完成每一次完整的注入,应该在知道原理的情况下,必然使用工具或者自己写代码实 ...
- Extjs整体加载树节点
Ext.onReady(function () { Ext.define('company', { extend: 'Ext.data.Mode ...