springBoot 整合servlet

2021-03-02 19:27

阅读:373

标签:autoconf   jdb   img   space   关于   src   order   auth   exclude   

一:用idea 创建 springboot 项目:

详情请参考:《使用IDEA创建一个springboot项目》



二:具体代码内容;

技术图片


一: FirstServlet


package com.alancode.springboot.Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: springBootAddServlet
 * @description: FristServlet
 *  一:传统的spring 框架增加 servlet 的配置方式
 *   
 *       FirstServlet
 *       com.alancode.springboot.Servlet.FirstServlet
 *   
 *   
 *       FirstServlet
 *       /frist
 *   
 *
 */
/***springBoot  配置 servlet 的配置代码
 * @author Alan_Liu***/
@WebServlet(name="FirstServlet",urlPatterns = "/frist")
public class FirstServlet extends HttpServlet {
    /**
     *
     *
     *@Param: [req, resp]
     *@return
     *@throws
     *@author Alan_Liu
     *@date   2021/2/14 23:22
     *
     **/
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //super.doGet(req,resp);
		System.out.println("------------springBoot 整合 servlet 方式一:注解配置方式------------------------------------------ -----------");
		System.out.println("-----------项目启动时候加载 @WebServlet 注解下的FirstServlet 类 - ---------------------------------");
		System.out.println("-----------当进行http://localhost:8080/frist 的浏览器地址访问后,进输出该打印内容 - ----------------------");
	}
}



二:SecondServlet


package com.alancode.springboot.Servlet;
/**
 * @author Alan_liu
 * @Create 2021/2/14 23:46
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *@program: springBootAddServlet
 *@description: SecondServlet
 *@author: Alan_Liu
 *@create: 2021-02-14 23:46
 */
public class SecondServlet extends HttpServlet {


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//super.doGet(req,resp);
		System.out.println("------------springBoot 整合 servlet 方式二: ------------------------------------------ -----------");
		System.out.println("-----------项目启动SpringBootAddServletApplication2时候加载声明初始化SecondServlet 类  - ---------------------------------");
		System.out.println("-----------当进行http://localhost:8080/second 的浏览器地址访问后,进输出该打印内容 - ----------------------");

	}
}


三:SpringBootAddServletApplication


package com.alancode.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * @author Alan_Liu
 */
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@ServletComponentScan
public class SpringBootAddServletApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootAddServletApplication.class, args);
	}

}

/***
 *
 *
 **注意:
 **关于编写启动器需要注意的问题。
 **启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。
 **但是不能把该文件放到controller平行级的包或者其子包下。
 * ServletComponentScan 含义:
 *   在springboot 启动时候进行扫描@webServlet注解的所有servlet类。并将该类实例化
 *
 * ***/


四:SpringBootAddServletApplication2

package com.alancode.springboot;

import com.alancode.springboot.Servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * @author Alan_Liu
 */
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SpringBootAddServletApplication2 {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootAddServletApplication2.class, args);
	}
	@Bean
	public ServletRegistrationBean getServletRegistrationBean(){
		ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
		bean.addUrlMappings("/second");
		System.out.println("------------springBoot 整合 servlet 方式二: ------------------------------------------ -----------");
		System.out.println("-----------项目启动SpringBootAddServletApplication2时候加载声明初始化SecondServlet 类  - ---------------------------------");
		System.out.println("-----------当进行http://localhost:8080/second 的浏览器地址访问后,进输出该打印内容 - ----------------------");

		return  bean;
	}
}

/***
 *
 *
 **注意:
 **关于编写启动器需要注意的问题。
 **启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。
 **但是不能把该文件放到controller平行级的包或者其子包下。
 * ServletComponentScan 含义:
 *   在springboot 启动时候进行扫描@webServlet注解的所有servlet类。并将该类实例化
 *
 * ***/


五:pom.xml

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    modelVersion>4.0.0modelVersion>
    parent>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-starter-parentartifactId>
        version>2.4.2version>
        relativePath/> 
    parent>
    groupId>com.AlanCode.springBootgroupId>
    artifactId>springBootAddServletartifactId>
    version>0.0.1-SNAPSHOTversion>
    name>springBootAddServletname>
    description>springBootAddServletdescription>
    properties>
        java.version>1.8java.version>
    properties>
    dependencies>
        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-webartifactId>
        dependency>

        dependency>
            groupId>org.springframework.bootgroupId>
            artifactId>spring-boot-starter-testartifactId>
            scope>testscope>
        dependency>
    dependencies>

    build>
        plugins>
            plugin>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

springBoot 整合servlet

标签:autoconf   jdb   img   space   关于   src   order   auth   exclude   

原文地址:https://www.cnblogs.com/ios9/p/14403055.html


评论


亲,登录后才可以留言!