In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Spring based on xml file configuration Bean process is how, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Configure by full class name:
The full class name of the class:bean, which creates the Bean in the IOC container by reflection, so it requires that there must be a no-parameter constructor in the bean.
Before the springIOC container reads the Bean configuration to create an instance of Bean, the container needs to be instantiated. Spring provides two types of IOC container implementations:
The basic implementation of the Beanfactory:IOC container.
ApplicationContext: provides more advanced features and is a subinterface of BeanFactory.
The main implementation classes of ApplicationContext:
ClassPathXmlApplicationContext: loads the configuration file from the classpath. FileSystemXmlApplicationContext: loads the configuration file from the file system. ConfigureableApplicationContext extends to ApplicationContext with two new methods, refresh () and close (), to give ApplicationContext the ability to start, refresh, and close the context.
ApplicaiotnContex instantiates all singletons' Bean at the time of initialization and context.
WebApplicationContext is dedicated to WEB applications and allows initialization to be done from a path relative to the WEB root directory.
Three ways of dependency injection
(1) attribute injection: through the setter method: that is, there is a setter method in bean.
(2) Constructor injection: the constructor is set one by one according to the parameters initialized in the construction method. At the same time, the overloaded constructor can be distinguished according to the order of parameters index and the type of parameters type.
(3) Factory method injection (rarely used, not recommended)
/ / the first way to inject attribute values / / the second way to inject attribute values 99.00
Package com.gong.spring.beans;public class Student {private String name; private int age; private double score; public Student (String name,int age,double score) {this.name = name; this.age = age; this.score = score;} @ Override public String toString () {return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";}}
Public static void main (String [] args) {/ / 1. Create the IOC container object of spring ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml"); / / 2. Get Bean instance Student student = (Student) ctx.getBean ("student") from the container; System.out.println (student.toString ());}
Output:
When an attribute value has a special symbol, use the following ways:
]] >
Use it.
After reading the above, have you mastered how Spring configures the Bean process based on xml files? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.