-
方法一:通过扫描注解整合
-
编写servlet
@WebServlet(name="MyServlet",urlPatterns="/first") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Hello World!!!!!!!!!!"); } }
2. 编写启动类
@SpringBootApplication
@ServletComponentScan //该注解写上以后会自动寻找包含@WebServlet注解的类,并实例化
public class ServletSpringApplication {
public static void main(String[] args) {
SpringApplication.run(ServletSpringApplication.class, args);
}
}
-
通过@Bean注解来完成对Servlet组件的注册
1. 编写Servlet (即方法一不用加上注解)
//@WebServlet(name="MyServlet",urlPatterns="/first")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Hello World!!!!!!!!!!");
}
}
2. 编写启动类
@SpringBootApplication
//@ServletComponentScan //该注解写上以后会自动寻找包含@WebServlet注解的类,并实例化
public class ServletSpringApplication {
public static void main(String[] args) {
SpringApplication.run(ServletSpringApplication.class, args);
}
@Bean
public ServletRegistrationBean getBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new MyServlet());
bean.addUrlMappings("/hello");
return bean;
}
}
俩种方法在运行后,通过访问,最后在控制台上都会打印
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2019-03-04 15:51:43.708 INFO 4808 --- [ main] c.example.demo.ServletSpringApplication : Starting ServletSpringApplication on hzp-PC with PID 4808 (started by hzp in G:\eclipse\Java工程\servletSpring)
2019-03-04 15:51:43.746 INFO 4808 --- [ main] c.example.demo.ServletSpringApplication : No active profile set, falling back to default profiles: default
2019-03-04 15:51:45.590 INFO 4808 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-03-04 15:51:45.631 INFO 4808 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-03-04 15:51:45.631 INFO 4808 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-04 15:51:45.647 INFO 4808 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_162\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_162/bin/server;C:/Program Files/Java/jre1.8.0_162/bin;C:/Program Files/Java/jre1.8.0_162/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\MySQL\MySQL Server 5.5\bin;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;D:\eclipse\Apache\maven\bin;D:\c++\Qt5.3.1\Tools\mingw482_32\bin;D:\c++\Qt5.3.1\5.3\mingw482_32\bin;C:\Program Files\mingw-w64\x86_64-5.3.0-win32-seh-rt_v4-rev0\mingw64\bin;F:\Program Files\Git\cmd;G:\Program Files\Tortoise\bin;;G:\软件\spring-tool-suite-4-4.1.1.RELEASE-e4.10.0-win32.win32.x86_64\sts-4.1.1.RELEASE;;.]
2019-03-04 15:51:45.802 INFO 4808 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-04 15:51:45.802 INFO 4808 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1967 ms
2019-03-04 15:51:46.160 INFO 4808 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-04 15:51:46.450 INFO 4808 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-04 15:51:46.454 INFO 4808 --- [ main] c.example.demo.ServletSpringApplication : Started ServletSpringApplication in 3.435 seconds (JVM running for 3.883)
Hello World!!!!!!!!!!