浅谈Spring(一)
一、Spring引言
Spring是一款轻量级框架,代码入侵量很小,并且还是众多优秀的设计模式的组合(工厂、代理、模板、策略)。
特点:
1、方便解耦,简化开发
2、AOP编程的支持
3、声明式事务的支持
4、方便程序的测试
5、方便集成各种优秀框架
二、着手Spring
1、环境搭建
a、导入核心jar包跟第三方依赖jar包。
b、配置文件
spring的配置文件只有一个:applicationContext.xml,放置的位置随意。
2、初识
a、 核心API
ApplicationContext工厂类。
(1)ClassPathXmlApplicationContext(非web环境下)
(2)WebXmlApplicationContext(web环境下)
通过以上两个接口都可以获得工厂类,该工厂类是一个重量级资源,一个应用只应该创建一个。在运用了spring的项目中,所有用户所写的类,都应该通过ApplicationContext工厂类来生产。
b、代码示例
applicationContext.xml配置:
<bean id="first" class="xxxxx.xxxx.First"/>
java代码:
ApplicationContext ctx = new ClassPathXmlApplicationContext("/xxxx/applicationContext.xml"); First first = (First)ctx.getBean("first");
解释:
第一行通过ClassPathXmlApplicationContext读取spring配置文件获取applicationContext工厂类,参数为Spring配置文件的路径。
第二行则是由applicationContext工厂类来生产在配置文件中配置过的对象(根据id属性获得)。
c、web环境中的基本配置
在web.xml做以下配置
<!--配置spring配置文件的位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <!--配置spring核心监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
三、IOC和DI(控制反转和依赖注入)
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。 控制反转一般分为两种类型,依赖注入(Dependency Injection,简称DI)和依赖查找(Dependency Lookup)。依赖注入应用比较广泛。
简单点来说,就是将成员变量赋值的控制权,从代码中转移到xml文件中去。这样做的好处是解耦和,其原理是工厂+反射。
1、JDK类型成员变量的注入
a、8中基本类型+String
<bean id="user" class="xxxxx.User"> <property name="name"> <value>tom</value> </property> <property name="password"> <value>123456</> </property> <property name="age"> <value>20</value> </property> </bean>
<propery name="password" value="123123"/>
两种写法都可以。
b、数组
<property name="emails"> <list> <value>tom1@live.com</value> <value>tom2@live.com</value> <value>tom3@live.com</value> </list> </property>
c、set集合
<set> <value>123123</value> <value>123123</value> <value>123123</value> </set> //set标签内部的标签,取决于存放的数据类型,不一定是value
d、List集合
<list> <value>123123</value> <set> <value>123123</value> <ref local=""/> </list> //和set同理,内部的标签取决于存的数据类型
e、map集合
<map> <entry> <key><value>tom</value></key> <value>123123</value> </entry> </map> //一个entry节点代表一个键值对。
f、properties类型,等价于Map<Stirng,String>
<props> <prop key="key1" >value1</prop> <prop key="key2" >value2</prop> </props>
2、用户自定义类型
首先要在spring配置文件中创建所对应的类<bean id="" class="" />。然后再将其注入。
//java类,示例代码 public class UserService{ private UserDao userDao; get、set }
//spring配置文件 <bean id="userDao" class="xxxx.UserDao"/> <bean id="userService" class="xxxx.UserService"> <property name="userDao"> <ref local="userDao"> </property> </bean>
用户自定义的类型要用<ref local="">标签来注入。
3、构造注入(不常用)
<bean id="user" class="xxxx.User" > <constructor-arg> <value>xxxx</value> </constructor-arg> </bean>
四、spring创建复杂对象
1、利用FactoryBean创建复杂对象
spring创建复杂对象需要用一个FactoryBean接口来实现。
a、实现FactoryBean接口
//代码仅为示例 public class MyFactoryBean implements FactoryBean{ //1.用于书写创建复杂对象的代码,并把创建的复杂对象作为方法的返回值。 public Object getObject(){ InputStream is = Resources.getResourceAsStream(""); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); return sqlSessionFactory; //2.用于返回所创建的复杂对象的Class对象。 public Class getObjectType(){ return SqlSessionFactory.class(); } //3.用于控制复杂对象创建的次数。 public boolean isSingleton(){ return true; //只会被spring工厂创建一次。 //return false;会被创建多次. } }
b、在Spring配置文件中进行配置
<!--与普通bean配置无异--> <bean id="sqlSessino"class="xxx.xxx.MyFactoryBean"/>
浅谈Spring(一)的更多相关文章
- 浅谈Spring中的Quartz配置
浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...
- 浅谈Spring的两种配置容器
浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41 作者:黄小鱼ZZZ ...
- 1.1浅谈Spring(一个叫春的框架)
如今各种Spring框架甚嚣尘上,但是终归还是属于spring的东西.所以在这里,个人谈一谈对spring的认识,笔者觉得掌握spring原理以及spring所涉及到的设计模式对我们具有极大的帮助.我 ...
- 浅谈Spring MVC知识
关于MVC框架,我相信大家都不陌生,都会说也就是模型-视图-控制器这三层的框架结构,如果你参加面试的时候考官会问:“MVC框架是什么?你说一说.”其实我们都知道这个问题还需要问的,只要你是一个开发人员 ...
- 浅谈Spring发展史
1 码农的春天----------Spring来了 Spring官网 :http://www.springframework.org 关于Spring的发展起源要回溯到2002年,当时正是Java E ...
- 浅谈spring 声明式事物
此处主要讲讲事物的属性. 事物属性包含了五个方面: 1.传播行为 2.隔离规则 3.回滚规则 4.事物超时 5.是否只读 一.传播行为 事务的第一个方面是传播行为(propagation behavi ...
- 浅谈spring security 403机制
403就是access denied ,就是请求拒绝,因为权限不足 三种权限级别 一.无权限访问 <security:http security="none" pattern ...
- [SSH 3]以网上商城项目浅谈spring配置
导读:在做ITOO项目的时候,就用到了容器+反射,从而运用了依赖注入和依赖查找.如果看过WCF端的配置文件,那么对于这个spring的配置就很容易理解.本篇博客,是对于自己做的一个小项目中所运用到的s ...
- 浅谈Spring(四)
一.Spring+MyBatis整合 spring大大简化了Mybatis的开发步骤. 1.MyBatis的开发要点: mybatis-config.xml配置文件:配置与数据库的链接.mapper文 ...
- 浅谈Spring(三)
一.基础Spring的标准测试 1.导入spring与junit继承的jar 2.引入注解 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfig ...
随机推荐
- SqlServer导库语句
GO /****** 对象: StoredProcedure [dbo].[sp_ExportDatabase] 脚本日期: 07/18/2013 12:37:26 ******/ SET ANSI_ ...
- CSS的W3C标准的盒子模型和低版本IE浏览器的盒子模型
CSS中盒子模型的组成由内容区(content).内边距(padding).边框(border).外边距(margin)组成.内边距可细分为 padding-top.padding-right.pad ...
- 异常 ORA-00257: archiver error. Connect internal only, until freed
我oracle 是安装在linux 下. ORA-00257: archiver error. Connect internal only, until freed 得知是错误是由于归档日志(arch ...
- http request parameter
http request parameter add htmlspecialchars host?vendor_id=1000000&q=Some%20children%20wish%20to ...
- 独立版Jexus
一:下载资源包 把 jexus压缩包下载到linux临时文件夹中. cd /tmp wget linuxdot.net/down/jexus--x64.tar.gz 二,解压: tar -zxvf j ...
- jquery中validate插件表单验证
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- attempting to bokeyaunrun eclipse useing the jre instead of jdk,to run eclipse using
关于eclipse运行出现,attempting to bokeyaunrun eclipse useing the jre instead of jdk,to run eclipse using错误 ...
- css区块定位之浮动与清除属性
float属性将所属标记的显示空间指定为一个浮动元素,并使其周围对象按一定的方式环绕它排列. float属性的作用就象图像和表格的align属性一样,但可以用到任何元素上. clear属性的作用是禁止 ...
- jenkins 配置安全邮件
Jenkins网页设置界面只支持SSL协议 ,对于STARTTLS协议,需要修改jenkins的配置文件去支持基于TLS的SMTP认证 1.修改jenkins配置文件 打开jenkins配置文件/et ...
- hdu 5046 Airport 二分+重复覆盖
题目链接 给n个点, 定义两点之间距离为|x1-x2|+|y1-y2|. 然后要选出k个城市建机场, 每个机场可以覆盖一个半径的距离. 求在选出点数不大于k的情况下, 这个半径距离的最大值. 二分半径 ...