Spring项目版

master
MKing 2023-10-30 08:03:50 +08:00
commit c8f17460aa
53 changed files with 537 additions and 0 deletions

8
.idea/.gitignore vendored 100644
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

13
.idea/compiler.xml 100644
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="maven-jedis" />
</profile>
</annotationProcessing>
</component>
</project>

View File

@ -0,0 +1,36 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
<option name="TOP_LEVEL_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="INNER_CLASS_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="METHOD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
</value>
</option>
<option name="FIELD_OPTIONS">
<value>
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
<option name="REQUIRED_TAGS" value="" />
</value>
</option>
<option name="IGNORE_DEPRECATED" value="false" />
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
<option name="myAdditionalJavadocTags" value="date" />
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

17
.idea/misc.xml 100644
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml 100644
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

2
maven-jedis.iml 100644
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

47
pom.xml 100644
View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jedis.query</groupId>
<artifactId>maven-jedis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>maven-jedis Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.79</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,34 @@
package example.controller;
import example.enjoy.EnjoyAutowired;
import example.enjoy.EnjoyController;
import example.enjoy.EnjoyRequestMapping;
import example.enjoy.EnjoyRequestParam;
import example.service.SpringMvcService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@EnjoyController
@EnjoyRequestMapping("/SpringMvc")
public class SpringMvcController {
@EnjoyAutowired("SpringMvcServiceImpl") //map.get(key)
private SpringMvcService service;
@EnjoyRequestMapping("/query")
public void query(HttpServletRequest request, HttpServletResponse response,
@EnjoyRequestParam("name") String name, @EnjoyRequestParam("age") String age){
try {
PrintWriter pw=response.getWriter();
String result = service.query(name, age);
pw.write(result);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,11 @@
package example.enjoy;
import javax.swing.*;
import java.lang.annotation.*;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented //javadoc
public @interface EnjoyAutowired {
String value() default "";
}

View File

@ -0,0 +1,10 @@
package example.enjoy;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented //javadoc
public @interface EnjoyController {
String value() default "";
}

View File

@ -0,0 +1,10 @@
package example.enjoy;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented //javadoc
public @interface EnjoyRequestMapping {
String value() default "";
}

View File

@ -0,0 +1,10 @@
package example.enjoy;
import java.lang.annotation.*;
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented //javadoc
public @interface EnjoyRequestParam {
String value() default "";
}

View File

@ -0,0 +1,10 @@
package example.enjoy;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented //javadoc
public @interface EnjoyService {
String value() default "";
}

View File

@ -0,0 +1,11 @@
package example.service.Impl;
import example.service.SpringMvcService;
public class SpringMvcServiceImpl implements SpringMvcService {
@Override
public String query(String name, String age) {
return "name==="+name+";age==="+age;
}
}

View File

@ -0,0 +1,5 @@
package example.service;
public interface SpringMvcService {
String query(String name,String age);
}

View File

@ -0,0 +1,218 @@
package example.servlet;
import example.controller.SpringMvcController;
import example.enjoy.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.*;
public class DispatcherServlet extends HttpServlet {
List<String> classNames=new ArrayList<String>();
Map<String,Object> beans=new HashMap<String,Object>();
Map<String,Object> hanlderMap=new HashMap<String,Object>();
/**
*
*/
private static final long serialVersionUID=1L;
public void init(ServletConfig config){
//扫描出所有的bean
scanPackage("org.example");
doInstance();//根据全类名创建bean
doIOC();//根据bean进行依赖注入
}
private void scanPackage(String basePackage){
URL url=this.getClass().getClassLoader().getResource("/"+basePackage.replaceAll("\\.","/"));
String fileSet=url.getFile();
File file = new File(fileSet);
String[] filesStr = file.list();
for (String path : filesStr) {
File filePath = new File(filesStr + path);
if (filePath.isDirectory()){
scanPackage(basePackage+"."+path);
}else {
//加入list
classNames.add(basePackage+"."+filePath.getName());//全类名
}
}
}
//根据扫描 List全类名进行实例化
private void doInstance(){
if (classNames.size() <=0){
System.out.println("包扫描失败");
return;
}
//list的class类 对这些类进行
for (String className : classNames) {
String cn = className.replace(".class","");
try {
Class<?> clazz = Class.forName(cn);
if (clazz.isAnnotationPresent(EnjoyController.class)){
Object instance = clazz.newInstance();//创建控制类
EnjoyRequestMapping requestMapping = clazz.getAnnotation(EnjoyRequestMapping.class);
String rmvalue=requestMapping.value();
beans.put(rmvalue,instance);
}else if (clazz.isAnnotationPresent(EnjoyService.class)){
EnjoyService service=clazz.getAnnotation(EnjoyService.class);
Object instance=clazz.newInstance();
beans.put(service.value(),instance);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
/**
* servicecontroller
*/
public void doIOC() {
if (beans.entrySet().size() <= 0) {
System.out.println("没有实例化的类");
}
//把map里的所有类实例化遍历出来
for (Map.Entry<String, Object> entry : beans.entrySet()) {
Object instance = entry.getValue();
Class<?> Class = instance.getClass();
if (Class.isAnnotationPresent(EnjoyController.class)) {
Field[] fields = Class.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(EnjoyAutowired.class)) {
EnjoyAutowired auto = field.getAnnotation(EnjoyAutowired.class);
String key = auto.value();
field.setAccessible(true);
try {
field.set(instance, beans.get(key));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
continue;
}
}
}else {
continue;
}
}
}
private void buildUrlMapping(){
if (beans.entrySet().size() <=0){
System.out.println("没有类的实例化-----");
return;
}
for (Map.Entry<String, Object> entry : beans.entrySet()) {
Object instance = entry.getValue();
Class<?> clazz = instance.getClass();
if (clazz.isAnnotationPresent(EnjoyController.class)){
EnjoyRequestMapping requestMapping = clazz.getAnnotation(EnjoyRequestMapping.class);
String classPath = requestMapping.value();
Method[] methods=clazz.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(EnjoyRequestMapping.class)){
EnjoyRequestMapping methodMapping=method.getAnnotation(EnjoyRequestMapping.class);
String methodPath = methodMapping.value();
hanlderMap.put(classPath+methodPath,method);
}else {
continue;
}
}
}else {
continue;
}
}
}
/**
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
String uri = req.getRequestURI();
String context = req.getContextPath();
String path = uri.replace(context, "");
Method method = (Method) hanlderMap.get(path);
SpringMvcController istance=(SpringMvcController) beans.get("/"+path.split("/")[1]);
Object[] args = hand(req, resp, method);
try {
method.invoke(istance, args);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private static Object[] hand(HttpServletRequest request, HttpServletResponse response, Method method) {
//拿到待执行的方法有哪些参数
Class<?>[] paramClazzs = method.getParameterTypes();
//根据参数的个数new 一个参数的数组将方法的所有参数赋值到args
Object[] args = new Object[paramClazzs.length];
int args_i = 0;
int index = 0;
for (Class<?> paramClazz : paramClazzs) {
if (ServletRequest.class.isAssignableFrom(paramClazz)) {
args[args_i++] = request;
}
if (ServletResponse.class.isAssignableFrom(paramClazz)) {
args[args_i++] = response;
}
Annotation[] paramAns = method.getParameterAnnotations()[index];
if (paramAns.length > 0) {
for (Annotation paramAn : paramAns) {
if (EnjoyRequestParam.class.isAssignableFrom(paramAn.getClass())) {
EnjoyRequestParam rp = (EnjoyRequestParam) paramAn;
args[args_i++] = request.getParameter(rp.value());
}
}
}
index++;
}
return args;
}
}

View File

@ -0,0 +1,19 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- SpringMvc的核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>example.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

View File

@ -0,0 +1,3 @@
artifactId=maven-jedis
groupId=com.jedis.query
version=1.0-SNAPSHOT

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,19 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- SpringMvc的核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>example.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -0,0 +1,5 @@
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

View File

@ -0,0 +1,9 @@
example\service\Impl\SpringMvcServiceImpl.class
example\enjoy\EnjoyController.class
example\servlet\DispatcherServlet.class
example\enjoy\EnjoyService.class
example\enjoy\EnjoyAutowired.class
example\service\SpringMvcService.class
example\controller\SpringMvcController.class
example\enjoy\EnjoyRequestMapping.class
example\enjoy\EnjoyRequestParam.class

View File

@ -0,0 +1,9 @@
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\controller\SpringMvcController.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\enjoy\EnjoyController.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\service\SpringMvcService.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\service\Impl\SpringMvcServiceImpl.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\enjoy\EnjoyRequestParam.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\servlet\DispatcherServlet.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\enjoy\EnjoyRequestMapping.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\enjoy\EnjoyService.java
C:\Users\李闯\AppData\Local\Temp\Temp0db3f973-e829-414b-9433-ceaf8f4759fe_Test1-master.zip\maven-jedis\src\main\java\example\enjoy\EnjoyAutowired.java