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

最新文章

  1. JavaScript 入门教程四 语言基础【2】
  2. 无环的visitor模式
  3. [ASP.NET]谈谈REST与ASP.NET Web API
  4. OAuth 2 的简单理解
  5. 【C语言】01-第一个c程序代码分析
  6. css详解笔记
  7. OC基础 内存管理
  8. Oracle 导入本地dmp文件 详细操作步骤
  9. jQuery Validation让验证变得如此容易(一)
  10. HTTP协议(四)
  11. Redis基础入门,Redis的优点也特点,Redis五种数据类型
  12. 一、.NET Core MVC 项目结构模板
  13. angular中使用ckplayer播放器
  14. 【Django】网页跳转的问题
  15. Installing TensorFlow on Ubuntu or Windows
  16. delegate的Invoke和BeginInvoke方法
  17. 第四章:Android架构
  18. Android基础系列合集
  19. 使用JFileChooser实现在指定文件夹下批量添加根据“数字型样式”或“非数字型样式”命令的文件夹
  20. 用mac的safari浏览器调试ios手机的网页

热门文章

  1. Java诊断利器Arthas优雅排查生产环境
  2. Dubbo(一):dubbo-spring-boot-starter
  3. Linux实用指令(5)
  4. java 手写 jvm高性能缓存
  5. shell脚本初学者笔记
  6. vue+element 根据内容计算单元格的宽度
  7. vue中的---MVVM(面试必问)
  8. 搜索引擎框架之ElasticSearch基础详解(非原创)
  9. MySQL数据逻辑备份
  10. 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)