zhangxiaoyu 3 years ago
commit
4d7c6d1604

+ 41 - 0
.gitignore

@@ -0,0 +1,41 @@
1
+.DS_Store
2
+*/.DS_Store
3
+HELP.md
4
+target/
5
+!.mvn/wrapper/maven-wrapper.jar
6
+!**/src/main/**/target/
7
+!**/src/test/**/target/
8
+log/
9
+/db/*.sql
10
+
11
+### STS ###
12
+.apt_generated
13
+.classpath
14
+.factorypath
15
+.project
16
+.settings
17
+.springBeans
18
+.sts4-cache
19
+
20
+### IntelliJ IDEA ###
21
+.idea
22
+*.iws
23
+*.iml
24
+*.ipr
25
+
26
+### NetBeans ###
27
+/nbproject/private/
28
+/nbbuild/
29
+/dist/
30
+/nbdist/
31
+/.nb-gradle/
32
+build/
33
+!**/src/main/**/build/
34
+!**/src/test/**/build/
35
+
36
+### VS Code ###
37
+.vscode/
38
+/src/main/java/com/semsx/common/config/InterceptorConfig.java
39
+/src/main/java/com/semsx/common/interceptor/PermsMethodInterceptor.java
40
+/src/main/resources/banner.txt
41
+/src/test/java/com/semsx/JavaTest.java

+ 34 - 0
pom.xml

@@ -0,0 +1,34 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>org.example</groupId>
8
+    <artifactId>spring_demo</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+
11
+    <properties>
12
+        <maven.compiler.source>8</maven.compiler.source>
13
+        <maven.compiler.target>8</maven.compiler.target>
14
+    </properties>
15
+    <dependencies>
16
+        <dependency>
17
+            <groupId>org.springframework</groupId>
18
+            <artifactId>spring-context</artifactId>
19
+            <version>5.2.2.RELEASE</version>
20
+        </dependency>
21
+        <dependency>
22
+            <groupId>junit</groupId>
23
+            <artifactId>junit</artifactId>
24
+            <version>RELEASE</version>
25
+            <scope>test</scope>
26
+        </dependency>
27
+        <dependency>
28
+            <groupId>com.rabbitmq</groupId>
29
+            <artifactId>amqp-client</artifactId>
30
+            <version>5.3.0</version>
31
+        </dependency>
32
+    </dependencies>
33
+
34
+</project>

+ 26 - 0
src/main/java/Main.java

@@ -0,0 +1,26 @@
1
+import com.controller.UserController;
2
+import com.service.MqService;
3
+import com.service.UserService;
4
+import com.service.impl.MqServiceImpl;
5
+import com.service.impl.UserServiceImpl;
6
+import org.springframework.beans.factory.BeanFactory;
7
+import org.springframework.context.ApplicationContext;
8
+import org.springframework.context.annotation.ComponentScan;
9
+import org.springframework.context.support.ClassPathXmlApplicationContext;
10
+import org.springframework.context.support.FileSystemXmlApplicationContext;
11
+
12
+public class Main {
13
+    public static void main(String[] args) throws Exception {
14
+        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("context.xml");
15
+        BeanFactory beanFactory = (BeanFactory) applicationContext;
16
+//        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\java\\renren\\spring_demo\\src\\main\\resources\\context.xml");
17
+        Object userController = beanFactory.getBean(UserController.class);
18
+        UserService bean = applicationContext.getBean(UserServiceImpl.class);
19
+
20
+        // userService1 在 xml文件中定义
21
+        MqService mqService =(MqService) applicationContext.getBean(MqServiceImpl.class);
22
+//        mqService.pushRabbitMessage();
23
+//        Thread.sleep(2000);
24
+//        mqService.listenRabbitQueue();
25
+    }
26
+}

+ 7 - 0
src/main/java/com/controller/UserController.java

@@ -0,0 +1,7 @@
1
+package com.controller;
2
+
3
+import org.springframework.stereotype.Controller;
4
+
5
+@Controller
6
+public class UserController {
7
+}

+ 6 - 0
src/main/java/com/service/MqService.java

@@ -0,0 +1,6 @@
1
+package com.service;
2
+
3
+public interface MqService {
4
+    void pushRabbitMessage();
5
+    void listenRabbitQueue();
6
+}

+ 4 - 0
src/main/java/com/service/UserService.java

@@ -0,0 +1,4 @@
1
+package com.service;
2
+
3
+public interface UserService {
4
+}

+ 57 - 0
src/main/java/com/service/impl/MqServiceImpl.java

@@ -0,0 +1,57 @@
1
+package com.service.impl;
2
+
3
+import com.rabbitmq.client.*;
4
+import com.service.MqService;
5
+import org.springframework.stereotype.Service;
6
+
7
+import javax.annotation.PostConstruct;
8
+import java.io.IOException;
9
+
10
+@Service("MqServiceImpl")
11
+public class MqServiceImpl implements MqService {
12
+    ConnectionFactory connectionFactory;
13
+    Connection connection;
14
+    Channel channel;
15
+    @PostConstruct
16
+    public void init() {
17
+        connectionFactory = new ConnectionFactory();
18
+        connectionFactory.setHost("47.110.156.18");
19
+        connectionFactory.setPort(5672);
20
+        connectionFactory.setUsername("semsx");
21
+        connectionFactory.setVirtualHost("/scanmq");
22
+        connectionFactory.setPassword("sczn2017");
23
+        try {
24
+            connection = connectionFactory.newConnection();
25
+            channel = connection.createChannel();
26
+        } catch (Exception e) {
27
+            e.printStackTrace();
28
+        }
29
+        System.out.println("init");
30
+    }
31
+
32
+    @Override
33
+    public void pushRabbitMessage() {
34
+        try {
35
+            // 获取信道
36
+            AMQP.Queue.DeclareOk declareOk = channel.queueDeclare("0503declare", false, false, true, null);
37
+            System.out.println("队列生成成功");
38
+            channel.basicPublish("","0503declare",null,"messageOH".getBytes());
39
+            System.out.println("队列推送消息成功");
40
+        } catch (Exception e) {
41
+            e.printStackTrace();
42
+        }
43
+    }
44
+
45
+    @Override
46
+    public void listenRabbitQueue() {
47
+        DeliverCallback deliverCallback = (tag,message)->{
48
+            System.out.println(new String(message.getBody()));
49
+        };
50
+        CancelCallback cancelCallback = System.out::println;
51
+        try {
52
+            channel.basicConsume("0503declare",true,deliverCallback,cancelCallback);
53
+        } catch (IOException e) {
54
+            e.printStackTrace();
55
+        }
56
+    }
57
+}

+ 8 - 0
src/main/java/com/service/impl/UserServiceImpl.java

@@ -0,0 +1,8 @@
1
+package com.service.impl;
2
+
3
+import com.service.UserService;
4
+import org.springframework.stereotype.Service;
5
+@Service("UserServiceImpl")
6
+public class UserServiceImpl implements UserService {
7
+
8
+}

+ 11 - 0
src/main/resources/context.xml

@@ -0,0 +1,11 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+
3
+<beans xmlns="http://www.springframework.org/schema/beans"
4
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
7
+<!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
8
+<!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
9
+<!--    </bean>-->
10
+    <context:component-scan base-package="com"></context:component-scan>
11
+</beans>