最近心血来潮,想自己写一个简单的Spring框架,过程中有啥问题的请各位大佬指出
开发准备:JDK:1.8 开发工具:Idea jar:servlet-api.jar
下面开始;
1.新建一个java项目,项目结构下图:
各文件的代码:
package controller;
import annotation.Autowired;
import annotation.RequestMapping;
import annotation.RestController;
import service.TestService;
/**
* @program: JavaStudy
* @description: 测试控制器
* @author: Deng.Hua
* @create: 2019-02-22 09:08
**/
@RestController
@RequestMapping("cs")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/test")
public void Test(){
testService.Test();
}
}
package service;
/**
* @program: JavaStudy
* @description: 测试服务
* @author: Deng.Hua
* @create: 2019-02-22 09:10
**/
public interface TestService {
void test();
}
package service.impl;
import annotation.Service;
import service.TestService;
/**
* @program: JavaStudy
* @description:
* @author: Deng.Hua
* @create: 2019-02-22 09:11
**/
@Service
public class TestServiceImpl implements TestService {
@Override
public void test() {
System.out.println("这是一个简单的spring框架");
}
}
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Autowired {
}
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RestController {
}
package annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Service {
}
package servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @program: SpringMvc
* @description: servlet
* @author: Deng.Hua
* @create: 2019-02-22 09:43
**/
public class DisServlet extends HttpServlet {
@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);
}
@Override
public void init() throws ServletException {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>DisServlet</servlet-name>
<servlet-class>servlet.DisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
至此,项目的初步准备已经完成 下一篇