- 添加导入bean配置文件
- Java代码使用bean
- 几个注解理解
- application/x-www-form-urlencoded request.getInputStream为空的问题
- SpringBoot操作数据库
- SpringBoot使用freemarker
- SpringBoot部署tomcat
添加导入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
文档信息
- 本文作者:zhupite
- 本文链接:https://zhupite.com/springboot/springboot.html
- 版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)