簡體   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