前面已经介绍过了spring和springmvc的基本配置过程了,可以得知之前的配置是用xml文件进行配置的,这次我们不使用xml文件就使用java来代替xml文件
1.先写pom.xml文件
4.0.0
org.example
javassm
1.0-SNAPSHOT
war
org.springframework
spring-webmvc
5.1.6.RELEASE
javax.servlet
javax.servlet-api
4.0.0
provided
2.SpringConfig文件(相当于配置spring的xml文件)
package org.javaboy.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
//这个文件的作用就是和传统配置spring的xml文件的作用是一样的
@Configuration
//这个注解的作用是包扫描
@ComponentScan(basePackages = "org.javaboy",useDefaultFilters = true,
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class)})
public class SpringConfig {
}
这里通过@ComponentScan进行扫描,扫描除了controller的所有类(原理和xml中的原理相同)
3.SpringMvcConfig文件(和配置springmvc的xml文件作用相同)
package org.javaboy.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
//这个类的作用和springmvc的配置类是一样的
@Configuration
//这个注解是包扫描 这里这扫描controller
@ComponentScan(basePackages = "org.javaboy",useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Configuration.class)})
public class SpringMvcConfig {
}
这里@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Configuration.class)的作用是将SpringConfig加载进来,如果不写spring的配置就加载不进来。
4.webInit文件(目的:将spring和springmvc的配置加载进来)
package org.javaboy.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
//这个类用来加载spring和springmvc配置
public class WebInit implements WebApplicationInitializer {
//当容器启动时这个方法会被触发
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
//创建AnnotationConfigWebApplicationContext对象
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//设置servletContext
ctx.setServletContext(servletContext);
//注册springmvc的bean
ctx.register(SpringMvcConfig.class);
//在springvc中创建核心控制器
ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(ctx));
//在springmvc中创建映射器
springmvc.addMapping("/");
//启动步骤设置为1(最先执行)
springmvc.setLoadOnStartup(1);
}
}
上面文件的作用和web.xml文件的作用一模一样,都是要将spring和springmvc的配置类加载进来
5.service类和controller类
package org.javaboy.controller;
import org.javaboy.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String hello() {
return helloService.sayHello();
}
}
package org.javaboy.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String sayHello() {
return "hello javassm!";
}
}
6.点击启动tomcat
访问路径:http://localhost:8080/javassm_war/hello
显示: