业务层框架Spring
1.Spring程序开发的基本步骤:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
251.准备好Spring开发的jar包,至少需要:
spring-aop.jar 使用Spring的AOP特性时所需的jar类库
spring-beans.jar 包含访问配置文件、创建和管理bean 以及进行IoC/DI操作相关的所有类
spring-context.jar 为Spring核心提供了大量扩展。例如,可以找到使用Spring ApplicationContext
特性时所需的全部类,JDNI所需的全部类,以及校验Validation方面等相关类.
spring-core.jar Spring框架的核心类库,Spring各个组件要都要使用到这个包里的类
spring-expression.jar Spring表达式语言需要的类库
可以下载第三方提供的日志jar commons-logging.jar,打印日志
2.开发工具:Eclipse+插件Spring Tool Suite,或者直接下载STS工具
3.创建实体类、配置文件(applicationContext.xml)、编写测试类Test
<!-- 配置文件 applicationContext.xml,通过<bean>标签实现对象的赋值 -->
<?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="唯一标识符" class="包名+类名">
<!-- 配置属性: value 值 -> name 字段 -->
<property name="属性" value="属性值"></property>
<property name="属性名" ref="引用对象的id值"></property>
</bean>
</beans>
2.Spring的特点(获取对象不需要new,直接从IOC容器拿。前提是要在springIOC中存放对象并赋值)1
2
3
4
5
6//1.创建Spring的IOC容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicatoinContext.xml");
//2.从IOC容器中获取Bean实例(id为"student"的Student对象)
//相当于 Student stu = new Student();
Student stu =(Student)context.getBean("student");
//继续操作
3.spring的学习内容1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 IOC(控制反转):将创建对象、属性值的方式进行了翻转,从new、setXxx()翻转为 从SpringIOC 容器getBean()获取。
DI(依赖注入):将属性值注入给属性,将属性注入给bean,将bean注入给IOC容器。
IOC容器赋值:基本类型 用 value=" "赋值;对象类型:ref =" 需要引用的对象的id值 ",实现对象与对象之间的依赖关系。
依赖注入的赋值方式:
1.setter注入((底层通过反射实现) 默认使用setter方法()给属性赋值 <property>标签
2.构造器注入:通过构造器注入 <constructor-arg>标签,其内部的属性赋值顺序需要和构造器参数的顺序一致;
如果不一致,则需要通过type、index或name指定顺序。
3.p命名空间注入:①引入命名空间:xmlns:p="http://www.springframework.org/schema/p"
②<bean>标签里面添加:简单类型(8大类型+String)(p:属性名="属性值")
引用类型(p:属性名-ref="引用对象的id")
③ 注入各种集合数据类型(List、Set、Map、properties) 都有各自对应的标签使用
List或Array 外层用<list>;内层用<value>或<ref>
Set 外层用<set>;内层用<value>或<ref>
Map 外层用<map>;中间层用<entry>;内层中键用<key><value>…</value></key>,
值用<value>…</value>或<ref>…</ref>
Properties 外层用<props>;内层中键写在<prop key=”..”>..</prop>的key值中,
值写在<prop>..</prop>中间。Properties中的键和值通常都是字符串类型。
自动装配: 自动装配 (只适用于ref类型)
Course类中有一个ref属性propertyName(属性名),并且ioc容器中也有一个bean为的id也ropertyName
autowire="byName":自动寻找其他bean的id值 = 该Course的属性名
autowire="byType":寻找其他bean的类型(class) = 该Course类的ref属性类型(只能有一个满足要求,否则报错)
autowire="constructor":寻找其他bean的类型(class) = 该Course类的构造方法参数(本质和byType一样)
<bean id="Course" class="包名+类名" autowire="byName或者byType或constructor">
<property name="..." value="..."></property>
<!-- <property name="..." ref="..."></property> -->
</bean>
注:<beans xmlns="http://www.springframework.org/schema/beans" ... default-autowire="byName">
可以一次性将该ioc容器中的所有bean统一设置成自动装配
注解定义bean:实体类添加注解,Spring在启动时,会根据base-package在改包中扫描所有类,查找这些类是否有注解。
如果有,则将该类加入spring ioc 容器。application.xml下添加:
<context:component-scan base-package="实体类所在包名"></context:component-scan>
常见注解(@Component)分为: dao层注解(@Repository)、service层注解(@Service)、控制器注解(@Controller)
AOP(面向切面编程):①前置通知 覆盖before()方法 目标方法执行前发生。
②后置通知 覆盖afterReturning()方法 目标方法执行后发生
③异常通知 无需覆盖方法 目标方法发生异常时执行
④最终通知 目标方法执行完毕后,插入的通知(不论是正常返回还是异常退出)。
④环绕通知 覆盖invoke()方法 拦截对目标方法调用,即调用目标方法的整个过程(功能最强大)
Spring整合Mybatis:将Mybatis操作Dao层的权力交给Spring,Spring与其他什么框架整合的时候,权力全部
交由Spring掌控,Spring是老大。