In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Xiaobian to share with you how to assemble Spring Xml Bean, I believe most people do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
1, Overview
There are three ways to configure beans in Spring:
Configuring discovery mechanisms and autoassembly principles for implicit beans in Java interfaces and implementation classes in xml files
All three are used frequently, and often mixed. This article starts with XML Assembly Bean.
2. Analyze bean tags
id: Give the bean a globally unique name. This attribute is optional. If it is not declared, Spring will automatically generate a number using the fully qualified name #{number}, counting from 0.
For example, two Person objects are declared, as follows:
To get an object:
ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml");Person person = (Person) ac.getBean(Person.class.getName() + "#1");System.out.println(person.toString());
Class: The type of the injected object, corresponding to the fully qualified name of the class.
3. Property: Defines the attributes of the class, where name represents the attribute name and value represents the attribute value.
3, assembly set
The above implementations of injection are for some basic data types and String types. If the data type is a collection, do the following:
1. First define a new class--> Coder:
package com.xl.spring.xlIoc.beans; import lombok.Data; import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set; @Datapublic class Coder { private String name; private List languages; private Set friends; private Map houses; private Properties properties;}
2. In Spring's xml file, inject Coder:
java JavaScript SQL Jin Zhongguo Ha Dong Xun Liu zaishi Chi shizhen Quan Shaomin 168 52.3 30
3. Call:
public static void testCoder() { //1. Get the context of spring through the configuration file ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml"); Coder coder = (Coder) ac.getBean("coder"); coder.getFriends().forEach(s -> System.out.println("friend ---> " + s));}4, Complex Bean Assembly
If the attribute is a complex collection object, such as List or Map, and the generic of List is an object, or the key and value of Map are both an object.
What should we do at this time?
Solution: First inject the Bean corresponding to the generic, and then introduce the past when injecting attributes.
1. First define three classes: UserRole requires the use of User objects and Role objects
package com.xl.spring.xlIoc.beans; import lombok.Data; @Datapublic class User { private Integer id; private String name;}package com.xl.spring.xlIoc.beans; import lombok.Data; @Datapublic class Role { private Integer id; private String name;} package com.xl.spring.xlIoc.beans; import lombok.Data; import java.util.List;import java.util.Map; @Datapublic class UserRole { private Integer id; private List users; private Map map;}
2. First inject the corresponding User object and Role object, and then inject the UserRole object:
3. Create objects and use:
public static void testComplexBean() { ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml"); UserRole userRole = (UserRole) ac.getBean("userRole"); System.out.println(userRole);} That's all for Spring Xml How to Assemble Beans. Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.