使用注解配置spring
一、步驟
1.為主配置文件引入新的命名空間(約束)
導入spring-context-4.2.xsd schema約束
2.開啟使用注解代理配置文件
1
2
3
4
|
// 在applicationcontext.xml中 // 指定掃描cn.zhli13.bean包下所有類的注解 // 掃描時會掃描指定包下的所有子孫包 <context:component-scan base- package = "cn.zhli13.bean" ></context:component-scan> |
3.在類中使用注解完成配置
1
|
// @componet等 |
二、將對象注冊到容器
1
2
3
4
5
|
// 將user注冊到spring容器中,相當于<bean name="user" class="cn.zhli13.bean.user"></bean> @componet ( "user" ) @service ( "user" ) // service層 @controller ( "user" ) // web層 @repository ( "user" ) // dao層 |
三、修改對象的作用范圍
1
2
|
// 指定對象的作用域 @scope (scopename= "prototypo" ) // 非單例模式 |
四、值類型注入
1
2
3
4
5
6
7
8
|
// 1.通過反射的field賦值,破壞了封裝性 @value ( "tom" ) private string name; // 2.通過set方法賦值,推薦使用 @value ( "tom" ) public void setname(string name) { this .name = name; } |
五、引用類型注入
1
2
3
4
|
@autowired // 自動裝配 // 問題:如果匹配多個類型一致的對象,將無法選擇具體注入哪一個對象 @qualifier ( "car2" ) // 使用@qualifier注解告訴spring容器自動裝配哪個名稱的對 private car car; |
六、初始化、銷毀方法
1
2
3
4
5
6
7
8
|
@postconstruct // 在對象創建后調用,xml配置中的init-method public void init () { system.out.println( "init" ); } @predestory // 在對象銷毀之前調用,xml配置中的destory-method public void destory () { system.out.println( "destory" ); } |
spring與junit整合測試
一、導包
額外導入
二、配置注解
1
2
3
4
5
6
7
8
9
|
// 幫我們創建容器 @runwith ( "springjunit4classrunner" ) // 指定創建容器時使用哪個配置文件 @contextconfiguration ( "classpath:applicationcontext.xml" ) public class demo { // 將名為user的對象注入到變量u中 @resource (name= "user" ) private user u; } |
三、測試
1
2
3
4
|
@test public void fun1() { system.out.println(u); } |
spring中的aop
一、概念
aop思想:橫向重復、縱向抽取
aop概念:spring能夠為容器中管理的對象生成動態代理
二、spring實現aop的原理
1.動態代理(優先)
被代理對象必須要實現接口,才能產生代理對象.如果沒有接口將不能使用動態代理技術
2.cglib代理(沒有接口)
第三方代理技術,cglib代理.可以對任何類生成代理.代理的原理是對目標對象進行繼承代理. 如果目標對象被final修飾.那么該類無法被cglib代理.
三、aop名詞學習
- joinpoint(連接點):目標對象中,所有可以增強的方法
- pointcut(切入點):目標對象,已經增強的方法
- adice(通知/增強):被增強的代碼
- target(目標對象):被代理的對象
- weaving(織入):將通知應用到切入點的過程
- proxy(代理):將通知織入到目標對象之后,形成代理對象
- aspect(切面):切入點 + 通知
spring aop的使用
一、導包
1
2
3
4
5
6
|
// 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 |
二、準備目標對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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( "查找用戶!" ); } } |
三、準備通知
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
// 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( "這是后置通知(如果出現異常不會調用)!!" ); } //環繞通知 @around ( "execution(* cn.itcast.zhli13.*serviceimpl.*(..))" ) public object around(proceedingjoinpoint pjp) throws throwable { system.out.println( "這是環繞通知之前的部分!!" ); object proceed = pjp.proceed(); //調用目標方法 system.out.println( "這是環繞通知之后的部分!!" ); return proceed; } //異常通知 @afterthrowing ( "execution(* cn.zhli13.service.*serviceimpl.*(..))" ) public void afterexception(){ system.out.println( "出事啦!出現異常了!!" ); } //后置通知 @after ( "execution(* cn.itcast.zhli13.*serviceimpl.*(..))" ) public void after(){ system.out.println( "這是后置通知(出現異常也會調用)!!" ); } } // 2.使用xml配置 // 移除上述通知類的注解就是xml配置的通知類 |
四、配置進行織入,將通知織入目標對象中
1
2
|
// 1.使用注解配置 <!-- 準備工作: 導入aop(約束)命名空間 --> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<!-- 1 .配置目標對象 --> <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配置 <!-- 準備工作: 導入aop(約束)命名空間 --> <!-- 1 .配置目標對象 --> <bean name= "userservice" class = "cn.zhli13.service.userserviceimpl" ></bean> <!-- 2 .配置通知對象 --> <bean name= "myadvice" class = "cn.zhli13.aop.myadvice" ></bean> <!-- 3 .配置將通知織入目標對象 --> <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" /> <!-- 環繞通知 --> <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> |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000015809544