|
如果你正在学习spring语言,并且是最新的spring4.x,那么我相信你应该知道这个名词“泛型依赖注入”,这就是spring4.0中的一个新特性(在spring语言的2.x和3.x中是的确不存在的概念),那么也许你想问到底什么是spring中的泛型依赖注入呢?
7 z2 G( a$ k: l& G2 |% N5 F! p1 v* R+ L
可以看我写得代码,相信你会从中找到感觉的。0 R: g/ k1 m! `7 ?6 k! u9 g1 T
) Y2 R& `4 q6 @0 f1 h. K
1、eclipse中建立一个java project工程就好了
3 B# h2 B2 V$ C' X2、加入spring相关jar包
) ?) n! W( l. d( K
0 m1 j0 t7 g% G/ Y- v. j7 U$ d/ [, L7 s" z0 f" L$ W7 Y9 U X' i e
3、src下建立spring配置文件,我这里叫作:beans-generic-di.xml1 `6 m' g5 [' ?$ H6 v& p
<?xml version="1.0" encoding="UTF-8"?>! H4 d% w' _& [7 _& G3 C
<beans xmlns="http://www.springframework.org/schema/beans"
3 l/ X: Z$ X* @! t9 t* C, s0 H' n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
4 |, a9 r1 F- s+ O xmlns:context="http://www.springframework.org/schema/context"
" @% m; |) V% V; m8 o1 M1 X* t xmlns:aop="http://www.springframework.org/schema/aop"6 Q# k2 W1 r: J$ p1 K! o1 o* |
xsi:schemaLocation="/ B: {& p3 Y" m) c- e
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd # z9 F8 X2 n9 h4 p7 X& X" W! b
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
g9 z4 x& R6 \& ?2 @ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd . c0 G, A& T J& g2 P
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">! S/ E9 m3 s7 n* h9 S. \4 k, z; H
<context:component-scan base-package="com.javazx.beans.generic.di"></context:component-scan>6 R8 M8 @7 P) T+ L" {. c, {3 Z
</beans>% a" N8 \) r- d
2 m8 c6 g# W4 j
& b. I3 m" _* H2 s$ r# P
4、建立BaseRespository类:/ `& r( O5 j$ p+ {3 I
package com.javazx.beans.generic.di;
( T8 n; \; r1 P( ]/**
$ @4 `4 K1 d3 v: | *1 @2 v) c/ ?" ^- \
* @author 作者 :javazx.com* v: q1 o7 J3 i: s
* @version 创建时间:2015年10月30日 下午8:05:03 & m. k2 x/ L; r+ [+ Q. e+ D
* @description 描述:3 G$ k: [* y' ~
*
0 _0 ~ i0 A" e( x# Z5 D7 S$ j5 T */" w3 g, z6 o" t- a+ P
public class BaseRespository<T> {8 b6 J6 H' p- v' d# k% d# c
; q0 L' {( n o4 d# Q4 ?! {7 v3 w}- g9 I/ @* ?4 ?9 E! e
3 \4 W8 Z6 M- p/ r, v* x8 K6 @
3 h6 [3 G7 |) L& T4 J2 S) i8 U7 X% `5、建立BaseService类:
! z. y: ]8 q W& M2 t4 j8 Q* xpackage com.javazx.beans.generic.di;
5 l; l8 ~: R. \/ }! i
" E6 n2 G6 h& W) R4 [9 Yimport org.springframework.beans.factory.annotation.Autowired;
4 Q3 x* Z. D0 G2 P4 G6 [
: P9 l; f( r% x* D q: c+ a/**
' a6 q; v9 K7 @ *
; L2 s' @% r+ k0 l1 }2 L * @author 作者 :javazx.com; K5 M' F v4 x4 f5 m1 e
* @version 创建时间:2015年10月30日 下午8:05:285 v9 N! e G; d. z& b
* @description 描述:泛型依赖注入: X+ d, n8 c3 S
* / t, k& _; p/ Q! z
*/% e0 q; x o# ]* G
% }8 V! X9 K3 t% O0 P5 [
public class BaseService<T> {& Z/ P9 E; `3 o4 `. Y
@Autowired
0 ]* U8 u, _* l: G3 Z* ^- U- h private BaseRespository<T> baseRespository;1 N ]% d& F6 b, c
: p7 f& ]4 e' K) e4 E7 \. q! s
public void add() {
) m" C, T( a+ g; i5 ?7 c/ m System.out.println("add...");
+ f5 ?, T3 X8 ?+ T" p4 X System.out.println(baseRespository);4 d) _, X8 u: W% u+ h% y
}
6 R) Y+ M3 [+ K( [- O% `}
( t( H6 T) u H% I8 s$ Z j+ z: o+ d, x4 _
8 K# U8 g: v& ?9 h7 N/ c+ Z6、建立UserRespository类:# Z2 X7 H6 [; S# r' F( l" v
package com.javazx.beans.generic.di; Y* Y$ H' c2 n0 `' G
}( w7 P% ^/ x& a8 D cimport org.springframework.stereotype.Repository;/ P% [" f1 s6 ~* I
+ X' S6 Q2 {- u) A/**
3 R* o N7 ?3 E$ L z& H *
3 n6 F% r" r8 r s. f/ p * @author 作者 :javazx.com) Y/ t8 ^8 i( Z/ ?
* @version 创建时间:2015年10月30日 下午8:15:37 ) y, o1 ~" W+ H u+ F* S4 a4 \
* @description 描述:
; ~) b7 `% S9 O *
& U- w% J4 f: d */" h5 [) X0 E& j J6 A/ Z Y
@Repository" l& A" R5 K6 j- m8 x$ s8 t
public class UserRespository extends BaseRespository<User> {) c1 F( n4 i% l8 s$ b) r" O; D, V$ A
0 Z5 ]' F8 m2 D* @& ], N8 J: G
}0 Y) X% s/ y2 u
) R7 s! _7 k2 [5 [. ^* R7 B2 k- I n+ W& X
7、建立UserService类:
, |0 ~# c4 ~) jpackage com.javazx.beans.generic.di;
! i; a% s K5 n0 G
( g8 ~7 O7 O' Q6 E0 W+ j3 |import org.springframework.stereotype.Service;/ O% E( ^5 h0 P2 {) G
8 ]! X& p. B/ m2 Y7 j9 d! M
/** # Q" F _9 d7 R
*( W& u2 _/ |# _8 ?0 f8 r
* @author 作者 :javazx.com J4 p: y9 s+ ^; x* P
* @version 创建时间:2015年10月30日 下午8:12:04
7 p3 L9 k C! \; }0 ^! @7 `7 U * @description 描述:& q/ V, @. ~; e6 m
*
, d/ q! A! C% V; U# l6 Z7 b B6 a */
3 \1 }" r- j6 Q9 {@Service4 b9 K1 A( O/ g4 b
public class UserService extends BaseService<User>{
: M; V4 I, c5 V! H8 C. p: _
- x! `* d( H5 t# |- I}
/ H q* c e# v7 L
' n* r+ [2 [1 T% C+ x6 [2 {
! L/ z9 u6 m6 Q6 R8、建立User 类:% M# N- ~0 ~6 L/ z- \! P( `' l3 |
package com.javazx.beans.generic.di;
! [7 I; I: L. d2 s9 @! ]/**
2 k5 G8 t8 \* P2 f *) _ X' H( \' ]! t
* @author 作者 :javazx.com
" e' }, t. A$ O, P* K * @version 创建时间:2015年10月30日 下午8:12:11 ! _) V' L+ B( R- a2 {
* @description 描述:
4 l4 W7 U# i, r7 N# p. q4 F *
! Z9 T! v* ^1 K) |2 G6 t1 J */& f9 \* K1 Q# f& P8 K Y: I8 ~
public class User {) [7 Z( }( e/ a) S; C5 V5 K
1 e% o! O; h; o- w1 t; y; M6 X G
}$ ?4 |+ O6 \( w
0 a: p' e( g% u' O
/ U0 l, W. Y; R \( Y. y1 C
9、最后建立测试类:
$ N1 L4 Z; w! M3 `* W' u8 e8 @& ipackage com.javazx.beans.generic.di;0 y: O3 e, U% }8 u* i% p+ l
: i* U0 v+ e" Y! K0 P8 L
import org.springframework.context.ApplicationContext;2 Q3 J* D0 s5 X) \- s
import org.springframework.context.support.ClassPathXmlApplicationContext;
6 u/ ~+ W& ^( E% @$ _
) M8 b) {5 `+ h/**
4 q) j: G! s. Q6 j8 F6 |, v9 V' d *
, f! N# ]6 j' k/ l$ L3 R" Y * @author 作者 :javazx.com
8 ~' t( `8 ~ M* L * @version 创建时间:2015年10月30日 下午8:19:15
1 y1 e% I, ~3 Q- b& }& T+ X; t. J * @description 描述:* E% W. ]4 g! r5 s3 z; [* Y) ^
* ; p' N. J/ f' A5 K; }
*/
( [. W% A) i5 V+ Y2 V+ `+ [public class Main {
' {7 W0 E; W2 i- S! Y8 f- c K3 y8 W9 Y7 U. v
private static ApplicationContext ac;
+ s( {/ N- ~6 @+ S; x, S& L3 }! ~5 `6 s$ z8 h2 z
public static void main(String[] args) {
9 C6 X% w/ f% s! m ac = new ClassPathXmlApplicationContext(- h, `/ _$ f1 A" G/ Y
"beans-generic-di.xml");
' \$ F# c( S1 A+ ~/ @ UserService uc = (UserService) ac.getBean("userService");: ^5 ^9 V3 r* w
uc.add();
! n" W+ q& L4 }. E* v6 x; q }
( |2 {8 E7 U4 K2 g
1 u) ?& x: [: |1 J5 P}6 U f3 l! W( C9 Q
( T5 F- J! z) G% N( G
$ ^# m/ c9 X, K! {# o10、运行main方法就可以了啊!相信你对spring泛型依赖注入应该有所了解了吧,如果还有哪些疑问,请直接回帖。0 o4 x* m) b: r8 a. w S
# _8 G6 \6 A o
; w8 q" A# C2 Q! _" p, P* Y" n V: Y7 g4 U
) |- k5 Z- j4 C* _' { N, F' g
# \ v2 S9 Q) B) m7 D) f! I
& m. O% O6 ?$ W. J) x4 | f# A0 |4 P2 y# t
|
|