-- 记录 -- 问题记录

thymeleaf不能渲染问题

发现叶子模板的文件根本没有被渲染就直接输出到流量器

如果渲染过后的话应该是不会显示红框内容的
发现原因:
没有将html放置templates中。

在springboot中其他静态文件路径是不会被thymeleaf渲染的,只有放置指定的路径

从spring boot配置文件中读取一个Map结构数据

配置文件
map.properties

com.hjh.map[status]=123
com.hjh.map[message]=xxx

map实例

MyMap.java

@Component
@ConfigurationProperties(prefix = "com.hjh")
@PropertySource("classpath:map.properties")
public class MyMap {

    public Map<String, String> map;

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
}

提供getset

这样就可以使用@Autowrite获得map了

springboot包的命名引发的问题。

SpringBoot项目的Bean装配默认规则是根据项目入口文件(Application)类所在的包位置从上往下扫描! 
例如。Application类所在包为com.hjh。则只会扫描com.hjh包和其子包。

可以使用@ComponentScan指定要扫描的包以及要扫描的类。

场景一

springboot中无法注入Bean

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxx: Unsatisfied dependency expressed through field 'xxx': No qualifying bean of type [xxx] found for dependency [xxx]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [xxx] found for dependency [com.example.repositories.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

原因:
注入bean的类跳出了项目入口文件(Application)的包范围,没有被扫描到。

场景二

在springboot多模块下。项目运行模块,无法获得某个模块的配置,或者Bean。

也可能是入口文件和其他项目的包名问题。
要确保要注入的对象有被扫描到、

posted on 2019-08-19 13:44  丶心  阅读(132)  评论(0编辑  收藏  举报