springboot汇总

添加导入bean配置文件

使用xml配置bean的自动装配,可以参考:二、Spring属性注入

在Application里添加注解:

@ImportResource(locations = {"classpath:bean.xml"})
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

在xml配置文件中引用.properties文件

<!-- 加载数据资源属性文件 -->
<context:property-placeholder location="classpath:application-dev.properties" ignore-unresolvable="true"/>

在bean配置文件中引用.yml文件

<context:annotation-config/>
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
    <property name="resources" value="classpath:application.yml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>

测试发现,在IDEA中配置.yml文件的方式并不能让变量正确识别,所以还是用.properties文件的方式吧。

Java代码使用bean

参考:使用@Configuration注解来代替Spring的bean配置

添加一个配置类:

@Configuration
public class AppBeanConfig {

   @Bean(initMethod = "init", destroyMethod = "destory")
   public AnotherClass createAnotherClass() {
        AnotherClass obj = new AnotherClass();
        return obj;
    }
}

使用时:

ApplicationContext context = new AnnotationConfigApplicationContext(AppBeanConfig.class);
AnotherClass obj = context.getBean(AnotherClass.class);

几个注解理解

  • @Repository :对应存储层Bean
  • @Service :对应业务层Bean
  • @Controller :对应展示层Bean

application/x-www-form-urlencoded request.getInputStream为空的问题

参考:SpringMVC 中 request.getInputStream() 为空解惑

解决方案:添加一个配置类WebComponentConfig:

package com.example.test;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.HttpPutFormContentFilter;

@Configuration
public class WebComponentConfig {
    @Bean
    public HttpPutFormContentFilter httpPutFormContentFilter() {
        return new HttpPutFormContentFilter();
    }
    @Bean
    public FilterRegistrationBean disableSpringBootHttpPutFormContentFilter(HttpPutFormContentFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new HiddenHttpMethodFilter();
    }
    @Bean
    public FilterRegistrationBean disableSpringBootHiddenHttpMethodFilter(HiddenHttpMethodFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }
}

SpringBoot操作数据库

可以参考这篇:SpringBoot 学习二:操作数据库,讲解了:

  • 配置数据源
  • SpringBoot 整合 Mybatis
  • SpringBoot 整合 JdbcTemplate
  • SpringBoot 整合 Redis

SpringBoot使用freemarker

<bean id="templateMerger"
    class="com.example.test.freemarker.TemplateMerger">
    <property name="resource">
        <value>/common/</value> <!--指定目录:webapp/common -->
    </property>
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/views/" />
    <property name="freemarkerSettings">
       <props>
           <!--用于解决前端报空指针问题-->
           <prop key="classic_compatible">true</prop>
            <prop key="defaultEncoding">UTF-8</prop>
           <prop key="number_format">0</prop>
       </props>
   </property>
</bean>

SpringBoot部署tomcat

springboot极简教程022-tomcat部署

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值