zhang 3 anni fa
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 3
 import com.controller.UserController;
2 4
 import com.service.MqService;
3 5
 import com.service.UserService;
@@ -16,9 +18,12 @@ public class Main {
16 18
 //        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\java\\renren\\spring_demo\\src\\main\\resources\\context.xml");
17 19
         Object userController = beanFactory.getBean(UserController.class);
18 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 25
         // userService1 在 xml文件中定义
21
-        MqService mqService =(MqService) applicationContext.getBean(MqServiceImpl.class);
26
+       // MqService mqService =(MqService) applicationContext.getBean(MqServiceImpl.class);
22 27
 //        mqService.pushRabbitMessage();
23 28
 //        Thread.sleep(2000);
24 29
 //        mqService.listenRabbitQueue();

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

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

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

@@ -3,9 +3,20 @@
3 3
 <beans xmlns="http://www.springframework.org/schema/beans"
4 4
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 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 10
 <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
8 11
 <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
9 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 21
     <context:component-scan base-package="com"></context:component-scan>
11 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 2
 jdbc.url=jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
3 3
 jdbc.username=sczn
4 4
 jdbc.password=sczn159357