Home > Back-end >  The Spring annotations and XML mode into the bean
The Spring annotations and XML mode into the bean

Time:10-03

Watching the Spring source, test at a place is a little bit confused,
The BeanFactory factory=new XmlBeanFactory (new ClassPathResource (" spring - test - bean. XML ")); Manually configure a bean in the XML, in the process of the Spring loaded, XML bean will be placed in the beanFactory inside, but on the other through annotations inject beans, but not at this time,
 public void xmlbeanFactoryResource () throws IOException {
The BeanFactory factory=new XmlBeanFactory (new ClassPathResource (
"Spring - test - bean. XML"));
//getBeanTest XML injection, have value
GetBeanTest beanTest=(GetBeanTest) factory) getBean (" GetBeanTest ");
BeanTest. BeanTest ();
//personArtServiceimpl @ service into the factory has no value, an error
System. The out. Println (factory) getBean (" personArtServiceimpl "));
}

Do you have any friend can tell me why, when injection is the automatic annotation (ApplicationContext I also is such),
My idea is to start the process of the Spring, a Spring bean in the configuration file is should be the first injection, to scan in accordance with the rules for annotations injection, however, so a few lines of code is, where he was injected annotation bean?

CodePudding user response:

When the servlet init createaplicationcontext load the XML file, he will be destroyed before before loading

CodePudding user response:

ComponentScanBeanDefinitionParser parse method in the class, when scanning class loading

CodePudding user response:

refer to the second floor youxiaoyue response:
ComponentScanBeanDefinitionParser parse method in the class, the scan class load

However, the application does not have

CodePudding user response:

You don't have a scan, annotation bean is certainly no

CodePudding user response:

XML file inside your package of personArtServiceimpl in the scan

CodePudding user response:

You configure the context: component - scan, and base - package to cover your classes in package
Source can see ContextNamespaceHandler. There were about component - scan processing class registered, will be called ComponentScanBeanDefinitionParser. Parse method for specific operation
  • Related