Spring框架简介
Spring框架是一个轻量级的开源框架。
Spring框架的核心主要是两部分
1,AOP:面向切面编程,在Spring中AOP主要应用在数据库事务的问题上,项目中大部分的事务代码都是重复的,所以spring框架就把事务提取出来用AOP的编程思想去管理事务。
2,IOC:控制反转,简单的说就是要去调用不是static修饰的方法的时候要创建出来方法所在的类的对象才能调用,而Spring就是帮你去创建了这个对象,也就是说可以吧所有的要用到的对象Spring都可以帮你创建出来。
Spring是一个一站式的框架(一步到位,javaee 三层开发),Spring本身也有这层对应的功能,web层有SpringMvc,SpringBoot。Service层有Spring,Dao层有Spring整合的JDBC。Spring本身就是就是一个容器,放了什么对象就有什么对象的功能,当然Hibernate和Struts2等等都可以放到Spring容器中,也就是Spring帮别的框架去管理对象。我自己对Spring的理解就是,Spring帮别的东西管理对象,就是把别的东西放入到Spring中,也就是说spring也可以帮你去创建别的框架里面的对象,像SessionFactory,DataSource都可以
Spring配置详解(这里简单的写上集中简单的常用的配置)
先在这里写一个相对完整的配置文件可以对应下面的详细的进行理解学习。
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/p">
<bean name="user" class="cn.hd.injection.User">
<property name="name" value="赵赛昌"></property>
<property name="age" value="10"></property>
<property name="car" ref="car"></property>
</bean>
<bean name="car" class="cn.hd.injection.Car">
<property name="name" value="法拉利"></property>
<property name="color" value="红色"></property>
</bean>
<bean name="user1" class="cn.hd.injection.User">
<constructor-arg name="name" value="李超杰" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg name="car" ref="car" index="1"></constructor-arg>
</bean>
<bean name="user2" class="cn.hd.injection.User" p:age="12" p:name="tianweilin" p:car-ref="car"></bean>
<bean name="user3" class="cn.hd.injection.User">
<property name="name" value="#{user2.name}"></property>
<property name="car" value="#{car}"></property>
</bean>
<bean name="collection" class="cn.hd.injection.CollectionBean">
<!--数组中的元素那么他的配置和基本类型一样-->
<!--<property name="arr" value="1"></property>-->
<property name="arr">
<array>
<!--数组中直接存值-->
<value>张三</value>
<value>李四</value>
<!--数组中存入对象-->
<ref bean="car"></ref>
</array>
</property>
<!--如果list集合中只有一个元素配置和基本类型一样-->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<ref bean="car"></ref>
</list>
</property>
<property name="map">
<map>
<entry key="name" value="王二"></entry>
<entry key="user" value-ref="user"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="driverClass">com......</prop>
<prop key="jdbcUrl">jdbc://localhost:3306/canteen</prop>
<prop key="user">root</prop>
</props>
</property>
</bean>
</beans>
1. 配置bean元素
<!--
配置bean元素
name给对象起个名字
class类的完整路径名
id:唯一 里面不出现特殊字符 struts "/sasdsa"
-->
<bean name="user"class="cn.hd.test.User" id="user"></bean>
2. 创建方式
<!--
scope 对象的创建方式
singleton:单例模式(默认)
prototype:多例模式,什么时候用什么时候创建,并且创建的方式每次重新new
了解:坚决不用
requiest :javaweb环境下 对象的生命周期和requset一样
session :web环境下 对象的生命周期和session一样
-->
<bean name="user" class="cn.hd.test.User"id="user" scope="prototype"></bean>
一般情况下全都用默认的。
多例模式,在整合struts2的时候Action必须使用多例模式
3. 生命周期的方法
在对象创建后马上执行一个方法
对象销毁前执行一个方法
<bean name="user" class="cn.hd.test.User"id="user" init-method="init"destroy-method="destroy"></bean>
在User类中,书写init和destroy方法
测试,可以手动的销毁对象,调ClassPathXmlApplicationContext的clos方法
注意:销毁对象,对象必须是单例的,如果是多例的无法手动销毁
对象属性的注入
(1) set注入(最重要的)
基本属性 8大基本类型+String
<bean name="car" class="cn.hd.injection.Car">
<property name="name" value="法拉利"></property>
<property name="color" value="红色"></property>
</bean>
name类属性名 value属性值
引用属性 对象
<bean name="user" class="cn.hd.injection.User">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<property name="car" ref="car"></property>
</bean>
name类属性名 ref引用的bean的name
(2) 构造函数注入
因为构造函数会有很多种情况,例如传参的顺序啊 传参的类型啊 所以Spring也有对应得到属性
<bean name="user1" class="cn.hd.injection.User">
<constructor-arg name="name" value="李超杰" index="0"type="java.lang.String"></constructor-arg>
<constructor-arg name="age" value="15"index="1"></constructor-arg>
<constructor-arg name="car" ref="car"index="2"></constructor-arg>
</bean>
name | value(ref) | index | type |
属性 | 具体的值
| 参数的位置 | 参数的类型 |
(3) p空间注入(最简单)但是业界不认可,还是使用<property>
导入约束
xmlns:p="http://www.springframework.org/schema/p"
(4) spel注入类似于EL和OGNl表达式
相当于复制的功能
<bean name="user3" class="cn.hd.injection.User">
<property name="name" value="#{user2.name}"></property>
<property name="car" value="#{car}"></property>
</bean>
复杂类型的注入
数组
<!--如果数组中的元素只有一个,那么他的配置和基本配置一样-->
<!-- <property name="arr"value="1"></property>-->
<property name="arr">
<array>
<value>赵赛昌</value>
<value>李超杰</value>
<ref bean="car"></ref>
</array>
</property>
List/set
<!--list集合中只有一个元素配置和基本配置一样-->
<property name="list">
<list>
<value>白学光</value>
<value>张永镇</value>
<ref bean="user1"></ref>
</list>
</property>
Map
键值对 key key-ref value value-ref map<K,V>
<property name="map">
<map>
<entry key="name" value="卢佳威"></entry>
<entry key="car" value-ref="car"></entry>
<!--<entry key-ref=""></entry>-->
</map>
</property>
Properties
<property name="properties">
<props>
<prop key="driverClass">com.</prop>
<prop key="jdbcUrl">jdbc://localhost:3306/hello</prop>
<prop key="user">root</prop>
</props>
</property>
模块化
<import resource="classpath:applicationContext.xml"></import>
resource:其他配置文件的地址