Springboot通过继承spring-boot-starter-web模块能通过启动自带tomcat相关的jar提供给web项目一个容器。
使用Springboot开发Web项目能省略大部分的配置文件,让xml中的配置加入到代码中
通过继承SpringBootServletInitializer抽象类(实现了WebApplicationInitializer接口)并且重写它的configure方法,这样让springboot会会找到SpringBootServletInitializer的子类并解析配置然后启动Servlet容器进行设置
步骤:1、首先创建一个springboot初始项目,通过继承web-parent 继承web模块依赖
2、让启动类通过继承SpringBootServletInitializer并重写configure方法
3、实现自动配置或者自动义各种配置类来实现无配置文件的Web开发
Springboot启动类:
/**
* The Class extend SpringBootServletInitializer let the project support servlet3.0, and the container startup can read the configurations
*/
@SpringBootApplication
@ComponentScan(basePackages = {"work.yanghao"})
public class ProjectApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class,args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ProjectApplication.class);
}
}
如果需要部署到外部的Servlet容器中运行,则必须继承SpringBootServletInitializer然后重写configure方法。
为了能实现自定义配置,需要定义各种模块的配置类,这里配置了数据源和mybatis框架、spring框架的配置:
WebApplicationConfigure.java
@Configuration
public class WebApplicationConfigure implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MVCInterceptor()).addPathPatterns("/intercept");
}
}
DataSourceConfigure.java
/**
* datasource configuration
* @Configuration the annotation can replace the xml file, as the springframework old configure file the application.xml
* @ConditionalOnMissingBean if the bean haven created by spring boot,the method will not be executed
* @Bean the annotation up the method can get a object(bean) as write the xml code(<bean></bean>) in application.xml
*/
@Configuration
@AutoConfigureAfter(WebApplicationConfigure.class)
@PropertySource(value={"classpath:jdbc.properties"},ignoreResourceNotFound=true)
public class DataSourceConfigure {
@Value("${jdbc.driverClassName}")
private String driverClass;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
@ConditionalOnMissingBean
public DataSource dataSource(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMaxActive(20);
dataSource.setMinIdle(10);
dataSource.setDefaultAutoCommit(false);
return dataSource;
}
}
MybatisConfigure.java
@Configuration
@AutoConfigureAfter(DataSourceConfigure.class)
public class MybatisConfigure {
@Bean
@ConditionalOnMissingBean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// set the datasource
sqlSessionFactoryBean.setDataSource(dataSource);
// set mybatis configuration file
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);
// set the pojo package name
sqlSessionFactoryBean.setTypeAliasesPackage("work.yanghao.domain");
// set the mapper path that let the framework scan the mapper files
try {
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mybatis/mappers/*.xml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sqlSessionFactoryBean;
}
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
到此,基本的架构搭建完毕,可以直接在外部容器(需要高版本Servlet容器支持)运行,下一步可以着手开发控制层和业务了,未完待续。。。。。。
项目地址:https://github.com/dlyanghao/springbootwebdemo.git