zoukankan      html  css  js  c++  java
  • spring整理

    spring

    1.核心容器
    core、beans、context、expression、spring-context-support

    2.spring bean
    bean的属性
    1.name 指定唯一的bean的标识符,在基于XML的配置元数据中,可以使用ID/或name属
    性来指定。
    2.socpe 指定由特定的bean定义创建的对象的作用域-》延伸,bean的作用域分类
    (1.sigleton 创建一个实例,ioc容器每次返回同一个实例 2.prototype 创建多个
    实例,每次返回的都是一个新的实例 3.request 仅对HTTP请求产生作用,每次
    HTTP请求都会创建一个新的bean,适用于WebApplicationContext环境。4.仅用于
    HTTP Session,同一个Session共享一个bean实例,不同Session使用不同的实例。
    5.global-session 仅用于HTTP Sessio,所有Session共享同一个bean)
    3.constructor-arg
    4.properties
    5.autowiring mode 以上3,4,5都是用来注入依赖关系的
    6.lazy-initalization mode 懒加载,bean在第一次被请求时才创建,而不是启动时
    就创建bean。
    7.destruction 当包含该bean的容器被销毁时,使用回调方法

    3.spring配置bean的方式
    1.基于XML的配置方式
    2.基于注解的配置
    3.基于JAVA的配置

    4.spring bean的生命周期 @
    1.实例化
    2.属性赋值
    3.初始化
    4.销毁

    5.spring依赖注入
    1.构造方法注入
    2.set方法参数注入
    3.接口注入

    6.spring注入集合
    1.<list> 注入一列值,允许重复
    2.<set> 注入一组值,但不能重复
    3.<map> 用来注入键值对的集合,建和值可以是任何类型
    4.<props> 用来注入键值对的集合,建和值只能是字符串类型

    7.spring beans自动装配(在XML中配置bean即可具体例子如下
    <bean id="textEditor" class="com.tutorialspoint.TextEditor"
    autowire="byName">
    <property name="name" value="Generic Text Editor" />
    </bean>

    1.no 默认设置,没有自动装配
    2.byName 由属性名自动装配
    3.byType 由属性数据类型自动装配
    4.constructor 类似于byType,但该类型适用于构造参数类型
    5.autodetect Spring首先尝试通过 constructor 使用自动装配来连接,如果它不执
    行,Spring 尝试通过 byType 来自动装配。

    8.spring基于注解的配置

    1.在XML文件中开启注解配置
    <context:annotation-config/>

    2.@Required注释
    @Required注释应用于bean属性的setter方法,表名受影响的bean属性在配置时必须放
    在XML配置文件中否则抛出bean初始化异常BeanInitializationException
    例子
    java文件中的内容
    public class Student {
    private Integer age;
    private String name;
    @Required
    public void setAge(Integer age) {
    this.age = age;
    }
    @Required
    public void setName(String name) {
    this.name = name;
    }
    public String getName() {
    return name;
    }
    XML文件中的内容
    <context:annotation-config/>

    <!-- Definition for student bean -->
    <bean id="student" class="com.tutorialspoint.Student">
    <property name="name" value="Zara" />

    <!-- try without passing age and check the result -->
    <!-- property name="age" value="11"-->此处注释掉了会抛出异常
    </bean>

    3.@Autowired注释
    1.@Autowired注释对在哪里和如何完成自动连接提供了更多的细微的控制
    2.@Autowired注释可以在setter方法中被用于自动连接bean

    9.spring基于java的配置

    1.@Configuration 和 @Bean 注解
    @Configuration的注解类表示这个类可以使用springIoC容器作为bean定义的来源
    @Bean注解告诉spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注
    册为在spring应用程序上下文中的bean。
    例子
    package com.tutorialspoint;
    import org.springframework.context.annotation.*;
    @Configuration
    public class HelloWorldConfig {
    @Bean
    public HelloWorld helloWorld(){
    return new HelloWorld();
    }
    }
    上述代码相当于在XML文件中配置bean
    <beans>
    <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" />
    </beans>
    如何获取这个bean呢?使用AnnotationConfigApplicationContext方法获bean
    例子
    public static void main(String[] args) {
    ApplicationContext ctx =
    new AnnotationConfigApplicationContext(HelloWorldConfig.class);
    HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
    helloWorld.setMessage("Hello World!");
    helloWorld.getMessage();
    }
    注意上述两个方法都先要有pojo类
    例子
    package com.tutorialspoint;

    public class HelloWorld {
    private String message;

    public void setMessage(String message){
    this.message = message;
    }

    public void getMessage(){
    System.out.println("Your Message : " + message);
    }
    }

    2.@Import 注解
    @Import 注解允许从另外一个配置类中加载@Bean定义
    例子
    配置类A
    @Configuration
    public class ConfigA {
    @Bean
    public A a() {
    return new A();
    }
    }
    配置类B
    @Configuration
    @Import(ConfigA.class)
    public class ConfigB {
    @Bean
    public B a() {
    return new A();
    }
    }
    获取对象bean对象(初始化上下文时,不需要同时指定 ConfigA.class 和
    ConfigB.class,只有 ConfigB 类需要提供)
    例子
    public static void main(String[] args) {
    ApplicationContext ctx =
    new AnnotationConfigApplicationContext(ConfigB.class);
    // now both beans A and B will be available...
    A a = ctx.getBean(A.class);
    B b = ctx.getBean(B.class);
    }
    别忘了A和B的pojo类

    3.@Bean下面可以指定作用域
    例子
    @Configuration
    public class AppConfig {
    @Bean
    @Scope("prototype")
    public Foo foo() {
    return new Foo();
    }
    }

    spring框架AOP

    spring框架的一个关键组件是面向切面的编程框架。

    AOP术语
    1.Aspect 一个模块具有一组提供横切需求的APIs,例如一个日志模块为了记录日志将
    被AOP方面调用。

    2.join point 在应用程序中代表一个点,可以插在AOP方面。

    3.Advice 实际行动之前或者之后执行的方法。

    4.Pointcut 这是一组一个或多个连接点,通知应该被执行。

    5.Introduction 引用允许你添加方法或属性到现有的类中。

    6.Target object 被一个或者多个方面所通知的对象,这个对象永远都是一个被代理
    对象

    7.Weaving Weaving把方面连接到其他的应用程序类型或者对象上,并创建一个被通知
    的对象。

    通知的类型
    1.前置通知Befoore 在一个方法执行执行
    2.后置通知After 在一个方法执行之后执行,不管怎样都执行
    3.返回后通知AfterReturning 方法执行成功后的通知
    4.抛出异常后的通知AfterThrowing方法抛出异常时执行的通知
    5.环绕通知Around 在方法调用之前和之后执行的通知。

    spring中基于AOP的XML架构
    1.导入相关包(aspectjrt.jar,aspectjweaver.jar,aspectj.jar,
    aopalliance.jar)
    2.XML文件中声明一个aspect(一个aspect是使用元素声明的,支持的bean是使用ref
    属性引用的)
    例子
    <aop:config>
    <aop:aspect id="myAsepct" ref="aBean">
    ...
    <aop:config/>
    <bean id="aBean" class="...">
    ...
    </bean>
    3.声明一个切入点(一个切入点有助于确定使用不同建议执行的感兴趣的 连接点==方
    法)
    例子
    <aop:config>
    <aop:aspect id="myAspect" ref="aBean">
    <aop:pointcut id="businessService"
    expression="execution(* com.xyz.myapp.service.*.*(..))"/>
    ...
    </aop:aspect>
    </aop:config>
    <bean id="aBean" class="...">
    ...
    </bean>
    下面的示例定义了一个名为 “businessService” 的切入点,该切入点将与
    com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:
    <aop:config>
    <aop:aspect id="myAspect" ref="aBean">
    <aop:pointcut id="businessService"
    expression="execution(* com.tutorialspoint.Student.getName(..))"/>
    ...
    </aop:aspect>
    </aop:config>
    <bean id="aBean" class="...">
    ...
    </bean>
    4.使用五种通知的方法
    在expresssion后面配置即可,具体样式
    <aop:通知类型 pointcut-ref="pointcut的id" method="方法名"/>
    例子
    <aop:config>
    <aop:aspect id="myAspect" ref="aBean">
    <aop:pointcut id="businessService"
    expression="execution(* com.xyz.myapp.service.*.*(..))"/>
    <!-- a before advice definition -->
    <aop:before pointcut-ref="businessService"
    method="doRequiredTask"/>
    <!-- an after advice definition -->
    <aop:after pointcut-ref="businessService"
    method="doRequiredTask"/>
    <!-- an after-returning advice definition -->
    <!--The doRequiredTask method must have parameter named retVal -->
    <aop:after-returning pointcut-ref="businessService"
    returning="retVal"
    method="doRequiredTask"/>
    <!-- an after-throwing advice definition -->
    <!--The doRequiredTask method must have parameter named ex -->
    <aop:after-throwing pointcut-ref="businessService"
    throwing="ex"
    method="doRequiredTask"/>
    <!-- an around advice definition -->
    <aop:around pointcut-ref="businessService"
    method="doRequiredTask"/>
    ...
    </aop:aspect>
    </aop:config>
    <bean id="aBean" class="...">
    ...
    </bean>

    spring中基于AOP的@AspectJ
    1.引入相关包(跟上述相同)

    2.声明一个aspect(先在在类上加注解@Aspect,再在XML文件配置该类)

    第一步
    package org.xyz;
    import org.aspectj.lang.annotation.Aspect;
    @Aspect
    public class AspectModule {
    }
    第二步
    <bean id="myAspect" class="org.xyz.AspectModule">
    <!-- configure properties of aspect here as normal -->
    </bean>

    3.声明一个切入点(在在方法上配置@Pointcut,execution视具体情况而定)
    1.在方法配置@Pointcut,所有方法都匹配该切入点
    例子
    import org.aspectj.lang.annotation.Pointcut;
    @Pointcut("execution(* com.xyz.myapp.service.*.*(..))") // expression
    private void businessService() {}
    2.在具体的方法上配置@Poincut,表示在该方法上匹配该切入点
    import org.aspectj.lang.annotation.Pointcut;
    @Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
    private void getname() {}

    4.声明建议(在A方法上声明@通知类型("B方法")),B才是主体,A方法是在B方法执
    行前后要做的操作即通知
    例子
    @Before("businessService()")
    public void doBeforeTask(){
    ...
    }
    @After("businessService()")
    public void doAfterTask(){
    ...
    }
    @AfterReturning(pointcut = "businessService()", returning="retVal")
    public void doAfterReturnningTask(Object retVal){
    // you can intercept retVal here.
    ...
    }
    @AfterThrowing(pointcut = "businessService()", throwing="ex")
    public void doAfterThrowingTask(Exception ex){
    // you can intercept thrown exception here.
    ...
    }
    @Around("businessService()")
    public void doAroundTask(){
    ...
    }


    spring事务管理
    1.事务特性
    1.原子性
    2.一致性
    3.隔离性
    4.持久性
    2.spring支持的事务管理
    1.编程式事务(在编程的帮助下管理事务,灵活性大,难维护)
    2.声明式事务(用注释或者XML配置管理事务)

  • 相关阅读:
    Hadoop学习之编译eclipse插件
    js堆栈溢出错误
    java——推断日期是否在今天之前
    AlertDialog.Builder中的setMultiChoiceItems中的事件处理
    Qemu之Network Device全虚拟方案二:虚拟网卡的创建
    【Android Tricks 6】ViewPager首页与尾页的滑动动作响应
    JFinal开发web项目出现故障小记
    HDU-4407-Sum(容斥原理)
    自己动手写CPU之第五阶段(3)——MIPS指令集中的逻辑、移位与空指令
    待字闺中之巧妙排序分析:
  • 原文地址:https://www.cnblogs.com/qiannianguyao/p/11878241.html
Copyright © 2011-2022 走看看