相当于web.xml
package pers.hanchao.configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* <p>实现了WebApplicationInitializer接口,是web.xml的替代方式</p>
* @author hanchao 2018/1/20 19:40
**/
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* <p>用于获取Spring应用容器的配置文件</p>
* @author hanchao 2018/1/20 19:55
**/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{MySpringMVCConfiguration.class};
}
/**
* <p>获取Spring MVC应用容器</p>
* @author hanchao 2018/1/20 19:55
**/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[0];
}
/**
* <p>负责指定需要由DispatcherServlet映射的路径</p>
* @author hanchao 2018/1/20 19:56
**/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
相当于spring-mvc-servlet.xml
package pers.hanchao.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
//声明这是一个配置文件,类似于applicationcontext.xml
@Configuration
//开启注解,等同于<mvc:annotation-driven/>
@EnableWebMvc
//开启自动装配,等同于 context:component-scan base-package="pers.hanchao.*"
@ComponentScan(basePackages = "pers.hanchao.*")
public class MySpringMVCConfiguration {
/**
* 视图解析器,相当于:
* <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
* <property name="prefix" value="/"/>
* <property name="suffix" value=".jsp"/>
* </bean>
*/
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-61616-2.html
我在台服玩过网游