无配置文件 搭建

今天搞了个spring3.1+servlet3 无配置项目,事到此时终于算出了一个小东东了

记录于此,已被后用。

1.创建项目,使用eclipse+tomcat7

2.导入spring的包,除此之外还导入了asm-2.2.2.jar、cglib-nodep-2.1_3.jar(这个别导入hibernate的了)、jstl-1.0.2.jar、jackson-all-1.9.1.jar、commons-logging-1.1.1.jar、mysql-connector-java-5.1.12-bin.jar

3.删除了web.xml,准备二个文件:

   jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/db_smg?useUnicode=true&charaterEncoding=UTF-8
jdbc.username=root
jdbc.password=****

  properties-config.xml

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
  <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
  <mvc:resources mapping="/static/**" location="/static/"/>  
</beans>

 4.编写启动容器加载的java文件和java 程序代替的配置

Initializer.java

package com.acme.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class Initializer implements WebApplicationInitializer {

	public void onStartup(ServletContext servletContext) throws ServletException {
		// TODO Auto-generated method stub
		AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
		mvcContext.register(MvcConfig.class);
		  ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
		    "dispatcher", new DispatcherServlet(mvcContext));
		  dispatcher.setLoadOnStartup(1);
		  dispatcher.addMapping("/");
	}

}

 MvcConfig.java

package com.acme.web;

import java.lang.reflect.Method;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.acme.DBConfig;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring.my")
@Import(DBConfig.class)
public class MvcConfig  extends WebMvcConfigurerAdapter{
	
	@Bean
	 public ViewResolver contentNegotiatingViewResolver() {
	  InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
	  internalResourceViewResolver.setPrefix("/views/");
	  internalResourceViewResolver.setSuffix(".jsp");
	  internalResourceViewResolver.setViewClass(JstlView.class);
	  return internalResourceViewResolver;
	 }
	@Bean
	public ModelAndViewResolver modelAndViewResolver(){
		return new ModelAndViewResolver() {
			@Override
			public ModelAndView resolveModelAndView(Method arg0, Class arg1,
					Object arg2, ExtendedModelMap arg3, NativeWebRequest arg4) {
				// TODO Auto-generated method stub
				return UNRESOLVED;
			}
		};
	}
	@Override
	public void addArgumentResolvers(
			List<HandlerMethodArgumentResolver> argumentResolvers) {
		// TODO Auto-generated method stub
		super.addArgumentResolvers(argumentResolvers);
	}
	@Override
	public void addFormatters(FormatterRegistry registry) {
		// TODO Auto-generated method stub
		super.addFormatters(registry);
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO Auto-generated method stub
		super.addInterceptors(registry);
	}
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub
//		registry.addResourceHandler("");
		super.addResourceHandlers(registry);
	}
	@Override
	public void addReturnValueHandlers(
			List<HandlerMethodReturnValueHandler> returnValueHandlers) {
		// TODO Auto-generated method stub
		super.addReturnValueHandlers(returnValueHandlers);
	}
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// TODO Auto-generated method stub
		super.addViewControllers(registry);
	}
	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		// TODO Auto-generated method stub
//		configurer.enable();
		super.configureDefaultServletHandling(configurer);
	}
	@Override
	public void configureHandlerExceptionResolvers(
			List<HandlerExceptionResolver> exceptionResolvers) {
		// TODO Auto-generated method stub
		super.configureHandlerExceptionResolvers(exceptionResolvers);
	}
	@SuppressWarnings("rawtypes")
	@Override
	public void configureMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		// TODO Auto-generated method stub
		converters.add(new MappingJacksonHttpMessageConverter());
		converters.add(new SourceHttpMessageConverter());
		converters.add(new FormHttpMessageConverter());
		converters.add(new StringHttpMessageConverter());
		converters.add(new ByteArrayHttpMessageConverter());
		super.configureMessageConverters(converters);
	}
	@Override
	public Validator getValidator() {
		// TODO Auto-generated method stub
		Validator v= super.getValidator();
		return v;
	}
	
}

 DBConfig.java

package com.acme;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class DBConfig {
	private @Value("${jdbc.driver}")
	String driver;
	private @Value("${jdbc.url}")
	String url;
	private @Value("${jdbc.username}")
	String username;
	private @Value("${jdbc.password}")
	String password;
	public @Bean
	DataSource dataSource() {
		System.out.println("OK-------------------------");
		DriverManagerDataSource dataSource=new DriverManagerDataSource(url, username, password);
		dataSource.setDriverClassName(driver);
		return dataSource;
	}
	public @Bean
	JdbcTemplate jdbcTemplate(){
		return new JdbcTemplate(dataSource());
		
	}
}

 5.创建好各个目录(根据配置程序和仅有的一个xml)

6. 编写测试程序,启动项目测试(附件含有)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值