spring-javaConfig-无xml的方式
原创大约 1 分钟
起一个maven项目,配置pom文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
- servlet必须要3.0以上
配置ApplicationConfig.class,相当于spring-context.xml
package com.wyl.security.springmvc.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
* @Description SpringAppliction 相当于spring-context.xml
* @Author YiLong Wu
* @Date 2020/2/3 20:28
* @Version 1.0.0
*/
(basePackages = "com.wyl.security"
,excludeFilters = {.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
public class ApplictionConfig {
// 配置除了controller以外的bean,例如:数据库连接池,业务bean,事务管理器
}
配置WebMvcConfig,相当于spring-mvc.xml
package com.wyl.security.springmvc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* @Description WebMvc 相当于spring-mvc.xml
* @Author YiLong Wu
* @Date 2020/2/3 20:35
* @Version 1.0.0
*/
(basePackages = "com.wyl.security"
,includeFilters = {.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
public class WebMvcConfig extends WebMvcConfigurationSupport {
// 配置视图解析器
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/views/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
// 配置视图控制器,初始化web时直接访问,无需@controller
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
}
}
配置SpringApplicationInit,相当于web.xml
package com.wyl.security.init;
import com.wyl.security.springmvc.config.ApplictionConfig;
import com.wyl.security.springmvc.config.WebMvcConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* @Description SpringApplicationInit 相当于web.xml
* @Author YiLong Wu
* @Date 2020/2/3 20:44
* @Version 1.0.0
*/
public class SpringApplicationInit extends AbstractAnnotationConfigDispatcherServletInitializer {
// 加载spring容器 相当于加载spring-context.xml
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ApplictionConfig.class};
}
// 相当于加载spring-mvc.xml
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebMvcConfig.class};
}
// 相当于加载 url-mapping
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
- web的启动 SpringApplicationInit 继承 AbstractAnnotationConfigDispatcherServletInitializer,实质上,实现了WebApplicationInitializer,从而扫描了它的子类,并加载到容器中
配置maven的tomcat7插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>