spring基本概念精煉

1. 概念解讀

控制反轉(zhuǎn):原來通過new構(gòu)造方法創(chuàng)建對象變成交由spring創(chuàng)建對象。
依賴注入:對象的屬性已經(jīng)被spring注入好值,可直接使用。
面向切面編程:其思想是將功能分為 核心業(yè)務(wù)功能和周邊功能 周邊功能又被定義為切面。在開發(fā)過程中,核心業(yè)務(wù)功能和切面功能獨立開發(fā),再‘編織’再一起,這就叫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. 屬性注入的方式

//這種方式會根據(jù)類型來注入Category
@Autowired
    private Category category;
//會根據(jù)名稱來注入 category
@Resource
private Category category;

4. AOP示例

spring boot中添加

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

核心業(yè)務(wù)代碼

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

切面

@Aspect
@Component
public class LoggerAspect {
    // * 返回任意類型 (..) 參數(shù)是任意數(shù)量和類型
    @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        //執(zhí)行核心業(yè)務(wù)代碼
        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 {  
        // 接收到請求,記錄請求內(nèi)容  
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  
        HttpServletRequest request = attributes.getRequest();  
        // 記錄下請求內(nèi)容  
        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 {  
        // 處理完請求,返回內(nèi)容  
        System.out.println("方法的返回值 : " + ret);  
    }  
  
    //后置異常通知  
    @AfterThrowing("webLog()")  
    public void throwss(JoinPoint jp){  
        System.out.println("方法異常時執(zhí)行.....");  
    }  
  
    //后置最終通知,final增強,不管是拋出異常或者正常退出都會執(zhí)行  
    @After("webLog()")  
    public void after(JoinPoint jp){  
        System.out.println("方法最后執(zhí)行.....");  
    }  
  
    //環(huán)繞通知,環(huán)繞增強,相當于MethodInterceptor  
    @Around("webLog()")  
    public Object arround(ProceedingJoinPoint pjp) {  
        System.out.println("方法環(huán)繞start.....");  
        try {  
            Object o =  pjp.proceed();  
            System.out.println("方法環(huán)繞proceed,結(jié)果是 :" + o);  
            return o;  
        } catch (Throwable e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文是我自己在秋招復(fù)習(xí)時的讀書筆記,整理的知識點,也是為了防止忘記,尊重勞動成果,轉(zhuǎn)載注明出處哦!如果你也喜歡,那...
    波波波先森閱讀 12,455評論 6 86
  • 1.1 spring IoC容器和beans的簡介 Spring 框架的最核心基礎(chǔ)的功能是IoC(控制反轉(zhuǎn))容器,...
    simoscode閱讀 6,855評論 2 22
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • 1.1 Spring IoC容器和bean簡介 本章介紹了Spring Framework實現(xiàn)的控制反轉(zhuǎn)(IoC)...
    起名真是難閱讀 2,676評論 0 8
  • W目前已經(jīng)麻木了,表面上不再堅持,身體恢復(fù)一些,進入了第三階段acceptance。他深知如果一直反抗到底,會得不...
    Celia歡閱讀 172評論 0 0

友情鏈接更多精彩內(nèi)容