简体   繁体   中英

regarding spring configuration file

i am using MyEclipse 8.6.1: this is my 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>

This is my java program: 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);
}

}

This is the exception i am getting

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

how to resolv this?

The dependency-check attribute has been deprecated since Spring 3.0 . The following workarounds can provide equivalent functionality

  • Use constructors (constructor injection instead of setter injection) exclusively to ensure the right properties are set.
  • Create setters with a dedicated init method implemented.
  • Create setters with @Required annotation when the property is required.
  • Use @Autowired -driven injection which also implies a required property by default.

Related: @Required example

I agree dependency got deprecated in spring 3, but I tested till spring 4, and it got removed in Spring 5.

I prefer using @Required annotation, because of demerits of dependency-check, as it considers all of the components as mandatory which might be required in all the cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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