spring 自動裝配和aop的使用
使用注解配置spring
一、步驟
1.為主配置文件引入新的命名空間(約束)
導(dǎo)入spring-context-4.2.xsd schema約束

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)入

二、配置注解
// 幫我們創(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能夠為容器中管理的對象生成動態(tài)代理
二、spring實現(xiàn)aop的原理
1.動態(tài)代理(優(yōu)先)
被代理對象必須要實現(xiàn)接口,才能產(chǎn)生代理對象.如果沒有接口將不能使用動態(tài)代理技術(shù)
2.cglib代理(沒有接口)
第三方代理技術(shù),cglib代理.可以對任何類生成代理.代理的原理是對目標(biāo)對象進行繼承代理. 如果目標(biāo)對象被final修飾.那么該類無法被cglib代理.
三、aop名詞學(xué)習(xí)
- JoinPoint(連接點):目標(biāo)對象中,所有可以增強的方法
- Pointcut(切入點):目標(biāo)對象,已經(jīng)增強的方法
- Adice(通知/增強):被增強的代碼
- Target(目標(biāo)對象):被代理的對象
- Weaving(織入):將通知應(yīng)用到切入點的過程
- Proxy(代理):將通知織入到目標(biāo)對象之后,形成代理對象
- aspect(切面):切入點 + 通知
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(){}
//前置通知
//指定該方法是前置通知,并制定切入點
@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配置的通知類
四、配置進行織入,將通知織入目標(biāo)對象中
// 1.使用注解配置 <!-- 準(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.開啟使用注解完成織入 -->
<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>
<!-- 配置切入點
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud Alibaba Seata (收藏版)
Seata是一款開源的分布式事務(wù)解決方案,致力于在微服務(wù)架構(gòu)在提供高性能和簡單一樣的分布式事務(wù)服務(wù)。這篇文章主要介紹了SpringCloud Alibaba Seata 的相關(guān)知識,需要的朋友可以參考下2020-10-10
解決springboot配置logback-spring.xml不起作用問題
這篇文章主要介紹了解決springboot配置logback-spring.xml不起作用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
SpringBoot+Websocket實現(xiàn)一個簡單的網(wǎng)頁聊天功能代碼
本篇文章主要介紹了SpringBoot+Websocket實現(xiàn)一個簡單的網(wǎng)頁聊天功能代碼,具有一定的參考價值,有需要的可以了解一下2017-08-08
springboot添加多數(shù)據(jù)源的方法實例教程
這篇文章主要給大家介紹了關(guān)于springboot添加多數(shù)據(jù)源方法的相關(guān)資料,在實際開發(fā)中經(jīng)常可能遇到在一個應(yīng)用中可能要訪問多個數(shù)據(jù)庫多的情況,需要的朋友可以參考下2023-09-09

