栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot常用配置(持续更新)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot常用配置(持续更新)

自动装配原理
  1. SpringBoot启动会加载大量的自动配置类xxxAutoConfiguration
  2. xxxAutoConfiguration会使用@EnableConfigurationProperties注解绑定配置文件中的属性值
  3. 给容器中的自动配置类添加组件时,会从xxxProperties类中获取指定的属性值,只需要在配置文件中指定这些属性即可.
  4. 配置文件中的key和xxxProperties中@ConfigurationProperties注解的prefix属性值一一对应

pom.xml
  • spring-boot-dependencies :核心依赖在父工程中

启动器starter :Springboot的启动场景

  • 
      org.springframework.boot
      spring-boot-starter
      2.3.7.RELEASE
    
    
  • 比如spring-boot-starter-web,会自动导入web环境所有的依赖

  • springboot会将所有的功能场景,都变成一个个的启动器

  • 要使用什么功能,就找到对应的启动器 starter


springboot的配置文件 application.yaml

通过查看原码(ResourceProperties类)
静态资源默认访问地址

  • classpath:/meta-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

连接数据库,配置数据源

pom.xml引入相关依赖

  
      mysql
      mysql-connector-java
      8.0.27
  
  
      org.springframework.boot
      spring-boot-starter-jdbc
  

application.yaml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

测试

@SpringBootTest
class SpringbootTestApplicationTests {
    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        System.out.println(dataSource.getClass());
        System.out.println(dataSource.getConnection());
    }
}

整合mybatis

pom.xml引入相关依赖


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.2

application.yaml

mybatis:
  type-aliases-package: com.hyj.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

Dao层使用Mapper注解,并DI注入到容器

@Mapper
@Repository
public interface UserMapper {
    public User queryById(@Param("id") int id);
}

Service层调用Dao层,并DI注入到容器

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public User queryById(int id) {
        return userMapper.queryById(id);
    }
}

mapper-locations:的文件夹下创建相对应xml




    
        select * from mybatis.user where id = #{id};
    

测试

@SpringBootTest
class SpringbootTestApplicationTests {
    @Autowired
    private UserService userService;
    @Test
    void contextLoads() {
        System.out.println(userService.queryById(1));
    }
}

Druid 监控
@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }
    //后台监控: web.xml
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        HashMap initParameters = new HashMap<>();
        //增加配置
        initParameters.put("loginUsername","admin");  //登录key 固定 loginUsername loginPassword
        initParameters.put("loginPassword","123456");	
        //允许谁可以访问
        //initParameters.put("allow","127.0.0.2");
        //禁止谁访问 禁止ip访问
//        initParameters.put("deny","127.0.0.1");
        bean.setInitParameters(initParameters);
        return bean;
    }

    //filter
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean<>();
     bean.setFilter(new WebStatFilter());
        HashMap initParameters = new HashMap<>();
        //这些东西不进行统计
        initParameters.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(initParameters);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}

配置完成后访问工程路径/druid,通过设置好的账号密码登录后台监控页面


自定义WebMvcConfigurer
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    public static class MyViewResolver implements ViewResolver {
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
    //自定义视图解析器
    @Bean
    public ViewResolver MyViewResolver(){
        return new MyViewResolver();
    }
    //自定义国际化相关
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/login","/css/**","/img/**");
    }
}

国际化


自定义LocaleResolver类

public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String lang = httpServletRequest.getParameter("lang");
        Locale aDefault = Locale.getDefault();
        if(lang!=null && !("".equals(lang))) {
            String[] s = lang.split("_");
            aDefault = new Locale(s[0],s[1]);
        }
        return aDefault;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

html层中英文切换传参

中文
英文

添加到自定义的WebMvcConfigurer

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591895.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号