Skip to content

Commit 56e7282

Browse files
committed
spring相关测试
1 parent 55ae096 commit 56e7282

20 files changed

Lines changed: 993 additions & 3 deletions

pom.xml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
<groupId>com.prd</groupId>
88
<artifactId>JavaLearning</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<packaging>pom</packaging>
10+
<version>1.0-SNAPSHOT</version>
11+
<modules>
12+
<module>springdemo</module>
13+
</modules>
1014

11-
<name>JavaLearning</name>
15+
<name>JavaLearning</name>
1216
<!-- FIXME change it to the project's website -->
1317
<url>http://www.example.com</url>
1418

@@ -63,6 +67,21 @@
6367
<artifactId>jdom</artifactId>
6468
<version>2.0.2</version>
6569
</dependency>
70+
71+
72+
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
73+
<dependency>
74+
<groupId>io.netty</groupId>
75+
<artifactId>netty-all</artifactId>
76+
<version>4.1.51.Final</version>
77+
</dependency>
78+
79+
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka -->
80+
<dependency>
81+
<groupId>org.apache.kafka</groupId>
82+
<artifactId>kafka_2.13</artifactId>
83+
<version>2.5.0</version>
84+
</dependency>
6685
</dependencies>
6786

6887
<build>

springdemo/pom.xml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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+
<parent>
6+
<artifactId>JavaLearning</artifactId>
7+
<groupId>com.prd</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springdemo</artifactId>
13+
14+
<name>springdemo</name>
15+
<!-- FIXME change it to the project's website -->
16+
<url>http://www.example.com</url>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<maven.compiler.source>1.7</maven.compiler.source>
21+
<maven.compiler.target>1.7</maven.compiler.target>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>4.11</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
<!-- IOC AOP 90% spring5中已经推荐使用springboot,而非这种方式 -->
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-context</artifactId>
36+
<version>5.0.9.RELEASE</version>
37+
</dependency>
38+
39+
</dependencies>
40+
41+
<build>
42+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
43+
<plugins>
44+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
45+
<plugin>
46+
<artifactId>maven-clean-plugin</artifactId>
47+
<version>3.1.0</version>
48+
</plugin>
49+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
50+
<plugin>
51+
<artifactId>maven-resources-plugin</artifactId>
52+
<version>3.0.2</version>
53+
</plugin>
54+
<plugin>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.8.0</version>
57+
</plugin>
58+
<plugin>
59+
<artifactId>maven-surefire-plugin</artifactId>
60+
<version>2.22.1</version>
61+
</plugin>
62+
<plugin>
63+
<artifactId>maven-jar-plugin</artifactId>
64+
<version>3.0.2</version>
65+
</plugin>
66+
<plugin>
67+
<artifactId>maven-install-plugin</artifactId>
68+
<version>2.5.2</version>
69+
</plugin>
70+
<plugin>
71+
<artifactId>maven-deploy-plugin</artifactId>
72+
<version>2.8.2</version>
73+
</plugin>
74+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
75+
<plugin>
76+
<artifactId>maven-site-plugin</artifactId>
77+
<version>3.7.1</version>
78+
</plugin>
79+
<plugin>
80+
<artifactId>maven-project-info-reports-plugin</artifactId>
81+
<version>3.0.0</version>
82+
</plugin>
83+
</plugins>
84+
</pluginManagement>
85+
</build>
86+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.prd.dao;
2+
3+
public interface SpringCoreDemoDao {
4+
void hellworld();
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.prd.dao;
2+
3+
public class SpringCoreDemoDaoImpl implements SpringCoreDemoDao {
4+
@Override
5+
public void hellworld() {
6+
System.out.println("hellworld");
7+
}
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.prd.service;
2+
3+
import com.prd.dao.SpringCoreDemoDao;
4+
5+
public class SpringCoreDemoService {
6+
7+
private SpringCoreDemoDao dao;
8+
9+
public void Hello() {
10+
dao.hellworld();
11+
}
12+
13+
public void setDao(SpringCoreDemoDao dao) {
14+
this.dao = dao;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.prd.service;
2+
3+
import org.springframework.context.support.ClassPathXmlApplicationContext;
4+
5+
public class SpringCoreTest {
6+
public static void main(String[] args) {
7+
8+
// xml编程风格,及set方法注入测试
9+
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
10+
"classpath:spring.xml");
11+
SpringCoreDemoService service = (SpringCoreDemoService) context.getBean("service");
12+
service.Hello();
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
<bean id="dao" class="com.prd.dao.SpringCoreDemoDaoImpl">
6+
</bean>
7+
<bean id="service" class="com.prd.service.SpringCoreDemoService">
8+
<property name="dao" ref="dao" />
9+
</bean>
10+
</beans>

src/main/java/com/prd/io/bio/BIOChatServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* (1)基于BIO模型
1515
* (2)支持多人同时在线
1616
* (3)每个用户的发言都被转发给其他用户
17+
*
18+
* 分单线程和多线程方式
1719
*/
1820
public class BIOChatServer {
1921
public static final int DEFAULT_PORT = 8888;
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.prd.io.nio;
2+
3+
import com.prd.io.bio.BIOChatClient;
4+
5+
import java.io.Closeable;
6+
import java.io.IOException;
7+
import java.net.InetSocketAddress;
8+
import java.nio.ByteBuffer;
9+
import java.nio.channels.SelectionKey;
10+
import java.nio.channels.Selector;
11+
import java.nio.channels.ServerSocketChannel;
12+
import java.nio.channels.SocketChannel;
13+
import java.nio.charset.Charset;
14+
import java.util.Set;
15+
16+
/**
17+
* NIO实现多人聊天室,功能
18+
* (1)基于NIO模型
19+
* (2)支持多人同时在线
20+
* (3)每个用户的发言都被转发给其他用户
21+
*
22+
* NIO客户端 非阻塞
23+
*/
24+
public class NIOChatClient {
25+
26+
public static final int DEFAULT_PORT = 8888;
27+
public static final String DEFAULT_HOST="127.0.0.1";
28+
public static final String QUIT ="quit";
29+
public static final int BUFFER = 1024;
30+
31+
private SocketChannel client;
32+
private Selector selector;
33+
private ByteBuffer rBuffer = ByteBuffer.allocate(BUFFER);
34+
private ByteBuffer wBuffer = ByteBuffer.allocate(BUFFER);
35+
private Charset charset = Charset.forName("UTF-8");
36+
private int port;
37+
private String host;
38+
39+
public NIOChatClient(int port, String host) {
40+
this.port = port;
41+
this.host = host;
42+
}
43+
44+
public NIOChatClient() {
45+
this(DEFAULT_PORT,DEFAULT_HOST);
46+
}
47+
48+
/**
49+
* 检查用户是否退出
50+
* @param msg
51+
* @return
52+
*/
53+
public boolean readyToQuit(String msg) {
54+
return QUIT.equals(msg);
55+
}
56+
57+
public void close(Closeable closeable) {
58+
if (closeable!=null) {
59+
try {
60+
closeable.close();
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
}
66+
67+
/**
68+
* 启动客户端
69+
*/
70+
public void start() {
71+
try {
72+
client = SocketChannel.open();
73+
client.configureBlocking(false);
74+
75+
selector = Selector.open();
76+
//注册 连接事件监听
77+
client.register(selector,SelectionKey.OP_CONNECT);
78+
client.connect(new InetSocketAddress(host,port));
79+
80+
while(true) {
81+
selector.select();
82+
Set<SelectionKey> selectionKeys = selector.selectedKeys();
83+
for (SelectionKey key:selectionKeys) {
84+
handlers(key);
85+
}
86+
selectionKeys.clear();
87+
}
88+
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
} finally {
92+
// 只关闭selector,就可以同时关闭selector上注册的通道
93+
close(selector);
94+
}
95+
}
96+
97+
private void handlers(SelectionKey key) throws IOException {
98+
SocketChannel client = (SocketChannel) key.channel();
99+
// CONNECT事件 --连接就绪事件
100+
if (key.isConnectable()) {
101+
// 判断连接是否就绪,如果返回false表明连接正在进行,需要等待。
102+
if (client.isConnectionPending()) {
103+
client.finishConnect();
104+
// 处理用户的输入
105+
// new Thread(new UserInputHandler(this)).start();
106+
}
107+
client.register(selector,SelectionKey.OP_READ);
108+
}
109+
// READ事件 --服务器转发消息
110+
else if (key.isReadable()) {
111+
String msg = receive(client);
112+
if (msg.isEmpty()) {
113+
//服务器异常,则客户端退出
114+
close(selector);
115+
} else {
116+
System.out.println(msg);
117+
}
118+
}
119+
}
120+
121+
private String receive(SocketChannel client) throws IOException {
122+
rBuffer.clear();
123+
while (client.read(rBuffer)>0);
124+
rBuffer.flip();
125+
return String.valueOf(charset.decode(rBuffer));
126+
}
127+
128+
129+
private static class UserInputHandler implements Runnable{
130+
131+
//TODO 未完成
132+
// private NIOChatClient c
133+
134+
@Override
135+
public void run() {
136+
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)