zoukankan      html  css  js  c++  java
  • spring重要知识点总结

    一、面向切面编程

    配置applicationContext.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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                  http://www.springframework.org/schema/aop
                  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                  http://www.springframework.org/schema/context
                  http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    <!--面向切面注解方式 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!--自动扫描包中的类,并创建实例 -->
    <context:component-scan base-package="cn.com.test,cn.com.daoImp,cn.com.service,cn.compony.action"/></beans>

    注解类,如下例子:

    @Aspect@Component//注意:一定要添加该注解component,并且在applicationContext.xml的配置context:component-scan中加入该包名
    public class Aspectj_dowhat {
    @Pointcut("execution(* cn.com.test.dowhat.say*(..))")
    public void pointCut(){}
    @After("pointCut()")
    public void after(){
    System.out.println("after");
    }
    @Before("pointCut()")
    public void before(){
    System.out.println("before");
    }
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinpoint){
    //joinpoint.getArgs()得到传入到方法的参数
    Object valu = null;
    try {
    System.out.println("around before");
    valu = joinpoint.proceed();
    //返回所代理的方法(有返回值的方法)返回值
    System.out.println("around after:"+valu);
    } catch (Throwable e) {
    e.printStackTrace();
    /*try {
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    request.getRequestDispatcher("/error.html").forward(request, response);
    } catch (Exception e2) {
    e2.printStackTrace();
    }*/
    }
    return valu;
    }

    @AfterReturning("pointCut()")
    public void afteReturning(){
    System.out.println("afteReturning");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing(){
    System.out.println("afterThrowing");
    }
    }

    二、使用注解方式自动扫描类,注册到spring容器和依赖注入,

    1、applicationContext.xml配置与第一项相同。

    2、类定义时的注解,下面的注解功能都一样,不同表达让代码功能分类更清晰。

    @Service用于标注业务层组件
    @Controller用于标注控制层组件(如struts2中的action)
    @Repository用于标注数据访问组件,即DAO组件
    @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

    3、实例的注入。@Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上。

    @Resource默认是按照名称来装配注入,当找不到与名称匹配的bean会按照类型来装配注入;

    @Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用;

    三、实例的生命周期的设置(@scope)

    @scope默认是单例模式:线程不安全

    prototype:每次获取Bean的时候会有一个新的实例。线程安全

    request:对每一次HTTP请求都会产生一个新的bean。

    session:每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效。

    global session:类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。

    初始化方法:

    @PostConstruct
    public void init() {
    }

    销毁方法 :
    @PreDestroy
    public void destory() {
    }

  • 相关阅读:
    34组合总和(39)
    33 原子的数量(726)
    32 划分为k个相等的子集(698)
    31有效的井字游戏(794)
    30 设置交集大小至少为2
    28拼接最大数(321)
    js for循环闭包解决循环变量i遍历值
    js 绑定无响应 父元素监听,绑定子元素,事件绑定的几种方法以及区别
    如何让div+css兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器
    css实现左侧固定宽,右侧自适应
  • 原文地址:https://www.cnblogs.com/wbjgogogo/p/7552990.html
Copyright © 2011-2022 走看看