Spring快速入门
什么是Spring
Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架
分层
SUN提供的EE的三层结构:web层、业务层、数据访问层(持久层/集成层)
Struts2是web层基于MVC设计模式框架
Hibernate是持久层的一个ORM的框架
一站式
Spring框架有对三层的每层解决方案
web层:Spring MVC
持久层:JDBC Template
业务层:Spring的Bean管理
Spring的核心
IOC(Inverse of Control 控制反转):将对象的创建权,交由Spring完成
AOP(Aspect Oriented Programming): 是面向对象的功能延伸.不是替换面向对象,是用来解决OO中一些问题.
Spring优点
方便解耦,简化开发
Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
AOP编程的支持
Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
声明式事务的支持
只需要通过配置就可以完成对事务的管理,而无需手动编程
方便程序的测试
Spring对Junit4支持,可以通过注解方便的测试Spring程序
方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
降低JavaEE API的使用难度
Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低
Spring的入门的程序
下载Spring的开发包
spring-framework-3.2.0.RELEASE-dist.zip ---Spring开发包
docs :spring框架api和规范
libs :spring开发的jar包
schema :XML的约束文档.
spring-framework-3.0.2.RELEASE-dependencies.zip ---Spring开发中的依赖包
创建web工程引入相应jar包
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
开发的日志记录的包:
com.springsource.org.apache.commons.logging-1.1.1.jar --- 用于整合其他的日志的包(类似Hibernate中slf4j)
com.springsource.org.apache.log4j-1.2.15.jar
创建Spring的配置文件
在src下创建一个applicationContext.xml
引入XML的约束:找到xsd-config.html.引入beans约束
<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.xsd">
编写实现类
public class HelloServiceImpl implements HelloService {
private String info;
public void setInfo(String info) {
this.info = info;
}
public void sayHello() {
System.out.println("Hello Spring..."+info);
}
}
在配置中配置类
<!-- 通过一个<bean>标签设置类的信息,通过id属性为类起个标识. -->
<bean id="userService" class="cn.yzu.spring3.demo1.HelloServiceImpl">
<!-- 使用<property>标签注入属性 -->
<property name="info" value="你好"/>
</bean>
创建测试类
@Test
// Spring开发
public void demo2() {
// 创建一个工厂类.
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) applicationContext.getBean("userService");
helloService.sayHello();
}
IOC和DI(*****)区别?
IOC:控制反转:将对象的创建权,由Spring管理.
DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中.
Spring框架加载配置文件
ApplicationContext 应用上下文,加载Spring 框架配置文件
加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml"); :加载classpath下面配置文件
加载磁盘路径:new FileSystemXmlApplicationContext("applicationContext.xml"); :加载磁盘下配置文件
BeanFactory与ApplicationContext区别?
ApplicationContext类继承了BeanFactory
BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类,ApplicationContext类加载配置文件的时候,创建所有的类
ApplicationContext对BeanFactory提供了扩展:国际化处理,事件传递,Bean自动装配,各种不同应用层的Context实现
早期开发使用BeanFactory,用例:
@Test
public void demo4(){
// ClassPathResource FileSystemResource
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("applicationContext.xml"));
HelloService helloService = (HelloService) beanFactory.getBean("userService");
helloService.sayHello();
}
MyEclipse配置XML提示
Window--->xml catalog--->add 找到schema的位置 ,将复制的路径 copy指定位置,选择schema location.
Spring快速入门的更多相关文章
- spring快速入门(四)
一.在spring快速入门(三)的基础上,我们来了解BeanFactory及配置. Client package com.murong.client; import org.springframewo ...
- spring快速入门(三)
一.在spring快速入门(二)的基础上,原先我们是采用构造方法完成对象的注入.这里还有其他的方法可以完成注入,通过set方法来完成. 修改UserActionImpl package com.mur ...
- spring快速入门(二)
一.在spring快速入门(一)的基础上,我们来了解spring是如何解决对象的创建以及对象之间的依赖关系的问题 (比如client中依赖UserAction的具体实现,UserActionImpl中 ...
- Java基础-SSM之Spring快速入门篇
Java基础-SSM之Spring快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java ...
- 【Java】Spring快速入门(一)
Spring介绍 Spring可以轻松创建Java企业应用程序.它提供了在企业环境中使用Java语言所需的一切,支持Groovy和Kotlin作为JVM上的替代语言,并可根据应用程序的需要灵活地创建多 ...
- spring快速入门(一)
对于为什么使用spring框架,这里不多做解释,详情请百度.本人推荐面向驱动程序学习,通过实战来瞧瞧spring技术的伟大.所以先来看看原始开发一个简单的例子,由例子引入spring相关的技术.如果错 ...
- spring3.0+mybatis+spring快速入门
一.首先奉上项目目录结构: 说明: dao,mapping,model包下的所有内容可以使用Generator工具自助生成. 具体用法,可以网上学习一下,比较简单,主要做以下工作: 1.提供相关的数据 ...
- Spring重温(一)--Spring快速入门
1.spring官网(https://repo.spring.io)下载依赖jar. 2.配置spring环境时还需要commons-logging相关jar. 3.打开eclise创建一个工程,并将 ...
- Spring 快速入门
1.持久层 (1) 域模型层 (2) Dao 持久层接口 (3) DaoImpl 持久层接口实现 2.业务层 Service 业务接口层 ServiceImpl 业务接口实现 3.展现层 Sp ...
随机推荐
- RabbitMQ 声明Queue时的参数们的Power
参数们的Power 在声明队列的时候会有很多的参数 public static QueueDeclareOk QueueDeclare(this IModel model, string queue ...
- Beta阶段第四次Scrum Meeting
情况简述 Beta阶段第四次Scrum Meeting 敏捷开发起始时间 2016/12/13 24:00 敏捷开发终止时间 2016/12/14 24:00 会议基本内容摘要 进度平稳推进,分配新任 ...
- iOS 8 Extensions
本文由海水的味道收集整理,欢迎转载 当前版本 0.0.1 iOS 8 Extensions 一.扩展概述 扩展(Extension)是iOS 8中引入的一个非常重要的新特性.扩展让app ...
- angularjs自带过滤器
filter: filter过滤器第一个参数若是对象: <ul> <li ng-repeat="friend in friends | filter:{'name':'Jo ...
- centos7下 安装mysql
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-rele ...
- Java排序算法——希尔排序
package sort; //================================================= // File Name : ShellSort //------- ...
- IO
文件过滤 http://codego.net/9245/ C# 文件处理 http://wenku.baidu.com/link?url=yXKiIA_OZYR4MIynDgz-qhOnfJoCyOQ ...
- Oracle开机自启动
linux下启动oracle su - oracle #用oracle用户登陆 sqlplus /nolog conn /as sysdba startup exit lsnrctl start ex ...
- 在.net中使用GAC
转自:http://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的dll的 ...
- IOS热更新-JSPatch实现原理+Patch现场恢复
关于HotfixPatch 在IOS开发领域,由于Apple严格的审核标准和低效率,IOS应用的发版速度极慢,稍微大型的app发版基本上都在一个月以上,所以代码热更新(HotfixPatch)对于IO ...