一、spring概述

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。

二、Spring特点

1、方便解耦,简化开发。

2、AOP编程的支持。

3、声明式事务的支持。

4、方便程序的测试

5、方便集成各种优秀框架

三、spring下载

下载地址:https://repo.spring.io/libs-release-local/org/springframework/spring/

进入后可选择下载版本,选择版本后,进入目录结构。其中dist是最终发布版本,包含开发所需lib和源码。docs是开发文档。schema是一些约束文件。

四、spring搭建入门案例

1、在eclipse中创建一个动态的web工程。

2、导入spring的基础lib包到lib文件夹下。

3、编写一个实体User类

package com.jichi.entity;

public class User {
private String name;
private Integer age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

4、创建applicationContext.xml文件

在src文件下,新建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:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"> </beans>

5、在beans内将user实体配置进去

<bean name="user" class="com.jichi.entity.User"></bean>

6、书写测试代码

public class TestDemo {
@Test
public void test1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user");
System.out.println(user);
}
}

7、结果显示,将user对象交给spring容器管理成功

com.jichi.entity.User@db5e9c

五、spring中的工厂BeanFactory与ApplicationContext的区别

1、BeanFactory

spring原始接口,功能较为单一,在从容器中获得对象的时候才会创建对象。

2、ApplicationContext

每次启动容器的时候,初始化容器中的所有对象并提供更多功能。

其中的实现类ClassPathXmlApplicationContext是加载类路径下的spring配置文件,FileSystemXmlApplicationContext是加载本地磁盘下spring的配置文件。

3、实现图

4、结论

web开发中,使用applicationContext。

在资源匮乏的环境可以使用BeanFactory。

六、spring的bean元素属性详解

1、class:被容器管理对象的完整类名。

2、name:被容器管理对象的名称,根据此名称从容器中获得该对象,可以重复,可以使用特殊字符。

3、id:被容器管理对象的唯一标识,id不可重复,不可使用特殊字符,作用与name相同,建议使用name。

4、scope:

  (1)singleton(默认):单例对象,该对象在spring容器中只会存在一个。

  (2)prototype:多例模式,配置为多例的对象不会在spring容器中创建,只有在从容器中要获取该对象的时候,容器对该对象进行创建,每次创建都是一个新的对象。注意在与struts2配合使用的时候,struts2中的action对象必须要配置成prototype这种多例模式。

  (3)request:web项目中,创建一个对象,将其存放在request域中。

  (4)session:web项目中,创建一个对象,将其存放在session域中。

5、init-method与destory-method

init-method是对象创建的时候所执行的方法,destory-method是对象销毁时所执行的方法。对象必须是单例的,同时容器关闭的时候,对象的销毁方法才会执行。

<bean name="user" class="com.jichi.entity.User" init-method="init" destroy-method="destory"></bean>
public class User {
private String name;
private Integer age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void init(){
System.out.println("初始");
}
public void destory(){
System.out.println("销毁");
}
}

七、spring创建对象的方式

1、空参构造方法

<bean name="user" class="com.jichi.entity.User" ></bean>

2、静态工厂实例化

(1)创建一个工厂类

public class UserFactory {
public static User createUser(){
return new User();
}
}

(2)配置

<bean name="user" class="com.jichi.factory.UserFactory" factory-method="createUser"></bean>

3、实例工厂实例化

(1)创建一个工厂类

public class UserFactory {
public static User createUser(){
return new User();
}
}

(2)配置

<bean name="user" factory-bean="userFactory" factory-method="createUser"></bean>

Spring入门详细教程(一)的更多相关文章

  1. spring入门详细教程(五)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

  2. Spring入门详细教程(四)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

  3. Spring入门详细教程(三)

    前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...

  4. Spring入门详细教程(二)

    前言 本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/1016553 ...

  5. ThinkJS框架入门详细教程(二)新手入门项目

    一.准备工作 参考前一篇:ThinkJS框架入门详细教程(一)开发环境 安装thinkJS命令 npm install -g think-cli 监测是否安装成功 thinkjs -v 二.创建项目 ...

  6. 经典Spring入门基础教程详解

    经典Spring入门基础教程详解 https://pan.baidu.com/s/1c016cI#list/path=%2Fsharelink2319398594-201713320584085%2F ...

  7. Xcode和github入门详细教程

    Xcode和github详细教程! 主要是参考了现在网上的一些资料给没整过的人一个详细的指南. (1)先在github上注册账号,自行解决! (2)在导航栏右上角new一个repository(仓库) ...

  8. SpringBoot入门详细教程

    一.SpringBoot入门 1.SpringBoot简介 SpringBoot是整个Spring技术栈的整合,来简化Spring应用开发,约定大于配置,去繁从简,just run 就能创建一 个独立 ...

  9. windows下Gulp入门详细教程 &&gulp安装失败的原因(红色)

    以下教程亲自实践可行: 另外添加一个Gulp自动编译.压缩.更新.测试的教程链接:https://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8 ...

随机推荐

  1. [NewLife.XCode]脏数据

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netstandard,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示 ...

  2. 以 SPI 方式获取 SD 卡容量(V2.0)

    下面是 SD 卡 V2.0 协议的 CSD 寄存器内容,来自官方手册: 单片机如何确定当前的 SD 卡遵循 V2.0 协议 CSD 寄存器为 128 个位,即 16 个字节.通过检测 CSD 寄存器的 ...

  3. VUE 组件通信总结

    1.prop 父组件传递给子组件,即通过VUE本身具有的Props属性来传递值 Child组件 <template> <span>{{message}}</span> ...

  4. 在Object-C中学习数据结构与算法之排序算法

    笔者在学习数据结构与算法时,尝试着将排序算法以动画的形式呈现出来更加方便理解记忆,本文配合Demo 在Object-C中学习数据结构与算法之排序算法阅读更佳. 目录 选择排序 冒泡排序 插入排序 快速 ...

  5. [转]Angular4首页加载慢优化之路

    本文转自:https://blog.csdn.net/itest_2016/article/details/80048398 Angular是一个比较完善的前端MVC框架,包含了模板,数据双向绑定,路 ...

  6. .NET使用ServerManager获取网站物理路径

    最近因为工作需要,用wpf做了一个辅助小工具,如下图 为了获取网站的物理路径,我分析了通过ServerManager获取到的变量,也通过百度搜索了很多,但仍然没有找到方法. 后来使用必应,在国外网站找 ...

  7. 多个微信小程序一个服务端架构

    由于某些特定的业务场景,当多个小程序需要一个服务端后台提供数据时,大家可能想到是HTTP路由.是的,实际上我们使用微服务的GateWay网关也是一样的,如下图微服务架构: 网关GateWay的作用在于 ...

  8. mysql函数技巧整理

    IF(expr,v1,v2) expr表达式为true时返回v1,否则返回v2 IFNULL(v1,v2) 如果v1为NULL,返回v2 :v1不为NULL 则返回v1 CASE expr WHEN ...

  9. Maven(八)Eclipse创建Web项目(复杂方式)

    1. 生成标准的Web工程结构 2. 勾选结尾为webapp的包 3. 生成的文件结构如下 3.1 生成的目录结构若存在错误,缺少servlet.api 3.1.1 添加步骤如下 4.生成后存在的缺点 ...

  10. C#设计模式之三抽象工厂模式(AbstractFactory)【创建型】

    一.引言 写了3篇有关设计模式的文章了,大家有了些反馈,说能从中学到一些东西,我感到很欣慰,那就继续努力.今天我要写第四个模式了,该模式叫抽象工厂.上一篇文章我们讲了[工厂方法]模式,它是为了解决[简 ...