方式一:继承WebMvcConfigurerAdapter类

1、使用一个类来继承

package com.wbg.springJavaConfig.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView; import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration; @Configuration//声明当前类是一个配置类(相当于一个Spring的xml文件)
@EnableWebMvc//若无此注解,WebMvcConfigurerAdapter无效
/**
*
* @ComponentScan 扫描("xxx")包下的@Service、@Controller、@Component、@Repository的类,并注册为Bean
*相当于:<context:component-scan base-package="xxx" />
*/
@ComponentScan("com.wbg.springJavaConfig.controller")
public class SpringConfig extends WebMvcConfigurerAdapter {
/**
* @Bean 相当Spring配置文件bean节点
* 添加一个ViewResolver解析view 配置jsp
* @return InternalResourceViewResolver
* 这里的配置相当于:
* <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
* <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
* <property name="prefix" value="/WEB-INF/jsp/" />
* <property name="suffix" value=".jsp" />
* </bean>
*/
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
return viewResolver;
} }
class WebInitializer implements WebApplicationInitializer {
/**
* <context-param>
* <param-name>contextConfigLocation</param-name>
* <param-value>classpath:spring/spring-*.xml</param-value>
* </context-param>
* <listener>
* <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
* </listener>
*
*
* <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
* <init-param>
* <param-name>contextConfigLocation</param-name>
* <param-value>classpath:spring-web.xml</param-value>
* </init-param>
*
* @param servletContext
*/
public void onStartup(ServletContext servletContext){
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
} }

2、创建jsp

3、使用

方式二:继承AbstractAnnotationConfigDispatcherServletInitializer类

1、创建WebInit继承 :

package com.wbg.springJavaConfig.spring2;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{ContextConfig.class};
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringWebConfig.class};
} @Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}

2、创建:SpringWebConfig类

package com.wbg.springJavaConfig.spring2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView; @Configuration
public class SpringWebConfig {
@Bean
public InternalResourceViewResolver internalResourceViewResolverConfig(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
} @Bean
public StandardServletMultipartResolver multipartResolverConfig(){
return new StandardServletMultipartResolver();
} public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();//对静态资源的请求转发到容器缺省的servlet,而不使用 DispatcherServlet
}
}

3、创建ContextConfig类

package com.wbg.springJavaConfig.spring2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.wbg.springJavaConfig.controller"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
public class ContextConfig {
}

4、使用

demo:https://github.com/weibanggang/SpirngwebConfig.git

Spring使用java代码配置Web.xml进行访问service的更多相关文章

  1. Java发送邮件--web.xml配置,Java代码配置

    前言:我目前总结的使用java发送邮件的方式有两种,分别是在spring框架xml配置文件使用bean标签,另一种方法是把发送功能封装成一个对象,废话不多说上代码, 边看代码边讲解,希望对需要的人能有 ...

  2. WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用

    本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...

  3. SpringMVC4零配置--web.xml

    servlet3.0+规范后,允许servlet,filter,listener不必声明在web.xml中,而是以硬编码的方式存在,实现容器的零配置. ServletContainerInitiali ...

  4. SpringMVC(十四):SpringMVC 与表单提交(post/put/delete的用法);form属性设置encrypt='mutilpart/form-data'时,如何正确配置web.xml才能以put方式提交表单

    SpringMVC 与表单提交(post/put/delete的用法) 为了迎合Restful风格,提供的接口可能会包含:put.delete提交方式.在springmvc中实现表单以put.dele ...

  5. 关于Java Webproject中web.xml文件

    提及Java Webproject中web.xml文件无人不知,无人不识,呵呵呵:系统首页.servlet.filter.listener和设置session过期时限.张口就来,但是你见过该文件里的e ...

  6. 使用Java代码配置MyBatis Generator

    使用MyBatis Generator生成器时,有时候没办法使用xml型的配置文件,比如将Maven项目设置成pom打包方式(<packaging>pom</packaging> ...

  7. Spring 框架配置web.xml 整合web struts

    package cn.itcast.e_web; import java.io.IOException; import javax.servlet.ServletContext; import jav ...

  8. Spring MVC 搭建过程中web.xml配置引入文件的路径问题

    为啥要说一下这么low的问题,因为我是一个比较low的人,哈哈.本来我技术有限,没事干自己撘个环境找找乐趣,结果被各种基础问题,弄的一脸蒙蔽.算了不多说,直接说问题. 1.首先说一下java编译后的文 ...

  9. Struts2配置文件复用代码【web.xml、struts.xml、常量配置】

    web.xml的分发器代码: <!-- 引入struts核心过滤器 --> <filter> <filter-name>struts2</filter-nam ...

随机推荐

  1. 一:HttpClient知识整理

    一:httpclient 简介 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支 ...

  2. docker 数据卷挂载总结

    原文

  3. 理解mouseover,mouseout,mouseenter,mouseleave

    mouseover定义和用法 当鼠标指针位于元素上方时,会发生 mouseover 事件. 该事件大多数时候会与 mouseout 事件一起使用. mouseover() 方法触发 mouseover ...

  4. 洛谷P1072 Hankson 的趣味题(数学)

    题意 题目链接 Sol 充满套路的数学题.. 如果你学过莫比乌斯反演的话不难得到两个等式 \[gcd(\frac{x}{a_1}, \frac{a_0}{a_1}) = 1\] \[gcd(\frac ...

  5. 什么是APP???APP的开发类型又分哪几种???

    开发者们都知道在高端智能手机系统中有两种应用程序: 一种是基于本地(操作系统)运行的APP —-Native App: 一种是基于高端机的浏览器运行的App —-WebApp因为这些高端智能手机(Ip ...

  6. 关于zepto 选择特定值的input 报错问题

    zepto 选择特定值的input  时,需要用单引号或双引号引用这个特定值 否则 报错

  7. Sql-exec

    --显示sql server现有的所有数据库 exec sp_helpdb --查看数据表设置的约束 exec sp_helpconstraint SubjectType --update selec ...

  8. apk下载安装,存储的位置,路径

    PackageInstaller 原理简述 应用安装是智能机的主要特点,即用户可以把各种应用(如游戏等)安装到手机上,并可以对其进行卸载等管理操作.APK是Android Package的缩写,即An ...

  9. redis 在linux安装

    转自:http://futeng.iteye.com/blog/2071867 下载 官网下载 安装 tar zxvf redis-2.8.9.tar.gz cd redis-2.8.9 #直接mak ...

  10. POI 导出excel带小数点的数字格式显示不对解决方法

    最近看到了一个问题就是java导出excel中带小数点的数字显示不对, 比如我想在excel中第一行显示:  3,000.0 但是在excle中导出的格式总是不带小数点 3000(非文本格式),而且也 ...