溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

spring自動裝配和aop怎么用

發(fā)布時間:2021-08-09 11:06:34 來源:億速云 閱讀:115 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“spring自動裝配和aop怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“spring自動裝配和aop怎么用”這篇文章吧。

使用注解配置spring

一、步驟

1.為主配置文件引入新的命名空間(約束)
導(dǎo)入spring-context-4.2.xsd schema約束

spring自動裝配和aop怎么用

2.開啟使用注解代理配置文件

// 在applicationContext.xml中
// 指定掃描cn.zhli13.bean包下所有類的注解
// 掃描時會掃描指定包下的所有子孫包
<context:component-scan base-package="cn.zhli13.bean"></context:component-scan>

3.在類中使用注解完成配置

// @Componet等

二、將對象注冊到容器

// 將user注冊到spring容器中,相當(dāng)于<bean name="user" class="cn.zhli13.bean.User"></bean>
@Componet("user")
@Service("user") // service層
@Controller("user") // web層
@Repository("user") // dao層

三、修改對象的作用范圍

// 指定對象的作用域
@Scope(scopeName="prototypo") // 非單例模式

四、值類型注入

// 1.通過反射的field賦值,破壞了封裝性
@Value("tom")
private String name;
// 2.通過set方法賦值,推薦使用
@Value("tom")
public void setName(String name) {
  this.name = name;
}

五、引用類型注入

@Autowired // 自動裝配
// 問題:如果匹配多個類型一致的對象,將無法選擇具體注入哪一個對象
@Qualifier("car2")// 使用@Qualifier注解告訴spring容器自動裝配哪個名稱的對
private Car car;

六、初始化、銷毀方法

@PostConstruct // 在對象創(chuàng)建后調(diào)用,xml配置中的init-method
public void init () {
  System.out.println("init");
}
@PreDestory // 在對象銷毀之前調(diào)用,xml配置中的destory-method
public void destory () {
  System.out.println("destory");
}

spring與junit整合測試

一、導(dǎo)包

額外導(dǎo)入

spring自動裝配和aop怎么用

二、配置注解

// 幫我們創(chuàng)建容器
@RunWith("SpringJunit4ClassRunner")
// 指定創(chuàng)建容器時使用哪個配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
  // 將名為user的對象注入到變量u中
  @Resource(name="user")
  private User u;
}

三、測試

@Test
public void fun1() {
  System.out.println(u);
}

spring中的aop

一、概念

aop思想:橫向重復(fù)、縱向抽取

aop概念:spring能夠?yàn)槿萜髦泄芾淼膶ο笊蓜討B(tài)代理

二、spring實(shí)現(xiàn)aop的原理

1.動態(tài)代理(優(yōu)先)
被代理對象必須要實(shí)現(xiàn)接口,才能產(chǎn)生代理對象.如果沒有接口將不能使用動態(tài)代理技術(shù)

2.cglib代理(沒有接口)
第三方代理技術(shù),cglib代理.可以對任何類生成代理.代理的原理是對目標(biāo)對象進(jìn)行繼承代理. 如果目標(biāo)對象被final修飾.那么該類無法被cglib代理.

三、aop名詞學(xué)習(xí)

  • JoinPoint(連接點(diǎn)):目標(biāo)對象中,所有可以增強(qiáng)的方法

  • Pointcut(切入點(diǎn)):目標(biāo)對象,已經(jīng)增強(qiáng)的方法

  • Adice(通知/增強(qiáng)):被增強(qiáng)的代碼

  • Target(目標(biāo)對象):被代理的對象

  • Weaving(織入):將通知應(yīng)用到切入點(diǎn)的過程

  • Proxy(代理):將通知織入到目標(biāo)對象之后,形成代理對象

  • aspect(切面):切入點(diǎn) + 通知

spring aop的使用

一、導(dǎo)包

// spring的aop包
spring-aspects-4.2.4.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
// spring需要第三方aop包
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

二、準(zhǔn)備目標(biāo)對象

public class UserServiceImpl implements UserService {
  @Override
  public void save() {
    System.out.println("保存用戶!");
  }
  @Override
  public void delete() {
    System.out.println("刪除用戶!");
  }
  @Override
  public void update() {
    System.out.println("更新用戶!");
  }
  @Override
  public void find() {
    System.out.println("查找用戶!");
  }
}

三、準(zhǔn)備通知

// 1.使用注解方式
// 表示該類是一個通知類
@Aspect
public class MyAdvice {
  @Pointcut("execution(* cn.zhli13.service.*ServiceImpl.*(..))")
  public void pc(){}
  //前置通知
  //指定該方法是前置通知,并制定切入點(diǎn)
  @Before("MyAdvice.pc()")
  public void before(){
    System.out.println("這是前置通知!!");
  }
  //后置通知
  @AfterReturning("execution(* cn.zhli13.service.*ServiceImpl.*(..))")
  public void afterReturning(){
    System.out.println("這是后置通知(如果出現(xiàn)異常不會調(diào)用)!!");
  }
  //環(huán)繞通知
  @Around("execution(* cn.itcast.zhli13.*ServiceImpl.*(..))")
  public Object around(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("這是環(huán)繞通知之前的部分!!");
    Object proceed = pjp.proceed();//調(diào)用目標(biāo)方法
    System.out.println("這是環(huán)繞通知之后的部分!!");
    return proceed;
  }
  //異常通知
  @AfterThrowing("execution(* cn.zhli13.service.*ServiceImpl.*(..))")
  public void afterException(){
    System.out.println("出事啦!出現(xiàn)異常了!!");
  }
  //后置通知
  @After("execution(* cn.itcast.zhli13.*ServiceImpl.*(..))")
  public void after(){
    System.out.println("這是后置通知(出現(xiàn)異常也會調(diào)用)!!");
  }
}
// 2.使用xml配置
// 移除上述通知類的注解就是xml配置的通知類

四、配置進(jìn)行織入,將通知織入目標(biāo)對象中

// 1.使用注解配置 
<!-- 準(zhǔn)備工作: 導(dǎo)入aop(約束)命名空間 -->

spring自動裝配和aop怎么用

<!-- 1.配置目標(biāo)對象 -->
<bean name="userService" class="cn.zhli13.service.UserServiceImpl" ></bean>
<!-- 2.配置通知對象 -->
<bean name="myAdvice" class="cn.zhli13.aop.MyAdvice" ></bean>
<!-- 3.開啟使用注解完成織入 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
// 2.使用xml配置
<!-- 準(zhǔn)備工作: 導(dǎo)入aop(約束)命名空間 -->
<!-- 1.配置目標(biāo)對象 -->
<bean name="userService" class="cn.zhli13.service.UserServiceImpl" ></bean>
<!-- 2.配置通知對象 -->
<bean name="myAdvice" class="cn.zhli13.aop.MyAdvice" ></bean>
<!-- 3.配置將通知織入目標(biāo)對象 -->
<aop:config>
  <!-- 配置切入點(diǎn) 
    public void cn.zhli13.service.UserServiceImpl.save() 
    void cn.zhli13.service.UserServiceImpl.save()
    * cn.zhli13.service.UserServiceImpl.save()
    * cn.zhli13.service.UserServiceImpl.*()
    
    * cn.zhli13.service.*ServiceImpl.*(..)
    * cn.zhli13.service..*ServiceImpl.*(..)
  -->
  <aop:pointcut expression="execution(* cn.zhli13.service.*ServiceImpl.*(..))" id="pc"/>
  <aop:aspect ref="myAdvice" >
    <!-- 指定名為before方法作為前置通知 -->
    <aop:before method="before" pointcut-ref="pc" />
    <!-- 后置 -->
    <aop:after-returning method="afterReturning" pointcut-ref="pc" />
    <!-- 環(huán)繞通知 -->
    <aop:around method="around" pointcut-ref="pc" />
    <!-- 異常攔截通知 -->
    <aop:after-throwing method="afterException" pointcut-ref="pc"/>
    <!-- 后置 -->
    <aop:after method="after" pointcut-ref="pc"/>
  </aop:aspect>
</aop:config>

以上是“spring自動裝配和aop怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI