详细介绍可参考博客:https://blog.csdn.net/yerenyuan_pku/article/details/69663685
☆什么是Spring
Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由Rod Johnson在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为J2EE应用程序开发提供集成的框架。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式)轻量级开源框架。
☆Spring基本特征
- Spring是一个非常活跃的开源框架;它是一个基于Core来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.
- Spring以一种非侵入式的方式来管理你的代码,Spring提倡”最少侵入”,这也就意味着你可以适当的时候安装或卸载Spring
☆开发spring所需要的工具
1>Spring的jar包
到http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
spring的核心类库 在spring文档的dist下
dist\spring.jar
引入的第三方类库 都spring文档的lib下
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
2>Spring配置文件
默认情况下是applicationContext.xml文件。可以建立很多xml文件,工程中一般都是这样配置的
☆Spring基本功能详解
①SpringIOC
Spring的控制反转:把对象的创建、初始化、销毁等工作交给spring容器来做。由spring容器控制对象的生命周期。
使用步骤
-
启动spring容器
方式一:在类路径下寻找配置文件来实例化容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});可以在整个类路径中寻找xml文件 * 通过这种方式加载。需要将spring的配置文件放到当前项目的classpath路径下 * classpath路径指的是当前项目的src目录,该目录是java源文件的存放位置。 方式二:在文件系统路径下寻找配置文件来实例化容器 ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});Spring的配置文件可以指定多个,可以通过String数组传入。 注:经常用第一种方法启动容器
-
从spring容器中把对象提取出来
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld"); helloWorld.hello();
☆创建对象的方式
1、无参构造函数
<bean id="helloWorld"
class="com.albin.spring.ioc.createobject.method.HelloWorld"></bean>
2、静态工厂
<!--
引入静态工厂类
factory-bean 指明工厂bean
-->
<bean id="helloWorld2"
class="com.albin.spring.ioc.createobject.method.HelloWorldFactory"
factory-method="getInstance"></bean>
3、实例工厂
<!--
引入实例工厂
-->
<bean id="helloWorldFactory"
class="com.albin.spring.ioc.createobject.method.HelloWorldFactory2"></bean>
<!--
factory-bean 指向工厂bean
factory-method 工厂方法
-->
<bean id="helloWorld3"
factory-bean="helloWorldFactory"
factory-method="getInstance"></bean>
延迟加载 lazy-init="true"
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="cn.albin.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“
别名
<bean id="helloWorld" class="com.albin.spring.ioc.alias.HelloWorld"></bean>
<alias name="helloWorld" alias="王二麻子"/>
<alias name="helloWorld" alias="狗蛋"/>
对象的scope
-
singleton(默认值)
在每个Spring IoC容器中一个bean定义只有一个对象实例(共享)。 默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如: <bean id="xxx" class="cn.albin.OrderServiceBean" lazy-init="true"/> 如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下: <beans default-lazy-init="true“ ...>
-
prototype
允许bean可以被多次实例化(使用一次就创建一个实例) . Spring不能对一个prototype bean的整个生命周期负责.这就意味着清楚prototype作用域的对象并释放任何prototype bean所持有的昂贵资源都是客户端的责任。
初始化bean时机
Spring默认在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化的一部分,ApplicationContext会自动创建并配置所有的singleton bean.通常情况下这是件好事。因为这样在配置中有任何错误能立即发现。
Lazy-init=”true or false”
Lazy-init 为false,spring容器将在启动的时候报错(比较好的一种方式)
Lazy-init 为true,spring容器将在调用该类的时候出错。
init、destroy
Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。
<bean id=“foo” class=“...Foo”
init-method=“setup”
destory-method=“teardown”/>
当foo被载入到Spring容器中时调用init-method方法。当foo从容器中删除时调用destory-method(scope = singleton有效)
//销毁容器
ClassPathXmlApplicationContext applicationContext = (ClassPathXmlApplicationContext)context;
applicationContext.close();
依赖注入 xml 版
public class Person {
private Long pid;
private String name;
private List list;
private Set set;
private Map map;
private Properties properties;
private Student student;
ApplicationContext.xml
<!--
把person放入到spring容器中
-->
<bean id="person" class="com.albin.spring.di.xml.setter.Person"
init-method="init"
lazy-init="true">
<!--
property用来描述person的属性
name属性代表属性的名称
value属性代表属性的值 属性为基本类型
因为student是引用类型,所以用ref赋值
-->
<property name="pid" value="2"></property>
<property name="name" value="王二麻子"></property>
<property name="student" ref="student"></property>
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>
<property name="map">
<map>
<entry key="entry1">
<value>entry1</value>
</entry>
<entry key="entry2">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="prop1">prop1</prop>
<prop key="prop2">prop2</prop>
</props>
</property>
</bean>
<!--
把student放入到spring容器中
-->
<bean id="student"
class="com.albin.spring.di.xml.setter.Student"></bean>