概念

1、是开源的轻量级框架

2、是一站式框架,就是说在java ee的三层结构中,每一层它都提供了不同的解决技术

web层:springMVC

servoce层:spring IOC ,控制反转,通过配置的方式来创建bean对象

 dao层:spring的jdbcTemplate

3、包含两大核心部分

(1)aop: 面向切面编程,相对于传统的纵向继承方式扩展功能,这种方式不需修改源代码就可以做到扩展,主要用于事务管理

(2) ioc : 控制反转,配置文件来实现创建对象

Ioc实现原理

这个技术的目的很简单,就是从配置文件中读出想要类名字串,利用反射来创建出他的实例,原理如下图

所需jar包 为4个核心的包

  ---  spring-beans-4.2.4.RELEASE.jar

  --- spring-context-4.2.4.RELEASE.jar

  --- spring-core-4.2.4.RELEASE.jar

  --- spring-expression-4.2.4.RELEASE.jar

ioc的两种创建类的方式

  (1)配置文件创建

  (2)注解方式

-- 配置文件方式

  第一步 创建配置文件,文件名与位置不固定,建议放在src下面,名字与源代码中默认文件名相同applicationContext.xml

第二步 在配置文件中引入schema约束文件

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"        
    xmlns:mvc="http://www.springframework.org/schema/mvc"     
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    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    
            http://www.springframework.org/schema/context     
            http://www.springframework.org/schema/context/spring-context.xsd    
            http://www.springframework.org/schema/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop.xsd "  
    default-autowire="byName">  
</beans>  

  第三步,配置需要创建的类

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"        
    xmlns:mvc="http://www.springframework.org/schema/mvc"     
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    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    
            http://www.springframework.org/schema/context     
            http://www.springframework.org/schema/context/spring-context.xsd    
            http://www.springframework.org/schema/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop.xsd "  
    default-autowire="byName">  
//ioc配置
<bean id = "user" class="cn.blueto.com.User"></bean>
</beans>  

  第四步, 引用对象

//加载配置文件
ApplicationContext c = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据配置文件中的对象id取出
User user=c.getBean("user");

在这里值得注意的是,bean标签里有几种属性需要记住

  1)id    表示对象的id,被引用时可填Id

  2) class  创建对象所在的类的路径

  3) name 与id效果一样,为了兼容之前的版本而保留,建议不使用

  4)scope 表示范围,取值有

    -singleton 默认值就是单例,表示全局只能创建一个实例

    -prototype 多例,可创建多个实例

    -request  创建对象放在request域里

    -session 放在session域里

    -gloabSession 放在globalSession里

----属性注入

就是给对象中的属性赋值,先看bean的定义

public class User{
private String name;
//有参数构造方式注入属性
public User(String n){
name = n;
}
//属性
public void setName(String name){
this.name=name;
}
}

配置文件中写法

<bean id="user" class="package.user">
//有参构造方式注入
<constructor-arg name= "name", value="叫啥名好呢"></constructor-arg>
</bean>

setter方式注入

<bean id="user" class="package.user">
<property name= "name", value="叫啥名好呢"></property>
</bean>

注入复杂对象写法

public class User{
private String name;
private Logininfo login; //复杂对象属性
public void setLogininfo(Logininfo n){
login= n;
}
//属性
public void setName(String name){
this.name=name;
}
}

复杂对象注入的配置文件中写法

<bean id="user" class="package.user">
<property name= "login", ref="login"></property> //ref引用其他beab对象的id
</bean>
<bean id="login" class="package.Logininfo"></bean>

数组等对象注入

public class User{
private String[] arrs;
private List list;
private Map map;
private Properties properties;
//setter方法
。。。
}
<bean id="user" class="package.user">
<property name= "arrs">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name= "list">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
</list>
</property>
<property name= "map">
<map>
<entry key ="a1" value="adfdf"></entry>
<entry key ="a2" value="adfdf"></entry>
<entry key ="a3" value="adfdf"></entry>
</map>
</property>
<property name= "properties">
<props>
<prop key ="driverClass">com.xxx.jdbc.Driver</prop>
<entry key ="passwd">123456</entry>
<entry key ="user">addd</entry>
</props>
</property>
</bean>

-- 注解方式创建对象

  与上一种方式相比 ,使用注解方式的配置则简单多了

  配置文件中写法

<beans xmlns="...">
<!-- 开户注解扫描
到指定包里扫描类,方法及属性上面是否有注解,指定包如果有多个,可用逗号分隔开,如果写成它的父包名字,则指包含父包下所有类
-->
<context:componet-scan base-package="cn.blueto"></context:componet-scan>
<!-- 开户注解扫描
只扫描属性上面是否有注解
-->
<context:annotation-config></context:annotation-config> </bean>

java类中写法

@Component(value="user")
@Scope(value="prototype")
public class User{
private String name;
//有参数构造方式注入属性
public User(String n){
name = n;
}
//属性
public void setName(String name){
this.name=name;
}
}

取得实例

//加载配置文件
ApplicationContext c = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据配置文件中的对象id取出
User user=c.getBean("user");

spring中的创建对象的4个注解

  -@Component

  -@Controller   web层

  -@Service      业务层

  -@Repository   持久层,数据库操作层

  目前四个注解都是一样的效果,只是创建对象而已

spring中的注入属性

  -@Autowired   根据类名找到对象的实现类并创建,再注入到属性中

  -@Resource   注入指定类的实例到属性中,@Resource(name="beanid")

Spring 加载配置文件原理

ioc原理是在服务加载配置文件后,创建配置文件中的bean对象,也就是执行这段代码:

//加载配置文件
ApplicationContext c = new ClassPathXmlApplicationContext("applicationContext.xml");
//根据配置文件中的对象id取出
//User user=c.getBean("user");

那服务启动后,在什么时候开始加载配置呢?它的加载过程是这样的

--服务器启动时,为每个web项目创建一个servletContext对象,当监听器监听到ServletContext对象创建后,开始加载配置文件,默认文件为applicationContext.xml

--把创建的各个bean对象通过setAttribute方法存到servletContext域里

--取对象时通过getAttribute得到

  Spring中已经封装好了监听器并加载配置文件的方法,如果想要在服务启动后自动加载配置文件,只需要在web.xml文件中作如下配置即可

//配置监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> //配置指定加载的配置文件,如果 不配置,则去Src下面找默认的applicationContext.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value> //如在src下直接写,如不在则要加包名路径
</context-param>

spring学习之一概念的更多相关文章

  1. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  2. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  3. spring技术核心概念纪要

    一.背景 springframework 从最初的2.5版本发展至今,期间已经发生了非常多的修正及优化.许多新特性及模块的出现,使得整个框架体系显得越趋庞大,同时也带来了学习及理解上的困难. 本文阐述 ...

  4. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  5. 我的Spring学习记录(二)

    本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...

  6. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  7. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  8. Spring学习13-中IOC(工厂模式)和AOP(代理模式)的详细解释

    我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC是工厂模式参考:设计模式- ...

  9. spring学习 8-面试(事务,解决线程安全)

    1.介绍一下Spring的事物管理 参考:Spring 学习7 -事务 2.Spring如何处理线程并发问题    Spring使用ThreadLocal解决线程安全问题 参考:Spring学习11- ...

随机推荐

  1. 【比赛】NOIP2017 奶酪

    开始看到题以为是计算几何,后面发现不是,然后秒掉了. 可能写SPFA写多了,别人都是并查集做的,我用的是SPFA. 不过无所谓,我们把题目中的下底面和上表面看成两个点,那么就是求这两个点的连通性,如果 ...

  2. Android性能优化:手把手带你全面了解 内存泄露 & 解决方案

    . 简介 即 ML (Memory Leak)指 程序在申请内存后,当该内存不需再使用 但 却无法被释放 & 归还给 程序的现象2. 对应用程序的影响 容易使得应用程序发生内存溢出,即 OOM ...

  3. Java8 新特性Stream 的学习和使用方法

    流(Stream) 流是java 8 中新引入的特性,用来处理集合中的数据,Stream 是一个来自数据源的元素队列并支持聚合操作. Java 中 Stream 不会存储元素. 数据源 流的来源. 可 ...

  4. hdu2138 Miller_Rabin

    Description   Give you a lot of positive integers, just to find out how many prime numbers there are ...

  5. 彻底解决mac下terminal路径显示问题

    mac 配色 mac shell配色  ~/.bash_profile是bash shell中当前登录用户的配置文件.bash是“终端”中默认的shell. alias ls=”ls -G”是给”ls ...

  6. Java利用poi生成word(包含插入图片,动态表格,行合并)

    转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...

  7. BAYES和朴素BAYES

    0 前言  朴素贝叶斯算法仍然是流行的十大挖掘算法之一,该算法是有监督的学习算法,解决的是分类问题,如客户是否流失.是否值得投资.信用等级评定等多分类问题.该算法的优点在于简单易懂.学习效率高.在某些 ...

  8. 不知道怎么改的尴尬R语言的ARIMA模型预测

    数据还有很多没弄好,程序还没弄完全好. > read.xlsx("H:/ProjectPaper/论文/1.xlsx","Sheet1") > it ...

  9. 【Asp.net入门2-01】C#基本功能

    C#是一种功能强大的语言,但并不是所有程序员都熟悉我们将在本书中讨论的所有功能.因此, 本章将介绍优秀的Web窗体程序员需要了解的C#语言功能. 本章仅简要介绍每一项功能.有关C#语言本身的知识不是本 ...

  10. array_multisort 二维数组排序

    用PHP自带array_multisort函数排序 <?php $data = array();    $data[] = array('volume' => 67, 'edition' ...