发表于: 2019-08-28 23:42:15
1 596
今天完成的事情:
1.基本学会了springboot的bean属性注入的方法:
1.从全局配置里面获取相关属性,使用@Value("${}")注入到响应的bean的变量里面:
public class MailUtil {
@Value("${mail.url}")
private String url;
2.上面的配置稍显麻烦,可以使用@ConfigurationProperties注解,在类上面加上这个注解来统一注入属性(需要配置文件里面的属性名和类变量名对应)
@Component
@ConfigurationProperties(prefix = "mail")
public class MailUtil {
全局配置application.properties
mail.url=http://api.sendcloud.net/apiv2/mail/send
mail.apiUser=yamete000_test_MMYeCI
mail.apiKey=1vR4RnAXDR617Rfs
mail.from=com.fan.service@sendcloud.im
3.@Configuration注解,这个注解相当于配置了一个spring.xml配置文件。然后在需要配置bean的方法上面写一个@Bean来注入一个bean,这里以配置tiles框架的TilesViewResolver为例
@Configuration
public class TilesCon {
@Bean
public TilesConfigurer tilesConfigurer() {
final TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] { "WEB-INF/tiles.xml" });
configurer.setCheckRefresh(true);
return configurer;
}
@Bean
public TilesViewResolver tilesViewResolver() {
final TilesViewResolver resolver = new TilesViewResolver();
resolver.setViewClass(TilesView.class);
return resolver;
}
}
这样之前的任务要求就能达到了(ssm整合mybatis完成前后端全部的内容)
前端webapp目录,
运行结果:
已然是这个报错:GsonBuilder这个bean创建失败,对springboot了解的太少了,因为大量使用了默认配置,根本没得一点头绪。我感觉可能是某个依赖包里面引入了Gson相关的包同时产生了冲突:
但是除了自己引入的gson包,并没有任何依赖可以看到。
报错信息:
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
java.lang.invoke.MethodHandleNatives.resolve(Native Method)
The following method did not exist:
com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
The method's class, com.google.gson.GsonBuilder, is available from the following locations:
jar:file:/D:/Apache/Local%20Hose/api/sms/2.6.3/sms-2.6.3.jar!/com/google/gson/GsonBuilder.class
jar:file:/D:/Apache/Local%20Hose/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar!/com/google/gson/GsonBuilder.class
It was loaded from the following location:
file:/D:/Apache/Local%20Hose/api/sms/2.6.3/sms-2.6.3.jar
感觉springboot的自动化很高大上,但是也有很多坑根本不了解。
(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
明天计划的事情:(一定要写非常细致的内容)
遇到的问题:第一次做demo的时候,并没有配置web.xml文件,因为springboot在springboot-web-starter集成了tomcat,但是如果需要显示jsp页面,就需要制定一个webapp目录来配置jsp的viewResolver,尽管这个web.xml文件是空的。
(遇到什么困难,怎么解决的)
收获:springboot有了稍微深刻的理解(通过今天的学习,学到了什么知识)
评论