Spring概述及思想及实现方法总结

Spring概述

:一个开源的轻量级的容器型框架,可在容器中对项目的所有对象进行管理,应用在web开发中的业务逻辑层,有两大核心思想,aop和ioc。
Aop即Aspect Oriented Programming,意为面向切面编程,可以在不改变源码的情况下扩展功能,利用的是Java中的动态代理机制,即反射机制,另一个优点是帮助我们在写代码时只需考虑主要流程,而不用考虑那些不重要的流程,事后使用AOP就可以隔山打牛的给所有取款流程加上验证这一子流程。
Ioc即Inversion of Control,控制反转,指new实例工作不由程序员来做,而是交给Spring容器来做。在Spring中BeanFactory是IOC容器的实际代表者。

Spring的4大核心模块

(1)core(2)beans(3)context(4)expression

Spring的2个容器

(1)BeanFactory(2)ApplicationContext
ApplicationContext 是 BeanFactory 的子接口,也被成为 Spring 上下文,包含 BeanFactory 所有的功能,有更多的实现方法。

Bean的5个作用域

:<bean id="..." class="..." scope="singleton">

(1)Singleton:在Spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值。Singleton是单例类型,创建容器时就同时自动创建了一个bean对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。
(2)Prototype:每次从容器中调用Bean时,都返回一个新的实例。Prototype是原型类型,创建容器时并未实例化,获取bean时才会创建一个对象,每次获取到的对象都不是同一个对象。
(3)Request(4)session(5)global-session

Bean的生命周期:

1、实例化一个Bean-也就是我们常说的new;
2、按照Spring上下文对实例化的Bean进行配置-也就是IOC注入;
3、如果这个Bean已经实现了BeanNameAware接口,会调用它实现的setBeanName(String)方法,传递Bean的id;
4、如果这个Bean已经实现了BeanFactoryAware接口,会调用它实现的setBeanFactory(setBeanFactory(BeanFactory),传递的是Spring工厂自身;
5、如果这个Bean已经实现了ApplicationContextAware接口,会调用setApplicationContext(ApplicationContext)方法,传入Spring上下文;
6、如果这个Bean关联了BeanPostProcessor接口,将会调用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor经常被用作是Bean内容的更改,由于是在Bean初始化结束时调用的方法,也可以被应用于内存或缓存技术;
7、如果Bean在Spring配置文件中配置了init-method属性会自动调用其配置的初始化方法。
8、如果这个Bean关联了BeanPostProcessor接口,将会调用postProcessAfterInitialization(Object obj, String s)方法;
9、当Bean不再被需要时,会进行清理阶段,如果Bean实现了DisposableBean这个接口,会调用其实现的destroy()方法;
10、最后,如果这个Bean的Spring配置中配置了destroy-method属性,会自动调用其配置的销毁方法。

Bean的继承

<bean...parent=""> 子bean可以从父bean中继承所需配置;

Bean的模板

<bean id=" " abstract="true"> 仅作为父bean使用,不可实例化;
Spring依赖注入集合:

  <property name="List_Set">
     <list>
        <value>INDIA</value>
     </list>
  </property>
  <property name="Map">
     <map>
        <entry key="1" value="INDIA"/>
     </map>
  </property>
  <property name="Prop">
     <props>
        <prop key="one">INDIA</prop>
     </props>
  </property>

Spring基于注解的配置:

(1)导入aop包;
(2)引入context约束;
(3)开启注解扫描<context:component-scan base-package=””>
(4)@Component 创建对象 类上
@Component的3个衍生注解@Controller@Service@Repository
(5)@Required 应用于bean属性的setter方法,表明bean属性在配置时必须放在配置文件中;
(6)@Autowired 注入属性时 自动装配;
(7)@Configuration 配置类;
(8)@Bean 与@Configuration配套使用;
(9)@Resource 注入属性时 自动装配 @Resource(name=”类名”);

Aop术语

:(1)Pointcut(2)advice(3)joinpoint(4)aspect(5)target(6)proxy(7)weaving(8)introduction 切面运用到目标类的过程;

Aop的XML实现:

(1)导包(1)aopalliance(2)spring-aop(3)aspectjwear(4)spring-aspects
(2)引入aop约束
(3)配置切入点
(4)配置切面:

<aop:config>
      <aop:aspect id=" " ref=" ">
         <aop:pointcut id="A" expression="execution(* com.tutorialspoint.*.*(..))"/>
         <aop:before pointcut-ref="A" method="beforeAdvice"/>
         <aop:after-returning pointcut-ref="A" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="AfterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>

Aop的注解实现:

(1)导包(1)aopalliance(2)spring-aop(3)aspectjwear(4)spring-aspects
(2)引入aop约束
(3)开启aop代理 aop:aspectj-autoproxy
(4)使用注解在增强类上@Aspect在增强方法上@Before(value=“execution(* cn.xx.xx.*(…))”)

Spring声明式事务管理:

(1)引入依赖 aop、tx
(2)配置数据源

 <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
      <property name="username" value="root"/>
      <property name="password" value="123"/>
   </bean>

(3)配置事务管理器

   <bean id="transactionManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource"  ref="dataSource" />    
   </bean>

(4)配置事务增强

   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
      <tx:method name="create" propagation=”” />
      </tx:attributes>
   </tx:advice>

(5)配置切面

   <aop:config>
      <aop:pointcut id="createOperation" 
      expression="execution(* com.tutorialspoint.StudentJDBCTemplate.create(..))"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation"/>
   </aop:config>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值