spring配置遇到的问题

时间:2023-03-10 02:31:15
spring配置遇到的问题

1.文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"

这个原因是因为我自动扫描mapping.xml的文件路径设置错误,把它设置成spring-context的路径,然后报了这个莫名的错误。

2.自动注入时出现nullpointerexception,这个问题是因为没有很好理解spring的原理,参考链接如下:

https://*.com/questions/19896870/why-is-my-spring-autowired-field-null

https://*.com/questions/19869301/spring-autowired-object-nullpointerexception

也就是说,你不能简单的new一个对象来调用dao,你必须通过注入的方式,即在spring-context中创建bean id,然后才能用dao。

dao是接口,可以直接配置成自动扫描所有的dao,然后直接调用dao,无需实现dao。

3.spring Failed to convert property value of type 'java.lang.String' to required type 'int' for proper

用之前的方式就是不行,

spring配置遇到的问题

换了个配置方式就可以了,真是奇怪了!参考链接:http://blog.****.net/tengdazhang770960436/article/details/45563969

换成如下形式就可以了:

<!-- 引入缓存的配置文件properties -->

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="ignoreResourceNotFound" value="true" />

<property name="ignoreUnresolvablePlaceholders" value="true" />

<property name="locations">

<list>

<!-- 此位置是相对于:部署后的项目根路径 -->

<!-- <value>/WEB-INF/cache.properties</value> -->

<!-- 此位置是相对于:文件直接在src 目录下 -->

<!-- <value>classpath*:cache.properties</value> -->

<!-- 此位置是相对于:文件在目录下面 -->

<!-- <value>classpath*:cache/cache.properties</value> -->

<value>classpath*:/cache/cache.properties</value>

<!-- 此位置是从服务器环境变量中查找名为:XXX 的值(例如:file:D:/test/test.properties) -->

<!-- <value>${XXX}</value> -->

<!-- 此位置是相对于:文件系统 -->

<!-- <value>file:D:/test/test.properties</value> -->

</list>

</property>

</bean>

4.expected at least 1 bean which qualifies as autowire candidate for this depende

自动扫描dao的时候,老是报这个错误,查了好多资料,发现,原来是我的目录配置错了!靠!!!浪费了一个下午!

所以配置spring的xml文件时一定要仔细。