spring基本概念精炼

2018-08-30  本文已影响0人  cccccttttyyy

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;  
        }  
    }  
}
上一篇 下一篇

猜你喜欢

热点阅读