Spring第一章笔记
Spring 概述
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。
Spring 作用
Spring 致力于提供一种方法管理你的业务对象。
Spring 的主要目的是使 JavaEE 易用和促进好编程习惯。
Spring 致力于 J2EE 应用的各层的解决方案,而不是仅仅专注于某一层的方案。
可以说 Spring是企业应用开发的“一站式”选择,并贯穿表现层、业务层及持久层。然而Spring
并不想取代那些已有的框架,而是与它们无缝地整合。
Spring 优点
方便解耦,简化开发
Spring 就是一个大工厂,可以将所有对象创建和依赖关系维护,交给 Spring 管理
AOP 编程的支持
Spring 提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
声明式事务的支持
只需要通过配置就可以完成对事务的管理,而无需手动编程
方便程序的测试
Spring 对 Junit4 支持,可以通过注解方便的测试 Spring 程序
方便集成各种优秀框架
Spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz 等)的直接
支持降低 JavaEE
API 的使用难度Spring 对 JavaEE 开发中非常难用的一些 API(JDBC、JavaMail、远程调用等),都提供了封装,使这些 API 应用难度大大降低
Spring 体系结构
Spring 框架是一个分层架构,它包含一系列的功能要素并被分为大约 20 个模块。这些模块分为 Core Container、Data Access/Integration、Web、AOP(Aspect Oriented Programming)、Instrumentation 和测试部分,如下图所示:
Spring IoC理解
IOC就是控制反转,是指创建对象的控制权的转移,以前创建对象的主动权和时机是由自己把控的,而现在这种权力转移到Spring容器中,并由容器根据配置文件去创建实例和管理各个实例之间的依赖关系,对象与对象之间松散耦合,也利于功能的复用。DI依赖注入,和控制反转是同一个概念的不同角度的描述,即 应用程序在运行时依赖IoC容器来动态注入对象需要的外部资源。
最直观的表达就是,IOC让对象的创建不用去new了,可以由spring自动生产,使用java的反射机制,根据配置文件在运行时动态的去创建对象以及管理对象,并调用对象的方法的。
Spring的IOC有三种注入方式 :构造器注入、setter方法注入、根据注解注入。
IoC让相互协作的组件保持松散的耦合,而AOP编程允许你把遍布于应用各层的功能分离出来形成可重用的功能组件。
什么是依赖注入
在依赖注入中,您不必创建对象,但必须描述如何创建它们。您不是直接在代码中将组件和服务连接在一起,而是描述配置文件中哪些组件需要哪些服务。由 IoC 容器将它们装配在一起。
IoC 的一些好处
它将最小化应用程序中的代码量。
它将使您的应用程序易于测试,因为它不需要单元测试用例中的任何单例或 JNDI 查找机制。
它以最小的影响和最少的侵入机制促进松耦合。
它支持即时的实例化和延迟加载服务。
Spring 创建对象的方法
方式1:无参构造方法创建对象
<bean id="p1" class="com.swjd.bean.Person" scope="singleton" lazy-init="true" init-method="init" destroy-method="xh">
<!--依赖注入 DI-->
<property name="id" value="666"></property>
<property name="name" value="张三"></property>
<property name="sex" value="男"></property>
</bean>
<!--prototype表示多例-->
<bean id="p2" class="com.swjd.bean.Person" scope="prototype">
<!--依赖注入 DI-->
<property name="id" value="666"></property>
<property name="name" value="张三"></property>
<property name="sex" value="男"></property>
</bean>
方法2:用带参的构造方法创建对象
<bean id="p3" class="com.swjd.bean.Person">
<constructor-arg index="0" type="int" value="7788"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="uuu"></constructor-arg>
<constructor-arg index="2" type="java.lang.String" value="男"></constructor-arg>
</bean>
方式3:静态工厂创建对象
<bean id="p4" class="com.swjd.beanFactory.PersonFactory" factory-method="create">
</bean>
//创建person的静态方法
public Person create(){
return new Person(1234,"ccc","男");
// System.out.println("通过静态工厂创建对象");
}
方式4:非静态工厂创建对象
<bean id="p5" class="com.swjd.beanFactory.NoStaticPersonFactory">
</bean>
<bean id="p6" factory-bean="p5" factory-method="create"></bean>
</beans>
Spring 注入方式
第一种:用set方法注入
<bean id="m1" class="com.swjd.bean.Man">
<!--第一种:用set方法注入-->
<property name="id" value="001"></property>
<property name="name" value="hhh"></property>
<property name="age" value="18"></property>
<!--<property name="dog" ref="d1"></property>-->
<property name="dog">
<ref bean="d1"></ref>
</property>
</bean>
第二种:用带参方法注入
<bean id="m2" class="com.swjd.bean.Man">
<!--第二种:用带参方法注入-->
<constructor-arg index="0" type="int" value="002"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="mmm"></constructor-arg>
<constructor-arg index="2" type="int" value="20"></constructor-arg>
<constructor-arg index="3" ref="d1"></constructor-arg>
</bean>
第三种:用p命名空间注入
<bean id="m3" class="com.swjd.bean.Man" p:id="3" p:name="eee" p:age="30" p:dog-ref="d1">
</bean>
第四种:用spel表达式注入
<bean id="m4" class="com.swjd.bean.Man">
<property name="id" value="#{m1.id}"></property>
<property name="name" value="#{m2.name}"></property>
<property name="age" value="#{m3.age}"></property>
<property name="dog" value="#{m1.dog}"></property>
</bean>
Spring 集合注入方式
第一种:用array注入
<property name="hobbies">
<array>
<value>唱歌</value>
<value>跳舞</value>
<value>学习</value>
</array>
</property>
第二种:用list注入
<property name="subject">
<list>
<value>Spring</value>
<value>MyBatis</value>
</list>
</property>
第三种:用map注入
<property name="map">
<map>
<entry key="CN">
<value>中国</value>
</entry>
<entry key="RU">
<value>俄罗斯</value>
</entry>
</map>
</property>
Spring 如何测试
public static void main(String[] args) {
//1.启动Spring容器
ClassPathXmlApplicationContext context=new
ClassPathXmlApplicationContext("applicationContext_stu.xml");
//2.拿对象
System.out.println(context.getBean("s1", Student.class));
}