Skip to content

Commit

Permalink
Merge pull request #1 from Snailclimb/master
Browse files Browse the repository at this point in the history
增加IOC功能
  • Loading branch information
liuxiany authored Oct 4, 2020
2 parents ad7902f + a2fad34 commit 1cb0a88
Show file tree
Hide file tree
Showing 52 changed files with 764 additions and 248 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
昨天早上,6 点多一点就起来写代码了,前天晚上和朋友一起吃晚饭回来之后也搞到很晚,有时候想尽快把某些东西写完的时候确实是会这样。

但是,**说实话效率会降低很多,不推荐这样做**。像我之前写[guide-rpc-framework](https://github.com/Snailclimb/guide-rpc-framework) 的时候,经常周末不出门,一坐就是一天。到了晚上整个人一脸油,眼睛还很酸,效率低了太多。

目前的话,已经把 Spring MVC 相关常用的注解比如`@GetMapping``@PostMapping``@PathVariable` 写完了。

整个项目的目录结构如下,还算比较清晰。我也已经将项目开源出来了,地址:[https://github.com/Snailclimb/jsoncat](https://github.com/Snailclimb/jsoncat)_原创开源不易,觉得不错的话,欢迎给良心作者 1 个 star 鼓励一下!_

![](https://guide-blog-images.oss-cn-shenzhen.aliyuncs.com/2020-9/image-20200929082415631.png)
原创开源不易,觉得不错的话,欢迎给良心作者 1 个 star 鼓励一下!

## 介绍

Expand Down Expand Up @@ -48,8 +40,9 @@

#### IOC

- [ ] `@Autowired` :注入对象
- [ ] `@Component`:声明对象被 IOC 容器管理
- [x] `@Autowired` :注入对象
- [x] `@Component`:声明对象被 IOC 容器管理
- [x] `@Qualifier`: 指定注入的bean

#### AOP

Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ dependencies {
// 测试:junit5
testImplementation "org.junit.jupiter:${junitVersion}"
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
// 接口测试:rest-assured
testImplementation 'io.rest-assured:rest-assured:4.3.1'
}

apply from: rootProject.file("gradle/git-hooks.gradle")
Expand Down
5 changes: 3 additions & 2 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@
<property name="max" value="16"/>
<property name="ignoreOverriddenMethods" value="true"/>
</module>


<module name="UnusedImports">
<property name="processJavadoc" value="true"/>
</module>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="EmptyForIteratorPad"/>
Expand Down
15 changes: 10 additions & 5 deletions gradle/git-hooks.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
task applyGitHooks(type: Copy) {
// TODO remove
def pushFile = rootProject.file(".git/hooks/pre-push")
if (pushFile.exists()) pushFile.delete()
static def isWindows() {
return org.gradle.internal.os.OperatingSystem.current().isWindows()
}

task applyGitHooks(type: Copy) {
from { rootProject.file("config/git-hooks/pre-commit") }
into { rootProject.file(".git/hooks") }
Runtime.getRuntime().exec("chmod -R +x .git/hooks/")
if (!isWindows()) {
println "not on windows"
Runtime.getRuntime().exec("chmod -R +x .git/hooks/")
} else {
println "windows"
}
}
compileJava.dependsOn applyGitHooks
44 changes: 0 additions & 44 deletions src/main/java/com/github/demo/UserController.java

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/java/com/github/demo/sms/AliSmsServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.demo.sms;

import com.github.jsoncat.annotation.ioc.Component;

/**
* @author shuang.kou
* @createTime 2020年09月30日 15:43:00
**/
@Component(name = "aliSmsServiceImpl")
public class AliSmsServiceImpl implements SmsService {


@Override
public String send(SmsDto smsDto) {
System.out.println("send message to " + smsDto.getPhone());
return AliSmsServiceImpl.class.getSimpleName();
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/github/demo/sms/QiNiuSmsServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.demo.sms;

import com.github.jsoncat.annotation.ioc.Component;

/**
* @author shuang.kou
* @createTime 2020年09月30日 15:43:00
**/
@Component(name = "qiNiuSmsServiceImpl")
public class QiNiuSmsServiceImpl implements SmsService {


@Override
public String send(SmsDto smsDto) {
System.out.println("send message to " + smsDto.getPhone());
return QiNiuSmsServiceImpl.class.getSimpleName();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/github/demo/sms/SmsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.demo.sms;

import com.github.jsoncat.annotation.ioc.Autowired;
import com.github.jsoncat.annotation.springmvc.PostMapping;
import com.github.jsoncat.annotation.ioc.Qualifier;
import com.github.jsoncat.annotation.springmvc.RequestBody;
import com.github.jsoncat.annotation.springmvc.RestController;

/**
* @author shuang.kou
* @createTime 2020年09月30日 15:43:00
**/
@RestController("/sms")
public class SmsController {
@Autowired
@Qualifier("aliSmsServiceImpl")
private SmsService smsService;

@PostMapping("/send")
public String send(@RequestBody SmsDto smsDto) {
return smsService.send(smsDto);
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/github/demo/sms/SmsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.demo.sms;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author shuang.kou
* @createTime 2020年09月30日 7:19:00
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SmsDto {
private String phone;
}
9 changes: 9 additions & 0 deletions src/main/java/com/github/demo/sms/SmsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.demo.sms;

/**
* @author shuang.kou
* @createTime 2020年09月30日 15:43:00
**/
public interface SmsService {
String send(SmsDto smsDto);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.demo;
package com.github.demo.user;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/github/demo/user/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.demo.user;

import com.github.jsoncat.annotation.ioc.Autowired;
import com.github.jsoncat.annotation.springmvc.GetMapping;
import com.github.jsoncat.annotation.springmvc.PathVariable;
import com.github.jsoncat.annotation.springmvc.PostMapping;
import com.github.jsoncat.annotation.springmvc.RequestBody;
import com.github.jsoncat.annotation.springmvc.RequestParam;
import com.github.jsoncat.annotation.springmvc.RestController;

import java.util.List;

/**
* @author shuang.kou
* @createTime 2020年09月24日 14:52:00
**/
@RestController("/user")
public class UserController {
@Autowired
private UserService userService;

@GetMapping
public User get(@RequestParam("name") String name, @RequestParam("des") String des, @RequestParam("age") Integer age) {
return new User(name, des, age);
}

@GetMapping("/{id}")
public User get(@PathVariable("id") Integer id) {
return userService.get(id);
}

@PostMapping
public List<User> create(@RequestBody UserDto userDto) {
return userService.create(userDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.demo;
package com.github.demo.user;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/github/demo/user/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.demo.user;

import com.github.jsoncat.annotation.ioc.Component;
import com.github.jsoncat.annotation.springmvc.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author shuang.kou
* @createTime 2020年09月30日 00:14:00
**/
@Component
public class UserService {
private Integer id = 1;

private final Map<Integer, User> users = new HashMap<Integer, User>() {
{
put(1, new User("盖伦", "德玛西亚", 22));
}
};

public User get(Integer id) {
return users.get(id);
}

public List<User> create(@RequestBody UserDto userDto) {
users.put(++id, new User(userDto.getName(), userDto.getDes(), userDto.getAge()));
return new ArrayList<>(users.values());

}

public void say() {
System.out.println("UserService say 你真帅!");
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/github/jsoncat/JsonCatApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class JsonCatApplication {
public static void main(String[] args) {
Banner.printBanner();
ApplicationContext applicationContext = ApplicationContext.getApplicationContext();
applicationContext.loadRoutes("com.github.demo");
applicationContext.run("com.github.demo");
HttpServer httpServer = new HttpServer();
httpServer.start();
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/github/jsoncat/annotation/ioc/Autowired.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.jsoncat.annotation.ioc;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

}
14 changes: 14 additions & 0 deletions src/main/java/com/github/jsoncat/annotation/ioc/Component.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.jsoncat.annotation.ioc;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
String name() default "";
}
16 changes: 16 additions & 0 deletions src/main/java/com/github/jsoncat/annotation/ioc/Qualifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.jsoncat.annotation.ioc;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Qualifier {
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.jsoncat.annotation;
package com.github.jsoncat.annotation.springmvc;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.jsoncat.annotation;
package com.github.jsoncat.annotation.springmvc;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.jsoncat.annotation;
package com.github.jsoncat.annotation.springmvc;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.jsoncat.annotation;
package com.github.jsoncat.annotation.springmvc;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.jsoncat.annotation;
package com.github.jsoncat.annotation.springmvc;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.jsoncat.annotation;
package com.github.jsoncat.annotation.springmvc;


import com.github.jsoncat.annotation.ioc.Component;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -11,6 +13,7 @@
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface RestController {
String value() default "";
}
Loading

0 comments on commit 1cb0a88

Please sign in to comment.