spring的了解以及简单框架的搭建
了解spring:
Spring是一个开源的控制反转(Inversion of Controller)和面向切面(AOP)的框架,目的是为了简化开发。
IOC(控制反转):
public class PersonServiceBean{
private PersonDao personDao=new PersonDaoBean(); public void save(Person person){
petsonDao.save(person);
}
}
PersonDaoBean是在应用内部创建及维护的。所谓的控制反转就是本身不依赖对象的创建以及维护,依赖对象的创建及维护是由外部容器负责的。这样控制权就由应用中转移到了外部容器。
依赖注入(Dependency Injection)
依赖注入,就是我们前面说的控制反转,将bean的创建交给了spring容器,那么在该bean中的一些依赖属性的值的赋值操作,就叫依赖注入。
一些使用spring的优势:
降低组件之间的耦合度,实现软件各层之间的解耦。
使用容器提供的服务,例如:事务传播行为
单例模式支持
AOP技术
spring简单框架的搭建
首先我们需要官网下载spring,至于这个,我也没找到。我的spring是spring2.5.6版本。链接:http://pan.baidu.com/s/1qYgtQtm
使用spring的简单需要jar包:
dist/spring.jar
lib/jakarta-commons/commons-logging.jar
如果需要使用面向切面(AOP)
lib/aspectj/aspectjweaver4.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果希望使用注解,还需要加入:
lib/j2ee/common-annotations.jar
配置文件模板beans.xml(文件名可随意):
配置文件beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
实例化spring容器:
方法一:
在类路径下寻找配置文件来实例化容器
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
方法二:
在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx=new FileSystemXmlApplicationContext(new String[]{"d:\\beans.xml"});
配置文件可以指定多个,字符串数组表示。
使用spring容器装载bean:
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
id:bean的唯一标识
name:bean的唯一标识
class:bean的源路径
为什么有了id还有name属性?
因为id是xml已有属性,会检查,是不能包含特殊字符
spring的三种实例化bean的方式:
1、构造方法实例化:
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
2、静态工厂实例化:
工厂中的生产bean方法为静态的
package cn.itcast.service.impl; public class PersonServiceBeanFactory { public static PersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
} } <bean id="personService2" class="cn.itcast.service.impl.PersonServiceBeanFactory"
factory-method="createPersonServiceBean"/>
3、实例化工厂实例化:
package cn.itcast.service.impl; public class PersonServiceBeanFactory { public static PersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
} public PersonServiceBean createPersonServiceBean2(){
return new PersonServiceBean();
} } <bean id="personServiceFactory" class="cn.itcast.service.impl.PersonServiceBeanFactory"/>
<bean id="personService3" factory-bean="personServiceFactory"
factory-method="createPersonServiceBean2"/>
bean的作用域:
通过bean中的scope标签可以设置bean的作用域。
<bean id=".." class=".." scope=".." />
singleton:单例模式,每一次getBean得到的是同一个。
prototype:每一次创建一个新实例
request
session
bean的生命周期:
1、bean的实例化:
默认情况下的bean作用范围是单实例的,是在容器实例化的时候就会对bean进行实例化的
bean的作用域范围是prototype的时候,是在调用getBean方法的时候进行实例化的
request,session也是这样。
如果希望在singleton中改变bean的实例化时机,可以使用lazy-init属性。
一般不建议在开发阶段使用,因为这样错误不方便查找。
singleton:单例模式,每一次getBean得到的是同一个。
可以设置延迟加载,使用lazy-init=true
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="singleton" lazy-init="true"/>
如果设置所有的bean延迟加载:
<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"
default-lazy-init="true">
bean的初始化方法和销毁方法:
bean的初始化方法:
public void init(){
System.out.println("初始化");
}
init-method属性
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"
init-method="init""/>
bean的销毁前方法:
public void destory(){
System.out.println("我要挂了");
}
destory-method属性:
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"
init-method="init" destroy-method="destory"/>
测试代码:
@Test
public void instanceSpring(){
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
PersonService personService=(PersonService) ctx.getBean("personService");
ctx.close();//正常关闭spring容器
}
注意:要使用AbstractApplicationContext对象才能正常关闭spring容器。
spring的了解以及简单框架的搭建的更多相关文章
- Spring security oauth2最简单入门环境搭建
关于OAuth2的一些简介,见我的上篇blog:http://wwwcomy.iteye.com/blog/2229889 PS:貌似内容太水直接被鹳狸猿干沉.. 友情提示 学习曲线:spring+s ...
- SSM(Spring+SpringMVC+Mybatis)+Mysql 框架整合搭建流程以及其间注意事项
复习SSM框架,太久没用自己手动撘一个,发现自己还是有很多地方忘记了和没注意的事项... 首先,直接给出总流程: 零.引jar包 1.引包(或者写maven.pom) 一.数据库部分 设计数据库各表结 ...
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- Spring+SpringMVC+Mybatis(SSM)框架集成搭建
Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...
- 浅析MyBatis(二):手写一个自己的MyBatis简单框架
在上一篇文章中,我们由一个快速案例剖析了 MyBatis 的整体架构与整体运行流程,在本篇文章中笔者会根据 MyBatis 的运行流程手写一个自定义 MyBatis 简单框架,在实践中加深对 MyBa ...
- asp.net mvc 简单项目框架的搭建(二)—— Spring.Net在Mvc中的简单应用
摘要:上篇写了如何搭建一个简单项目框架的上部分,讲了关于Dal和Bll之间解耦的相关知识,这篇来把后i面的部分说一说. 上篇讲到DbSession,现在接着往下讲. 首先,还是把一些类似的操作完善一下 ...
- asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦
学习asp.net 已经有近三个月的时间了,在asp.net mvc上花的时间最多,但个人真是有些菜,不得不说,asp.net mvc的水真的还是蛮深的.目前在公司实习,也见过公司几个项目的代码了.对 ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试
这一部分的主要目的是 配置spring-service.xml 也就是配置spring 并测试service层 是否配置成功 用IntelliJ IDEA 开发Spring+SpringMVC+M ...
- Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建
目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...
随机推荐
- 安装配置 Kafka Manager 分布式管理工具
Kafka Manager 特性,它支持以下内容(官方译解): 管理多个群集容易检查集群状态(主题,消费者,偏移量,经纪人,副本分发,分区分配)运行首选副本选举使用选项生成分区分配,以选择要使用的代理 ...
- (五)解决jQuery和其它库的冲突
在jQuery库中,几乎所有的插件都被限制在它的命名空间里.全局的对象都很好地存储在jQuery命名空间里,因此当把jQuery和其它javascript类库一起使用时,不会引起冲突.(注意:默认情况 ...
- MVC初了解
MVC:Model-View-Controller,将数据和显示形式分离. Model:能够看做是三层中的D层+B层,实现业务逻辑和与数据库的交互. View:看做是U层,用来显示数据. Contro ...
- Chrome自带恐龙小游戏的源码研究(完)
在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每 ...
- 解决php网页运行超时问题:Maximum execution time of 30 seconds exceeded
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Inetpub\wwwroot\ry.php on line 11 意 ...
- Unable to save settings: Failed to save settings. Please restart PyCharm解决
将工程的.ideas目录删掉,重启pycharm即可.
- Mysql 索引增加与删除
[1]索引 索引,通俗理解,即目录. 之前说过,计算机是对现实世界的模拟.目录应用在数据库领域,即所谓的索引. 目录的作用显而易见,所以建立索引可以大大提高检索的速度. 但是,会降低更新表的速度,如对 ...
- Makefile浅尝
[0]README makefile定义: 一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要一先编译,哪些文件需要后编译,哪 ...
- Linux 设置mysql开机启动
linux开启启动的程序一般放在/etc/rc.d/init.d/里面,/etc/init.d/是其软连接 mysql设为linux服务 cp /usr/local/mysql/support-fil ...
- 九度OJ 1017:还是畅通工程 (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4789 解决:2382 题目描述: 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府"畅通工程&quo ...