Spring

Spring

  1.  为什么要用Spring框架
    1. 企业级系统
      • 大规模:用户数量多、数据规模大、功能众多
      • 性能和安全要求高
      • 业务复杂
      • 灵活应变
  2. EJB的不足
    1. 学习比较难,开发难度大
    2. 依赖应用服务器
    3. 运用大量的设计模式
  3. Spring的优势
    1. 引入spring之后,spring的依赖注入可以统一管理和生成javabean,哪有需要调用就往哪注入,这种方式大大降低了开发难度
    2. 降低了代码的耦合度,给后期的维护也带来了方便
    3. 同时spring的aop还能将系统中那些类似于日志管理,事务等分布性比较强,但又必须有的代码集中生成,无需开发人员关注,提高工作效率
  4. 什么是Spring
    1. Spring是轻量级框架, Java EE的春天,当前主流框架
    2. 目标
      • 使现有技术更加易用,推进编码最佳实践
    3. 内容
      • IoC容器
      • AOP实现
      • 数据访问支持
      • 简化JDBC/ORM 框架
      • 声明式事务
      • Web集成
  5. Spring体系结构
  6. Spring设计理念
    1. Spring是面向Bean的编程
  7. Spring 两大核心技术
    1. 控制反转(IoC:Inversion of Control ) /依赖注入(DI:Dependency Injection )
    2. 面向切面编程(AOP:Aspect Oriented Programming)
  8. Spring的优点
    1. 低侵入式设计:非入侵式设计,基于Spring开发的应用一般不依赖于Spring的类
    2. 独立于各种应用服务器,真正实现:一次编写,到处运行。
    3. Spring的依赖注入特性使Bean与Bean之间的依赖关系变的完全透明,降低了耦合度:使用SpringIOC容器,将对象之间的依赖关系交给Spring,降低组件之间的耦合性,让我们更专注于应用逻辑
    4. 它的面向切面编程特性允许将一些通用任务如安全、事务、日志等进行集中式处理
    5. 并且它还提供了与第三方持久层框架的良好整合,并简化了底层数据库访问
    6. 高度的开放性(可以和Struts2、Hibernate、MyBatis、CXF等很多主流第三方框架无缝整合)
  9. IOC控制反转 / 依赖注入
    1. 将组件对象的控制权从代码本身转移到外部容器
    2. 组件化的思想:分离关注点,使用接口,不再关注实现
    3. 依赖的注入:将组件的构建和使用分开
  10. Spring的头部信息
    1. <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">

  11. 实体类配置

    1. <!-- 通过bean元素声明需要Spring创建的实例。该实例的类型通过class属性指定,并通过id属性为该实例指定一个名称,以便在程序中使用 -->

      <bean id="helloSpring" class="cn.springdemo.HelloSpring">

      <!-- property元素用来为实例的属性赋值,此处实际是调用setWho()方法实现赋值操作 -->

      <property name="who">

      <!-- 此处将字符串"Spring"赋值给who属性 -->

      <value>Jack</value>

      </property>

      </bean>

  12.  

    Java具体实现

    1. 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;
      	}
      
      }

       

    2. 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;
      	}
      
      }

       

  13. 使用控制反转/依赖注入有哪些好处?
  14. 什么是“简单工厂模式”?有什么优点?
  15. 使用Spring IoC的步骤是什么?
  16. AOP
    1. AOP目标:让我们可以专心做业务功能实现。
    2. 将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决。
    3. 采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。
  17. AOP相关术语
    1. 增强处理(Advice)
      • 前置增强
      • 后置增强
      • 环绕增强、异常抛出增强、最终增强等类型
    2. 切入点(Pointcut)
    3. 连接点(Join Point)
    4. 切面(Aspect)
    5. 目标对象(Target object)
    6. AOP代理(AOP proxy)
    7. 织入(Weaving)
  18. 新增的jar包:
  19. xml配置文件头信息
    1. <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">

  20. 定义切入点

    1. 织入增强处理
      • 织入:在切入点插入增强处理
      • <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>

    2. 切入点:简单的说,就是连接点的查询条件

      • <aop:config>

        <aop:pointcut id="pointcut"

                    expression="execution(public void addNewUser(entity.User))"/>

        </aop:config>

    3. 表达式的匹配规则

      • public * addNewUser(entity.User): “*”表示匹配所有类型的返回值。
        public void *(entity.User): “*”表示匹配所有方法名。
        public void addNewUser(..): “..”表示匹配所有参数个数和类型。
        * com.service.*.*(..):匹配com.service包下所有类的所有方法。
        * com.service..*.*(..):匹配com.service包及其子包下所有类的所有方法

         

      • 类名称使用了是完全限定名称
      • 通常会使用包的统配作为匹配规则
  21. 使用p命名空间注入属性值
    1. p 命名空间的特点:使用属性而不是子元素的形式配置Bean的属性,从而简化了配置代码
      1. 对于直接量(基本数据类型、字符串)属性:p:属性名="属性值"

      2. 对于引用Bean的属性:p:属性名-ref="Bean的id"

    2. 使用前要先要在Spring配置文件中引入p命名空间
      1. xmlns:p="http://www.springframework.org/schema/p"

    3. 使用p命名空间注入属性值

      1. <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" />

  22. 注入不同数据类型
  23. 小结,Spring注入方式
    1. 设值注入
    2. 构造注入
    3. p命名空间注入
  24. 异常抛出增强的特点
    1. 在目标方法抛出异常时织入增强处理
    2. 可拔插的异常处理方案
    3. <aop:after-throwing>元素:定义异常抛出增强

    4. <aop:aspect ref="theLogger">

              <aop:after-throwing method="afterThrowing"

                      pointcut-ref="pointcut" throwing="e" />

      </aop:aspect>

  25. 最终增强的特点

    1. 无论方法是否抛出异常,都会在目标方法最后织入增强处理,即:该增强都会得到执行
    2. 类似于异常处理机制中finally块的作用,一般用于释放资源
    3. 可以为各功能模块提供统一的,可拔插的处理方案
    4. <aop:after>元素:定义最终增强
  26. 环绕增强的特点
    1. 目标方法前后都可织入增强处理
    2. 功能最强大的增强处理
    3. 可获取或修改目标方法的参数、返回值,可对它进行异常处理,甚至可以决定目标方法是否执行
    4. <aop:around>元素:定义环绕增强
  27. 常用增强处理类型
    1. 增强处理类型

      特  点

      Before

      前置增强处理,在目标方法前织入增强处理

      AfterReturning

      后置增强处理,在目标方法正常执行(不出现异常)后织入增强处理

      AfterThrowing

      异常增强处理,在目标方法抛出异常后织入增强处理

      After

      最终增强处理,不论方法是否抛出异常,都会在目标方法最后织入增强处理

      Around

      环绕增强处理,在目标方法的前后都可以织入增强处理

  28. Spring AOP配置元素
    1. 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注解驱动的切面

  29. Spring注解实现IOC和AOP
  30. 注解方式将Bean的定义信息和Bean实现类结合在一起,Spring提供的注解有
    1. @Component :实现Bean组件的定义
    2. @Repository :用于标注DAO类
    3. @Service :用于标注业务类
    4. @Controller :用于标注控制器类
  31. 使用注解的示例

    1. @Repository("userDao")

      public class UserDaoImpl implements UserDao {

      }

    2. 与在XML配置文件中编写 <bean id="userDao"  class="dao.impl.UserDaoImpl" /> 等效
  32. 使用@Autowired注解实现Bean的自动装配,默认按类型匹配,可以使用@Qualifier指定Bean的名称
    1. @Service("userService") 
      public class UserServiceImpl implements UserService { 
      	@Autowired
      	@Qualifier("userDao")
      	private UserDao dao; 
      	…… 
      }

       

    2. 也可以用方法用参数进行标注
    3. @Service("userService") 
      public class UserServiceImpl implements UserService { 
      	private UserDao dao;
      	@Autowired
      	public void setDao(@Qualifier("userDao") UserDao dao) {
      	   this.dao = dao;
      	} 
        …… 
      }

       

  33. 使用注解信息启动注解容器
    1. <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>

  34. 使用@Resource注解实现组件装配,默认按名称匹配
    1. @Service("userService")

      public class UserServiceImpl implements UserService {

      @Resource(name = "userDao")

      private UserDao dao;

      ……

      }

    2. @Service("userService")

      public class UserServiceImpl implements UserService {

      @Resource

      private UserDao dao;

      ……

      }

  35. 使用注解定义切面

    1. 使用注解定义前置增强和后置增强实现日志功能
      • @Aspect
      • @Before
      • @AfterReturning
      • 编写Spring配置文件,完成切面织入
        1. <aop:aspectj-autoproxy />:启用对于@AspectJ注解的支持
      • 使用@AfterThrowing 注解定义异常抛出增强
      • 使用@After注解定义最终增强
      • 使用@Around注解定义环绕增强
  36. 总结
    1. 使用注解方式定义切面可以简化配置工作量
    2. 常用注解有@Aspect、@Before、@AfterReturning、@Around、@AfterThrowing、@After等
    3. 在配置文件中添加<aop:aspectj-autoproxy />元素,启用对于@Aspect注解的支持

觉得有点用记得给我点赞哦!

这篇文章就到这里啦,如果觉得有收获的话,不妨给一个赞喔;文章中涉及的内容肯定是不全面的,欢迎大家评论区留言补充。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值