spring基本概念精炼

https://www.jianshu.com/p/3c30279d58cd

jdk8.0 以及 spring5.0 之后已经使用java的注解方式 不需要使用xml配置文件了

并且spring改用了约定大约配置的原则。 

1. 概念解读

控制反转:原来通过new构造方法创建对象变成交由spring创建对象。
依赖注入:对象的属性已经被spring注入好值,可直接使用。
面向切面编程:其思想是将功能分为 核心业务功能和周边功能 周边功能又被定义为切面。在开发过程中,核心业务功能和切面功能独立开发,再‘编织’再一起,这就叫AOP。

2. 声明bean的方法

xml配置文件中声明

<context:annotation-config/>//开启注解方式
<context:component-scan base-package="com.how2java.pojo"/>//扫描注解方式声明的bean
<bean name="c" class="com.how2java.pojo.Category">
       <property name="name" value="category 1" />
   </bean>
   <bean name="p" class="com.how2java.pojo.Product">
       <property name="name" value="product1" />
<!--         <property name="category" ref="c" /> -->
   </bean>

注释方式声明一个bean

@Component("p")
public class Product {
}
//p就是此bean的名称  Product就是 class

3. 属性注入的方式

//这种方式会根据类型来注入Category
@Autowired
    private Category category;
//会根据名称来注入 category
@Resource
private Category category;

4. AOP示例

spring boot中添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

核心业务代码

package com.how2java.service;
@Component("s")
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
}

切面

@Aspect
@Component
public class LoggerAspect {
    // * 返回任意类型 (..) 参数是任意数量和类型
    @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        //执行核心业务代码
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

application.xml中添加

<context:component-scan base-package="com.how2java.aspect"/>
<context:component-scan base-package="com.how2java.service"/>
<aop:aspectj-autoproxy/>

详细点的使用,先定义pointcut
摘自 https://www.cnblogs.com/bigben0123/p/7779357.html

@Aspect
@Component
public class LogAspect {
    @Pointcut("execution(public * com.example.controller.*.*(..))")
    public void webLog(){}  

    @Before("webLog()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));  

    }  

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        System.out.println("方法的返回值 : " + ret);
    }  

    //后置异常通知
    @AfterThrowing("webLog()")
    public void throwss(JoinPoint jp){
        System.out.println("方法异常时执行.....");
    }  

    //后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
    @After("webLog()")
    public void after(JoinPoint jp){
        System.out.println("方法最后执行.....");
    }  

    //环绕通知,环绕增强,相当于MethodInterceptor
    @Around("webLog()")
    public Object arround(ProceedingJoinPoint pjp) {
        System.out.println("方法环绕start.....");
        try {
            Object o =  pjp.proceed();
            System.out.println("方法环绕proceed,结果是 :" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}

[转帖]spring基本概念精炼的更多相关文章

  1. Spring Security 概念基础 验证流程

    Spring Security 概念基础 验证流程 认证&授权 认证:确定是否为合法用户 授权:分配角色权限(分配角色,分配资源) 认证管理器(Authentication Manager) ...

  2. SSM 五:Spring核心概念

    第五章:Spring核心概念 一.Spring Ioc 优点: 1.低侵入式设计 2.独立于各种应用服务器 3.依赖注入特性将组建关系透明化,降低耦合度 4.面向切面编程的特性允许将通用性任务集中式处 ...

  3. Spring之旅第二篇-Spring IOC概念及原理分析

    一.IOC概念 上一篇已经了解了spring的相关概念,并且创建了一个Spring项目.spring中有最重要的两个概念:IOC和AOP,我们先从IOC入手. IOC全称Inversion of Co ...

  4. Spring 核心概念

    Spring 核心概念 引言 本文主要介绍 Spring 源码中使用到的一些核心类 1. BeanDefinition BeanDefinition表示Bean定义,BeanDefinition 中存 ...

  5. spring事务概念理解

    1.数据并发问题 脏读 A事务读取B事务尚未提交的更新数据,并在此数据的基础上操作.如果B事务回滚,则A事务读取的数据就是错误的.即读取了脏数据或者错误数据. 不可重复组 A事务先后读取了B事务提交[ ...

  6. 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍

    一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见, ...

  7. Spring核心概念之AOP

    一.AOP 的概念 AOP(Aspect Oriented Programming)的缩写,面向切面编程,主要作用就是对代码进行增强处理. 理解面向切面编程的含义:就是在不改变原有程序的基础上为代码增 ...

  8. Spring(概念)

    在本文中只讲述一些概念性的东西,因为我在开始学习JAVA的时候对这些概念性的东西总是不太理解,总结总结再感悟一下,也方便后人. 理解的不深,用通俗的语言讲一下: 百度百科这样介绍: spring框架主 ...

  9. Spring AOP概念理解

    1.我所知道的aop 初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后 ...

随机推荐

  1. 详解:Java字符串类型"switch"的底层原理

    前言: 最近更新得会比较频繁,希望大家见谅哦! 也感谢关注我的人,我会更加更加努力去做的! 基础 我们现在使用的Java的版本,基本上是都支持String类型的.当然除了String类型,还有int. ...

  2. 来看一下Java中“-”与equeals的区别

    简介: == ==是比较两个变量的值,如果是基本数据类型,那么就是比较的基本数据的大小值 情况一 int a=1; int b=1; System.out.println(a==b); 以上图中:== ...

  3. android studio学习----Android Studio导入github下载的工程--替换方法

    http://www.cnblogs.com/liuling/p/2015-9-16-01.html 这种方法是可行的,主要是先自己创建一个project ,然后把没有的文件夹都复制过去就OK了,特别 ...

  4. wc.exe个人项目

    1.GitHub项目 https://github.com/Littlehui3/wc 2.用时表格 PSP2.1 任务内容 计划完成需要的时间(min) 实际完成需要的时间(min) Plannin ...

  5. spring mybatis错误问题该怎么解决

    1.org.apache.ibatis.exceptions.PersistenceExc org.apache.ibatis.exceptions.PersistenceException: ### ...

  6. dapi 基于Django的轻量级测试平台三 接口关联

    QQ群: GitHub:https://github.com/yjlch1016/dapi 一.接口关联思路: 在接口测试中, 很多场景下, 上一个接口的出参要作为下一个接口的入参, 即上一个接口的响 ...

  7. Codeforces D. The Sum of the k-th Powers(拉格朗日插值)

    题目描述: The Sum of the k-th Powers time limit per test 2 seconds memory limit per test 256 megabytes i ...

  8. django 基础进阶ORM COOKIE

    ORM: class Book(models.Model): title=models.CharFiled(max_length=32) 类-----------------表    #  Book- ...

  9. NGUI里的sprite和label有白色的边框

    问题描述:NGUI里的sprite和label有白色的边框,而原图一切正常 如图: 解决方案: 给Sprite 边缘左右更增加1,这样拉伸的时候就忽略了左右1的位置,图片就不会显示白色边框了

  10. 清理Linux 磁盘空间

    1.执行   lsof | grep deleted发现有大量刚刚删除文件的进程存在,kill掉进程(或者重启进程)   OK 2.查看磁盘信息:df -lh 3.循环定位最大文件目录:du -h - ...