繁体   English   中英

关于spring配置文件

[英]regarding spring configuration file

我正在使用 MyEclipse 8.6.1:这是我的 applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<bean id="addr" class="info.inetsolv.Address" abstract="false"
    lazy-init="default" autowire="default" dependency-check="default"
    p:street="bk guda" p:city="hyd" p:state="ap">
</bean></beans>

这是我的 java 程序:package info.inetsolv;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class MyAppSprContnr {

public static void main(String[] args) {
  Resource resource = new ClassPathResource("applicationContext.xml");  
  BeanFactory container= new XmlBeanFactory(resource);
  System.out.println("container"+container);
}

}

这是我得到的例外

log4j:WARN No appenders could be found for logger      (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main"       org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML   document from class path resource [applicationContext.xml] is invalid; nested exception is   org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'dependency-check' is not   allowed to appear in element 'bean'.
at org.springframework.b

如何解决这个问题?

从 Spring 3.0开始, dependency-check属性已被弃用。 以下解决方法可以提供等效的功能

  • 专门使用构造函数(构造函数注入而不是 setter 注入)来确保设置正确的属性。
  • 使用实现的专用 init 方法创建 setter。
  • 当需要属性时,使用@Required注释创建 setter。
  • 使用@Autowired驱动的注入,默认情况下也暗示了必需的属性。

相关: @Required 示例

我同意依赖在 spring 3 中被弃用,但我测试到 spring 4,它在 Spring 5 中被删除。

我更喜欢使用 @Required 注释,因为依赖性检查的缺点,因为它认为所有组件都是强制性的,在所有情况下都可能需要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM