|
我有两个问题。首先,我在spring-boot中测试了db连接,如下图所示。; f7 V1 ` ?' F7 J
- @RunWith(SpringJUnit4ClassRunner.class)
' S( b( f" b7 B* E, b, n - public class OracleConnectionTest {
7 `! l( I, }1 S- s& O) \% ]: i# e - private static final Logger LOGGER = LoggerFactory.getLogger(OracleConnectionTest.class);
& w6 l0 f5 b, e$ }8 Z7 @' M
1 E3 ?( L- z& u: p& S5 q- private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";* t% B, W6 T n4 {! ] N) h% Z
- private static final String URL = "jdbcracle:thin127.0.0.1:1521rcl";- L" b) P9 |' I: C7 |3 C& B) O
- private static final String USER = "user1";
4 D6 g; [. e. U0 Y* I4 T7 F - private static final String PW = "user1";2 {% `8 s+ }0 Y8 T
2 L4 a4 a# O) q4 A" m- @Test
, g1 o8 d' V* _' c8 K - public void testConnection() throws ClassNotFoundException {
% K% F/ [8 [& z: ~ - Class.forName(DRIVER);
: G& l% X7 R& K5 k# j. S - " H) V# V' N2 R# d0 ]
- try (Connection connection = DriverManager.getConnection(URL, USER, PW)) {3 G/ @$ E# f1 n1 e8 V# n+ K9 j" N
- LOGGER.info(String.valueOf(connection));
3 h) t/ [9 k. r7 B) D8 ] - } catch (SQLException e) {' [' ~. H3 I) t( E
- LOGGER.error(String.valueOf(e));
' u$ w4 y7 m+ R - }
, F& U; O2 B9 E8 {# k - }}# F; M# Z2 k( J% \( j
但是我想把DB信息分离到application.properties文件(resourcesapplication.properties)中去,所以我需要制作JdbcConfig文件(java Bean)。所以我需要制作JdbcConfig文件(java bean),如下图所示(含测试代码)。
. ?: U+ W9 P6 [, e- @Getter
9 P) Q- U: I. | - @Setter$ u' Q- U) K {1 z% S( x" x' q/ A
- @Configuration+ w! k" ?; q5 {- D
- @ConfigurationProperties(prefix = "spring.datasource"). L0 k q9 n7 }, y- }+ J: n
- public class JdbcConfig {4 U" U5 E# L/ z, I# h6 b
- @Value("${driver-class-name}")
6 t% T, _& H3 F: n - private String driverClassName;% M6 R( ^7 C# b# @
- @Value("${url}")
& M$ }/ A5 m* { i0 l - private String url;
0 S; W9 b. L+ y - @Value("${username}")4 s8 S" X) B% c- u: q# |; X# f6 ?
- private String username; s$ Q: {- W6 ]4 B# ]0 U
- @Value("${password}")$ {+ j( @* U& C# x o& y8 B' o, q
- private String password;# m) ^# q7 ?4 z7 p# N
- ! I5 ]4 C% r6 E" P* W O
- @Override+ |1 y/ s) j6 a: X1 H0 E" N8 q5 o
- public String toString() {
$ v1 o0 @5 t2 i: \, Z# J4 j - return "JdbcConfig[" +1 U5 e/ \, e+ ~5 F1 d* Q Y
- "driverClassName=" + driverClassName R; y5 x" J# Q7 y7 D0 t! @
- + ", url=" + url2 F/ N M' r6 ^+ o- r
- + ", username=" + username
A9 _& @; F3 K4 @: I+ @# C - + ", password=" + password) q3 W0 O( t% }
- + "]";
4 X! z6 i9 \. Q4 c2 j7 J - }}
( v6 M+ [+ R% A: I% M; Z- D7 O
6 O5 I4 p1 D9 F; A/ i9 U O' b( }- @RunWith(SpringJUnit4ClassRunner.class) k* W& m! d/ j" P U" V9 V- C
- @ContextConfiguration(classes = {JdbcConfig.class})- a: r7 _. _9 N: W' {
- public class OracleConnectionTest {8 ]6 h9 s2 a3 \2 Z7 E
- private static final Logger LOGGER = LoggerFactory.getLogger(OracleConnectionTest.class);0 n/ u/ v& N) |
- 0 p3 R6 n# X9 _: b5 L7 j# j) [
- @Autowired3 g7 e# n5 y" h% ?: |4 W" r
- private JdbcConfig jdbcConfig;
8 T; I8 U* S6 @# j' m1 V# y - / r+ D/ d# O5 _2 A) Z8 U7 L1 X% u
- @Test
) E$ x3 M! R$ T. t1 T+ }4 A - public void testConnection() throws ClassNotFoundException {# L( H$ X: t+ i8 l. b
- LOGGER.info("Test: {}", jdbcConfig.toString());. c, G% s' C) [( l% t& D. g7 ?
- $ R9 [, g7 N+ S8 x/ B% Y/ c/ g- |
- Class.forName(jdbcConfig.getDriverClassName());. @$ ~$ t6 Z3 A6 j5 v
" \+ v! B6 x' _0 x; ?- try (Connection connection = DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUsername(), jdbcConfig.getPassword())) {
3 L, @ @0 w# U4 E: p1 w6 e0 N - LOGGER.info(String.valueOf(connection));
9 C! V7 S y* O/ R - } catch (SQLException e) {
, N4 d5 r& z, o' {3 Q1 R8 [$ ` - LOGGER.error(String.valueOf(e));
0 O a5 t ]( i, v7 M - }
( S$ E$ V* d+ t% o: M - }}0 y* Y, n9 q6 a: ?3 g. f/ |- m0 Y
但是,我遇到了 NullPointerException(JdbcConfig) 运行时 testConnection(). 所以我必须这样修改JdbcConfig类。2 V* Q3 y2 [, c
- @Getter
& f& V/ t4 s! w9 X, e5 \6 G - @Setter
! Q8 e3 B! |3 } - @Component+ L5 j8 o$ `8 t N2 N, M
- @PropertySource("classpath:application.properties")
' ~ |/ q' \, W$ u* B8 `4 o - public class JdbcConfig {, h0 R4 t; J' ?6 w
- @Value("${spring.datasource.driver-class-name}")
! h9 ?6 q) M2 _. X - private String driverClassName;
8 `: R/ j* M4 }0 V9 ^! o - @Value("${spring.datasource.url}"). b1 T1 |# w# j* @, a |: n
- private String url;
0 w0 h$ X+ k* y& v# a8 {. s - @Value("${spring.datasource.username}")2 v0 v, L! s' P1 V
- private String username; m5 ^! k. e: M8 |2 A: I
- @Value("${spring.datasource.password}")$ F# q; D( t9 y1 E
- private String password;3 R5 f6 m* I3 i3 f# d
- * f2 C, @: |6 W. e; @
- @Override, v: }) d& J0 W: \5 e
- public String toString() {
8 m( U# M2 t( {3 r& Y* Y4 {+ @ - return "JdbcConfig[" +
; f0 S* u& S6 f& l, s - "driverClassName=" + driverClassName3 h) t3 b2 w, {6 \0 q5 Y5 _
- + ", url=" + url7 q, B4 G" M, C2 _3 K. g
- + ", username=" + username1 i% S. d* D* G' P
- + ", password=" + password! @ ?0 z$ L" z# F4 {* U
- + "]";! V- G; [6 O; {- y
- }}
. f+ }5 b! H2 O* K( W; c 我不明白为什么 @Configuration & @ConfigurationProperties 注解不适用。其次,我以为spring-boot会自动创建 DataSource(javax.sql.DataSource) 通过阅读 application.properties. 例如,我的朋友在没有创建DataSource bean(class)的情况下测试成功。但是我遇到了一个错误,就像下面的错误一样,所以我必须创建DataSource bean。
1 u# e' C8 C$ y: C, i7 ~9 Q) ~! K- Unsatisfied dependency expressed through field 'ds'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available
: A* j9 g; y( g( b# P 所以我必须创建DataSource bean。我不知道为什么我朋友的代码不创建bean(DataSource)就能自动运行,而我却不一样。我的代码如下。
9 m* s' o( y. x- R- @RunWith(SpringJUnit4ClassRunner.class)4 E* m& o( h6 F1 E7 N) \
- @ContextConfiguration(classes = DataSourceConfig.class). f1 ^ C, [) R5 o3 K5 ~) g& d
- public class DataSourceTest {2 _' u7 ^8 Z) }. L/ D
- private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceTest.class);
% e4 x" x- \4 B9 D7 M
6 `/ B6 L* D5 `; y B6 r& B- @Autowired. Y- o* b" _: `5 x. \: ~
- private DataSource ds; // DataSource & DriverManagerDataSource
* \/ F: W C( A% g - # A* Q D# O5 y5 n0 K7 ^
- @Test
7 }( Q1 R) ]4 P5 b4 L& [ [ - public void testConnection() throws Exception {
/ i" c# L* m* J( V/ v - try (Connection con = ds.getConnection()) {
4 G* f+ E0 ^8 _) ~& d& k% k - LOGGER.info(String.valueOf(con));% O1 h/ J. v) m2 l: [
- } catch (Exception e) {! w8 L" b+ Y9 T7 y, h5 l
- e.printStackTrace();
4 Q! f. d3 l0 B- m - }
9 q$ d8 }; Z' q- @+ W - }: a Z- y# e7 n* v0 \
- } { E% ^ F7 V
. q4 _$ w& \- U/ M5 I9 }! K- @Configuration& q- u) {$ O G5 Q9 [" I9 C) L
- @Import(JdbcConfig.class)
6 i$ Z5 N2 m. I6 w5 ]' _9 ? - public class DataSourceConfig {
& L5 f+ N m( O - private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceConfig.class);
2 e7 i: X2 T5 R- v* S, H" M) |) A - 3 r7 e2 h) D6 x! x7 X
- @Bean0 X/ f( x* }$ E/ {6 l. F
- public DataSource dataSource(JdbcConfig jdbcConfig) {
2 U, R5 G% @; S; e& K a2 ^& o - LOGGER.debug("Test: {}", jdbcConfig.toString());
8 B$ I4 X; e) {' S5 c, ? - DriverManagerDataSource dataSource = new DriverManagerDataSource();
5 O& f3 O6 U% v, M# {2 l& S' i - dataSource.setDriverClassName(jdbcConfig.getDriverClassName());) I- b: W6 B% V. c% Y% Q
- dataSource.setUrl(jdbcConfig.getUrl());2 m' s* K7 m5 P: C. j; j2 K
- dataSource.setUsername(jdbcConfig.getUsername());
2 i- R8 _# M( V9 l% {2 B - dataSource.setPassword(jdbcConfig.getPassword());' Y& t; @2 g" i. |" G
- return dataSource;
6 z# z. W# n9 W3 ? - }0 [2 ]" [0 \9 _, J& a8 ?# G
- }- F4 r& ]4 [; N* _) V2 x) ^
谢谢你阅读我的长文。添加一个属性文件(application.properties)+ W. M5 n7 d, |" _9 P! s
- # JSP
" Q6 e( y, g+ y, K - spring.mvc.view.prefix = /WEB-INF/jsp/) q# @! r, V, \2 C+ F6 q
- spring.mvc.view.suffix = .jsp
/ v' \' y9 {1 h- S - # Y" V C( t& o! u3 e
- # language setting
) F$ x2 S- H" @. n$ \1 {) [5 I& B - spring.http.encoding.charset = UTF-8
' H+ o- g! h( g3 e2 }& N' i, q - spring.http.encoding.enabled = true
H8 h4 V7 p# v3 Z/ U/ C - spring.http.encoding.force = true
* k: d, W* T! Q; i% Q4 F: z+ G - ) }) [+ M) s" m6 D$ G
- # Database Info
# E; k, W9 E2 X& d$ t9 R - spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver# R; L, e& J% Q/ d e m
- spring.datasource.url = jdbcracle:thin127.0.0.1:1521rcl
/ f! K4 |* W. e5 T6 z - spring.datasource.username = user1spring.datasource.password = user12 k5 \3 r, }3 R( |% l* t) L
% `9 Y, [0 b0 ^3 @# d) D
# q7 \1 N5 z" p4 |我知道答案 回答被采纳将会获得6 金钱 已有0人回答 |
|