1、Spring概述
①Spring是一个开源框架
②Spring为简化企业级开发而生,使用Spring开发可以将Bean对象,Dao组件对象,Service组件对象等交给Spring容器来管理,这样使得很多复杂的代码在Spring中开发却变得非常的优雅和简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。
③Spring是一个IOC(DI)和AOP容器框架。
④Spring的优良特性
- [1] 非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API
- [2] 控制反转:IOC——Inversion of Control,指的是将对象的创建权交给Spring去创建。使用Spring之前,对象的创建都是由我们自己在代码中new创建。而使用Spring之后。对象的创建都是由给了Spring框架。
- [3]依赖注入:DI——Dependency Injection,是指依赖的对象不需要手动调用setXX方法去设置,而是通过配置赋值。
- [4]面向切面编程:Aspect Oriented Programming——AOP
- [5]容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
- [6]组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。
- [7]一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)
2. 使用
- 先在pom.xml中添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
- 实现类的创建–利用配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
ClassPathXmlApplicationContext bean = new ClassPathXmlApplicationContext("bean.xml");读入xml后就 完成了对象的创建
默认是通过无参构造
id -变量名
class - 类的路径
property -给属性赋值,用set注入
-->
<bean id="userservice" class="com.csf.service.UserServiceImpl">
<property name="userDao" ref="Userhello2"></property>
</bean>
<bean id="Userhello2" class="com.csf.Dao.Userhello2"/>
<!--
有参构造,通过变量名
-->
<bean id="user" class="com.csf.pojo.User">
<constructor-arg name="name" value="陈世枫">
</constructor-arg>
<constructor-arg name="age" value="21">
</constructor-arg>
</bean>
<!--
有参构造,通过下标
-->
<bean id="user2" class="com.csf.pojo.User">
<constructor-arg index="0" value="林秋婷">
</constructor-arg>
<constructor-arg index="1" value="15">
</constructor-arg>
</bean>
<!--起别名-->
<alias name="user" alias="usercopy"></alias>
<!--Bean配置-->
<bean id="user23" class="com.csf.pojo.User" name="我也是别名,我是第二个别名.....">
</bean>
</beans>
3.常见属性的设置
- 基本数据类型
- 引用类型
- 数组
- 集合
- Map
- 空指针
- properties
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="date" class="java.util.Date" >
<!-- <property name="date" value=""></property>-->
</bean>
<bean name="stu" class="com.csf.pojo.Student">
// <!--基本数据类型注入-->
<property name="age" value="12"></property>
// <!--引用类型注入注入-->
<property name="date" ref="date"></property>
//<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
// <!--集合注入-->
<property name="hobbys">
<list>
<value>看书</value>
<value>看电视</value>
</list>
</property>
// <!--空指针注入-->
<property name="wife">
<null></null>
</property>
// <!--MAP注入-->
<property name="card">
<map>
<entry key="lqt" value="csf"></entry>
</map>
</property>
//数组注入
<property name="info">
<props>
<prop key=" 学号"> 202000</prop>
<prop key=" url"> baidu.com</prop>
</props>
</property>
</bean>
</beans>