springboot

博客介绍了Spring Boot,它简化传统SSM框架配置,用实体bean和注解替代繁杂配置文件,还需application.properties或.yml文件。同时讲解了Spring Boot中的注解,如启动类相关注解,以及将配置文件转化为bean的注解,还对@EnableWebMvc注解进行了详细分析。

springboot介绍

springboot简化传统ssm框架中繁杂的配置文件,取而代之的是实体bean以及注解,他还需要一个配置文件application.properties或者.yml

springboot中的注解

1.启动类

//核心注解,通过启动main方法可以直接启动项目,用内置的tomcat
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)
//扫包
@MapperScan("net.wanho.mapper")
public class SsmDemoApplication {

    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    public static void main(String[] args) {
        SpringApplication.run(SsmDemoApplication.class, args);
    }
}

2.配置文件转化成bean

@Configuration
@Bean
@EnableWebMvc

@EnableWebMvc是使用Java 注解快捷配置Spring Webmvc的一个注解。在使用该注解后配置一个继承于WebMvcConfigurerAdapter的配置类即可配置好Spring Webmvc。

通过查看@EnableWebMvc的源码,可以发现该注解就是为了引入一个DelegatingWebMvcConfiguration Java 配置类。并翻看DelegatingWebMvcConfiguration的源码会发现该类似继承于WebMvcConfigurationSupport的类。

其实不使用@EnableWebMvc注解也是可以实现配置Webmvc,只需要将配置类继承于WebMvcConfigurationSupport类即可

package net.wanho.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
//只要在类上出现@Configuration就说明他是配置
@Configuration

@EnableWebMvc
public class MyMvcConfig extends WebMvcConfigurerAdapter  {
    @Bean
    public FilterRegistrationBean siteMeshFilter(){
        FilterRegistrationBean filter = new FilterRegistrationBean();
        Sitemesh3Filter siteMeshFilter = new Sitemesh3Filter();
        filter.setFilter(siteMeshFilter);
        return  filter;

    }

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");

        resolver.setSuffix(".jsp");
        return resolver;
    }

    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

application.yml

 //端口号
server :
  port : 8088
spring :
//数据库连接池信息
  datasource:
    driver-class-name : com.mysql.jdbc.Driver
    url : jdbc:mysql://localhost:3306/test?characterEncoding=utf8
    username : root
    password : root
 //   redis缓存数据库的ip+端口
  redis:
    database: 0
    host: 172.16.219.166
    port: 6379
    password:
//    redis连接池
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    timeout: 0
    //分页插件
pagehelper:
 helperDialect: mysql
 reasonable: true
 supportMethodsArguments: true
 params: count=countSql
#mybatis:
  #config-location : classpath:net/wanho/mapper/*.xml
  //配置mapper的扫描路径
mybatis:
  mapper-locations: classpath:net/wanho/mapper/*.xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值