Browse Source

数据源测试

zhang 3 years ago
parent
commit
f3f3ae9b9e

+ 7 - 2
src/main/java/Main.java

@@ -1,3 +1,5 @@
1
+import com.alibaba.druid.pool.DruidDataSource;
2
+import com.alibaba.druid.pool.DruidPooledConnection;
1
 import com.controller.UserController;
3
 import com.controller.UserController;
2
 import com.service.MqService;
4
 import com.service.MqService;
3
 import com.service.UserService;
5
 import com.service.UserService;
@@ -16,9 +18,12 @@ public class Main {
16
 //        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\java\\renren\\spring_demo\\src\\main\\resources\\context.xml");
18
 //        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\java\\renren\\spring_demo\\src\\main\\resources\\context.xml");
17
         Object userController = beanFactory.getBean(UserController.class);
19
         Object userController = beanFactory.getBean(UserController.class);
18
         UserService bean = applicationContext.getBean(UserServiceImpl.class);
20
         UserService bean = applicationContext.getBean(UserServiceImpl.class);
19
-
21
+//        DruidDataSource dataSource = (DruidDataSource) beanFactory.getBean("dataSource");
22
+        DruidDataSource dataSource = beanFactory.getBean(DruidDataSource.class);
23
+        DruidPooledConnection connection = dataSource.getConnection();
24
+        connection.close();
20
         // userService1 在 xml文件中定义
25
         // userService1 在 xml文件中定义
21
-        MqService mqService =(MqService) applicationContext.getBean(MqServiceImpl.class);
26
+       // MqService mqService =(MqService) applicationContext.getBean(MqServiceImpl.class);
22
 //        mqService.pushRabbitMessage();
27
 //        mqService.pushRabbitMessage();
23
 //        Thread.sleep(2000);
28
 //        Thread.sleep(2000);
24
 //        mqService.listenRabbitQueue();
29
 //        mqService.listenRabbitQueue();

+ 1 - 1
src/main/java/com/service/impl/MqServiceImpl.java

@@ -7,7 +7,7 @@ import org.springframework.stereotype.Service;
7
 import javax.annotation.PostConstruct;
7
 import javax.annotation.PostConstruct;
8
 import java.io.IOException;
8
 import java.io.IOException;
9
 
9
 
10
-@Service("MqServiceImpl")
10
+//@Service("MqServiceImpl")
11
 public class MqServiceImpl implements MqService {
11
 public class MqServiceImpl implements MqService {
12
     ConnectionFactory connectionFactory;
12
     ConnectionFactory connectionFactory;
13
     Connection connection;
13
     Connection connection;

+ 12 - 1
src/main/resources/context.xml

@@ -3,9 +3,20 @@
3
 <beans xmlns="http://www.springframework.org/schema/beans"
3
 <beans xmlns="http://www.springframework.org/schema/beans"
4
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
        xmlns:context="http://www.springframework.org/schema/context"
5
        xmlns:context="http://www.springframework.org/schema/context"
6
-       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
6
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
+       http://www.springframework.org/schema/beans/spring-beans.xsd
8
+       http://www.springframework.org/schema/context
9
+       https://www.springframework.org/schema/context/spring-context.xsd">
7
 <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
10
 <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
8
 <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
11
 <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
9
 <!--    </bean>-->
12
 <!--    </bean>-->
13
+<!--    加载外部 properties文件-->
14
+    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
15
+    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
16
+        <property name="driverClassName" value="${jdbc.driver}"></property>
17
+        <property name="url" value="${jdbc.url}"></property>
18
+        <property name="username" value="${jdbc.username}"></property>
19
+        <property name="password" value="${jdbc.password}"></property>
20
+    </bean>
10
     <context:component-scan base-package="com"></context:component-scan>
21
     <context:component-scan base-package="com"></context:component-scan>
11
 </beans>
22
 </beans>

+ 1 - 1
src/main/resources/jdbc.properties

@@ -1,4 +1,4 @@
1
-jdbc.driver=com.mysql.jdbc.Driver
1
+jdbc.driver=com.mysql.cj.jdbc.Driver
2
 jdbc.url=jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2
 jdbc.url=jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
3
 jdbc.username=sczn
3
 jdbc.username=sczn
4
 jdbc.password=sczn159357
4
 jdbc.password=sczn159357