发表于: 2017-05-10 21:39:27
2 1526
今天完成的事情:
之前项目一直报错,对Spring,mybatis,spring MVC的配置文件一直稀里糊涂的,
今天就把SSM配置文件重新梳理了一遍,遇到的许多问题,还在解决之中。
今天重点主要是spring的bean自动装配和通过Java注解的方式配置spring。
通过XML配置spring主要就是在spring的配置文件中,比如:applicationcontext.xml中进行一些声明:组件扫描,mapper扫描,声明配置等等。
这些事情同样可以在Java类中通过注解实现,比如:
@Configuration
@Import({StudentConfig.class,PostionConfig.class}) //拆分
@ImportResource("Classpath: application-context.xml") //导入xml配置文件
@ComponentScan("jnshu") //注解启用组件扫描,与XML配置相同
@MapperScan("mapper") //注解自动扫描mapper,与XML配置相同
public class jnshuConfig {......}
当单个spring Java类配置文件内容特别复杂的时候,可以拆分成多个类文件,
当有多个spring Java类配置文件的时候,可以通过@ImportResource进行汇总导入到同一个文件之中。
当有多个spring XML配置文件的时候,在需要的时候要全部进行导入,比如在web.xml中声明:
<!--导入多个spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml,classpath:spring-web.xml</param-value>
</init-param>
否则会报错,如:Cause: No qualifying bean of type […] is defined,找不到定义的bean。
明天打算做的事情:撸起袖子继续加油干任务四,争取能够显示页面。
遇到的问题:
1.
Error creating bean with name 'sqlSession' defined in class path resource [application-context.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [application-context.xml]: Invocation of init method failed; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 134; 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"。
目前可能是重复导入配置文件(可能是Java注解和XML两种方式导入配置文件冲突了)或者是配置文件标签没有写完整(我已经检查过了,应该不是这个)
2.
Cause: No qualifying bean of type […] is defined
这个就是找不到定义的bean,要没事没有定义,要么是没有导进spring的bean配置文件
收获:解决bug要从错误提示弄起,多思考出错的原因。
评论