使用Springboot快速搭建项目
2017-01-25 13:27
821 查看
1. 项目入口
package com.kyx.app; import com.kyx.app.bean.TestSpringSource; import com.kyx.app.spring.conf.AppConf; import com.zaxxer.hikari.HikariDataSource; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; @SpringBootApplication @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) @ServletComponentScan @MapperScan(basePackages = "com.kyx.app.mybatis.mapper") public class DemoApplication { private Logger logger = LoggerFactory.getLogger(DemoApplication.class); @Autowired private AppConf appConf; // @Value("${mybatis.mapperLocations}") // private String mapperLocations; // @Value("${mybatis.typeAliasPackage}") // private String typeAliasPackage; // @Bean // public FilterRegistrationBean get() { // SessionFilter sessionFilter = new SessionFilter(); // return new FilterRegistrationBean(sessionFilter); // } // @Bean // public ServletRegistrationBean servletRegistrationBean(){ // return new ServletRegistrationBean(new MyServlet(),"/lottery/go"); // } // @Bean // public ServletListenerRegistrationBean servletListenerRegistrationBean(){ // return new ServletListenerRegistrationBean(new MyServletListener()); // } // @Bean // public Filter sessionFilter(){ // SessionFilter sessionFilter = new SessionFilter(); // return sessionFilter; // } @Bean @ConfigurationProperties(prefix = "spring.dataSource") public DataSource dataSources(){ return new HikariDataSource(); } // @Bean // public SqlSessionFactory sqlSessionFactory() throws Exception{ // SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); // sessionFactory.setDataSource(dataSource()); // // 使用xml,必须 //// logger.info("=======>application.properties: {}",mapperLocations); //// logger.info("=======>application.properties: {}",typeAliasPackage); // // sessionFactory.setTypeAliasesPackage(appConf.getTypeAliasPackage()); // sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(appConf.getMapperLocations())); // return sessionFactory.getObject(); // } // @Bean // public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{ // SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); // sessionFactory.setDataSource(dataSource); // // 使用xml,必须 //// logger.info("=======>application.properties: {}",mapperLocations); //// logger.info("=======>application.properties: {}",typeAliasPackage); // // sessionFactory.setTypeAliasesPackage(appConf.getTypeAliasPackage()); // sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(appConf.getMapperLocations())); // return sessionFactory.getObject(); // } // // @Bean // public SqlSessionDaoSupport sqlSessionDaoSupport(SqlSessionFactory sqlSessionFactory){ // System.out.println(sqlSessionFactory); // SqlSessionDaoSupport sqlSessionDaoSupport = new SqlSessionDaoSupport() { // @Override // public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { // super.setSqlSessionFactory(sqlSessionFactory); // } // }; // return sqlSessionDaoSupport; // } @Bean public TestSpringSource setVersion(){ TestSpringSource test = new TestSpringSource(); test.setVersion("1.0"); return test; } public static void main(String[] args) { //System.setProperty("spring.devtools.restart.enabled", "true"); // TestSpringSource test = new TestSpringSource(); // test.setVersion("1.0"); //SpringApplication.run("classpath:applicationContext.xml", args); SpringApplication.run(DemoApplication.class, args); } }
1. 所有需要项目启动就加载的东西需要在@Configuration(@SpringBootApplication
继承了它)的类中实例化,如filter、listener等。
2. 实例化方式:
- @Bean 手动实例化
- @ServletComponentScan 和@WebFilter、 @WebListener、 @WebServlet
2. @RestController使用,使得URL更加restful
package com.kyx.config.api; import com.kyx.config.domain.DeviceConfig; import com.kyx.config.domain.DeviceConfigExample; import com.kyx.config.repository.mapper.DeviceConfigMapper; import com.kyx.config.service.DeviceConfigService; import com.kyx.config.support.view.Ok; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import java.util.List; /** * Created by XuHe on 17/1/9. */ @RestController @RequestMapping("/device") public class DeviceConfigController { @Autowired private DeviceConfigMapper mapper; @Autowired private DeviceConfigService service; @RequestMapping(path = "sn/{sn}" , method = RequestMethod.GET) public Ok getBySN(@PathVariable String sn){ return new Ok(service.getBySN(sn)); } @RequestMapping(path = "sn/{sn}" , method = RequestMethod.PUT) public String update(){ return "ok"; } }
相关文章推荐
- 使用maven 快速搭建springboot项目
- 【SpringBoot教程二】:RESTfull API简单项目的快速搭建
- 使用idea+springboot+Mybatis搭建web项目
- 4000 教你十分钟快速搭建springBoot项目实战
- 【Spring Boot】使用Spring Boot来搭建Java web项目以及开发过程
- Spring Boot-RESTfull API简单项目的快速搭建
- 003,spring boot RESTfull API简单项目的快速搭建
- Spring Cloud搭建微服务架构----使用Spring boot开发web项目
- springboot (1) 快速搭建简单项目
- 【Spring Boot教程】快速搭建spring boot项目工程
- 使用SpringBoot快速搭建WebSocket实现消息推送
- Spring Boot 初探[1]--快速搭建Spring Boot项目
- 使用SpringBoot快速搭建服务器
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- Maven快速使用教程(二) spring boot 项目构建
- 快速搭建Spring Boot项目
- 如何使用eclipse搭建SpringBoot项目
- Spring Boot 学习(一) 快速搭建SpringBoot 项目
- SpringBoot1-spirng项目快速搭建+maven+spring的基本配置
- springboot入门,简单能跑的项目快速搭建