Spring
- 为什么要用Spring框架
- 企业级系统
- 大规模:用户数量多、数据规模大、功能众多
- 性能和安全要求高
- 业务复杂
- 灵活应变
- 企业级系统
- EJB的不足
- 学习比较难,开发难度大
- 依赖应用服务器
- 运用大量的设计模式
- Spring的优势
- 引入spring之后,spring的依赖注入可以统一管理和生成javabean,哪有需要调用就往哪注入,这种方式大大降低了开发难度
- 降低了代码的耦合度,给后期的维护也带来了方便
- 同时spring的aop还能将系统中那些类似于日志管理,事务等分布性比较强,但又必须有的代码集中生成,无需开发人员关注,提高工作效率
- 什么是Spring
- Spring是轻量级框架, Java EE的春天,当前主流框架
- 目标
- 使现有技术更加易用,推进编码最佳实践
- 内容
- IoC容器
- AOP实现
- 数据访问支持
- 简化JDBC/ORM 框架
- 声明式事务
- Web集成
- Spring体系结构
- Spring设计理念
- Spring是面向Bean的编程
- Spring 两大核心技术
- 控制反转(IoC:Inversion of Control ) /依赖注入(DI:Dependency Injection )
- 面向切面编程(AOP:Aspect Oriented Programming)
- Spring的优点
- 低侵入式设计:非入侵式设计,基于Spring开发的应用一般不依赖于Spring的类
- 独立于各种应用服务器,真正实现:一次编写,到处运行。
- Spring的依赖注入特性使Bean与Bean之间的依赖关系变的完全透明,降低了耦合度:使用SpringIOC容器,将对象之间的依赖关系交给Spring,降低组件之间的耦合性,让我们更专注于应用逻辑
- 它的面向切面编程特性允许将一些通用任务如安全、事务、日志等进行集中式处理
- 并且它还提供了与第三方持久层框架的良好整合,并简化了底层数据库访问
- 高度的开放性(可以和Struts2、Hibernate、MyBatis、CXF等很多主流第三方框架无缝整合)
- IOC控制反转 / 依赖注入
- 将组件对象的控制权从代码本身转移到外部容器
- 组件化的思想:分离关注点,使用接口,不再关注实现
- 依赖的注入:将组件的构建和使用分开
- Spring的头部信息
-
<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-3.2.xsd">
-
-
实体类配置
-
<!-- 通过bean元素声明需要Spring创建的实例。该实例的类型通过class属性指定,并通过id属性为该实例指定一个名称,以便在程序中使用 -->
<bean id="helloSpring" class="cn.springdemo.HelloSpring">
<!-- property元素用来为实例的属性赋值,此处实际是调用setWho()方法实现赋值操作 -->
<property name="who">
<!-- 此处将字符串"Spring"赋值给who属性 -->
<value>Jack</value>
</property>
</bean>
-
-
Java具体实现
-
public class Talk { private People people; public void method() { System.out.println(people.getName() + " 说: " + people.getSay()); } public People getPeople() { return people; } public void setPeople(People people) { this.people = people; } }
-
public class People { /** * 人的姓名 */ private String name; /** * 说的内容 */ private String say; public People() { super(); } public People(String name, String say) { super(); this.name = name; this.say = say; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSay() { return say; } public void setSay(String say) { this.say = say; } }
-
- 使用控制反转/依赖注入有哪些好处?
- 什么是“简单工厂模式”?有什么优点?
- 使用Spring IoC的步骤是什么?
- AOP
- AOP目标:让我们可以专心做业务功能实现。
- 将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决。
- 采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。
- AOP相关术语
- 增强处理(Advice)
- 前置增强
- 后置增强
- 环绕增强、异常抛出增强、最终增强等类型
- 切入点(Pointcut)
- 连接点(Join Point)
- 切面(Aspect)
- 目标对象(Target object)
- AOP代理(AOP proxy)
- 织入(Weaving)
- 增强处理(Advice)
- 新增的jar包:
- xml配置文件头信息
-
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
-
-
定义切入点
- 织入增强处理
- 织入:在切入点插入增强处理
-
<aop:config>
<aop:pointcut id="pointcut"
expression="execution(public void addNewUser(entity.User))" />
<aop:aspect ref="userServiceLogger">
<aop:before method="before"
pointcut-ref="pointcut"></aop:before>
<aop:after-returning method="afterReturning"
pointcut-ref="pointcut" returning="result"/>
</aop:aspect>
</aop:config>
-
切入点:简单的说,就是连接点的查询条件
-
<aop:config>
<aop:pointcut id="pointcut"
expression="execution(public void addNewUser(entity.User))"/>
</aop:config>
-
-
表达式的匹配规则
-
public * addNewUser(entity.User): “*”表示匹配所有类型的返回值。 public void *(entity.User): “*”表示匹配所有方法名。 public void addNewUser(..): “..”表示匹配所有参数个数和类型。 * com.service.*.*(..):匹配com.service包下所有类的所有方法。 * com.service..*.*(..):匹配com.service包及其子包下所有类的所有方法
- 类名称使用了是完全限定名称
- 通常会使用包的统配作为匹配规则
-
- 织入增强处理
- 使用p命名空间注入属性值
- p 命名空间的特点:使用属性而不是子元素的形式配置Bean的属性,从而简化了配置代码
-
对于直接量(基本数据类型、字符串)属性:p:属性名="属性值"
-
对于引用Bean的属性:p:属性名-ref="Bean的id"
-
- 使用前要先要在Spring配置文件中引入p命名空间
-
xmlns:p="http://www.springframework.org/schema/p"
-
-
使用p命名空间注入属性值
-
<bean id="user" class="entity.User" p:age="23" p:username="张三" p:email="zhangsan@xxx.com" />
<bean id="userService" class="service.impl.UserServiceImpl" p:dao-ref="userDao" />
-
- p 命名空间的特点:使用属性而不是子元素的形式配置Bean的属性,从而简化了配置代码
- 注入不同数据类型
- 小结,Spring注入方式
- 设值注入
- 构造注入
- p命名空间注入
- 异常抛出增强的特点
- 在目标方法抛出异常时织入增强处理
- 可拔插的异常处理方案
-
<aop:after-throwing>元素:定义异常抛出增强
-
<aop:aspect ref="theLogger">
<aop:after-throwing method="afterThrowing"
pointcut-ref="pointcut" throwing="e" />
</aop:aspect>
-
最终增强的特点
- 无论方法是否抛出异常,都会在目标方法最后织入增强处理,即:该增强都会得到执行
- 类似于异常处理机制中finally块的作用,一般用于释放资源
- 可以为各功能模块提供统一的,可拔插的处理方案
- <aop:after>元素:定义最终增强
- 环绕增强的特点
- 目标方法前后都可织入增强处理
- 功能最强大的增强处理
- 可获取或修改目标方法的参数、返回值,可对它进行异常处理,甚至可以决定目标方法是否执行
- <aop:around>元素:定义环绕增强
- 常用增强处理类型
-
增强处理类型
特 点
Before
前置增强处理,在目标方法前织入增强处理
AfterReturning
后置增强处理,在目标方法正常执行(不出现异常)后织入增强处理
AfterThrowing
异常增强处理,在目标方法抛出异常后织入增强处理
After
最终增强处理,不论方法是否抛出异常,都会在目标方法最后织入增强处理
Around
环绕增强处理,在目标方法的前后都可以织入增强处理
-
- Spring AOP配置元素
-
AOP配置元素
描 述
<aop:config>
AOP配置的顶层元素,大多数的<aop:*>元素必须包含在<aop:config>元素内
<aop:pointcut>
定义切点
<aop:aspect>
定义切面
<aop:after>
定义最终增强(不管被通知的方法是否执行成功)
<aop:after-returning>
定义后置增强
<aop:after-throwing>
定义异常抛出增强
<aop:around>
定义环绕增强
<aop:before>
定义前置增强
<aop:aspectj-autoproxy>
启动@AspectJ注解驱动的切面
-
- Spring注解实现IOC和AOP
- 注解方式将Bean的定义信息和Bean实现类结合在一起,Spring提供的注解有
- @Component :实现Bean组件的定义
- @Repository :用于标注DAO类
- @Service :用于标注业务类
- @Controller :用于标注控制器类
-
使用注解的示例
-
@Repository("userDao")
public class UserDaoImpl implements UserDao {
…
}
- 与在XML配置文件中编写 <bean id="userDao" class="dao.impl.UserDaoImpl" /> 等效
-
- 使用@Autowired注解实现Bean的自动装配,默认按类型匹配,可以使用@Qualifier指定Bean的名称
-
@Service("userService") public class UserServiceImpl implements UserService { @Autowired @Qualifier("userDao") private UserDao dao; …… }
- 也可以用方法用参数进行标注
-
@Service("userService") public class UserServiceImpl implements UserService { private UserDao dao; @Autowired public void setDao(@Qualifier("userDao") UserDao dao) { this.dao = dao; } …… }
-
- 使用注解信息启动注解容器
-
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="......
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 扫描包中注解标注的类 -->
<context:component-scan base-package="service,dao" />
</beans>
-
- 使用@Resource注解实现组件装配,默认按名称匹配
-
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource(name = "userDao")
private UserDao dao;
……
}
-
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserDao dao;
……
}
-
-
使用注解定义切面
- 使用注解定义前置增强和后置增强实现日志功能
- @Aspect
- @Before
- @AfterReturning
- 编写Spring配置文件,完成切面织入
- <aop:aspectj-autoproxy />:启用对于@AspectJ注解的支持
- 使用@AfterThrowing 注解定义异常抛出增强
- 使用@After注解定义最终增强
- 使用@Around注解定义环绕增强
- 使用注解定义前置增强和后置增强实现日志功能
- 总结
- 使用注解方式定义切面可以简化配置工作量
- 常用注解有@Aspect、@Before、@AfterReturning、@Around、@AfterThrowing、@After等
- 在配置文件中添加<aop:aspectj-autoproxy />元素,启用对于@Aspect注解的支持
觉得有点用记得给我点赞哦!
这篇文章就到这里啦,如果觉得有收获的话,不妨给一个赞喔;文章中涉及的内容肯定是不全面的,欢迎大家评论区留言补充。