Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Spring configure Bean in the xml file

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Spring how to configure Bean in the xml file", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Spring how to configure Bean in the xml file" this article.

The Spring container is a large factory responsible for creating and managing all Bean.

The Spring container supports two formats of configuration files: xml files, properties files, and the most commonly used is xml files.

Configuration of Bean in xml file

The root element can contain multiple elements, one of which is the configuration of a Bean.

One is a Bean object. It turned out to be an object of this class from new, and an object was created in Spring.

Name specifies the name of the object, class specifies the class of the Bean, and scope specifies the scope of the object. The class attribute is required, other optional.

The name of an object can be specified through name or id, id can specify only one name, and name can specify one or more names separated by commas or semicolons. Example: name= "grade,score". When id or name is not specified, the value of the class property is taken by default.

Scope of Bean

Scope singleton (singleton) there is only one instance of the Bean (class) in the Spring container, and no matter how many times the Bean is referenced / obtained, it points to the same object. Singleton is the default scope of Bean and is suitable for Bean without session state (such as Dao build, Service components). Each time prototype (prototype) gets the Bean, it creates a new instance. In a HTTP request, request obtains the same instance of the Bean, which is only valid in this HTTP request. Different instances are created for different HTTP requests. Session obtains the same instance of the Bean in a HTTP session, and this instance is only valid in this HTTP session. GlobalSession gets the same instance of the Bean in a global HTTP session. Valid only when using the portlet context. Application creates an instance for each ServletContext object. Valid only in web-related ApplicationContext. Websocket creates an instance for each websocket object. Valid only in web-related ApplicationContext.

Example: singleton scope

The two objects obtained by ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext ("applicationContext.xml") / / are the same object. Student student1=applicationContext.getBean ("student", Student.class); Student student2=applicationContext.getBean ("student", Student.class); / / output the same System.out.println (student1); System.out.println (student2)

When the scope property is the default, the default is singleton scope.

Example: prototype scope

The two objects obtained by ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext ("applicationContext.xml") / / are different. Call getBean () once and a new object is created. Student student1=applicationContext.getBean ("student", Student.class); Student student2=applicationContext.getBean ("student", Student.class); / / output different System.out.println (student1); System.out.println (student2)

Description:

In the xml configuration file, a Bean is configured and a class is configured, not an instance (object) of the class.

When the Bean instance in the container is obtained / referenced when getBean () is called, the Spring container finds the corresponding configuration of the Bean according to id/name and checks the scope. If the instance of the Bean is re-created, it will be re-created, and the instance that already exists will be returned.

Child elements of--

Used to pass parameters to the constructor of the bean. One passes a parameter, one can have multiple child elements, and the corresponding constructor is called according to the number.

Name or index specifies the parameter, name is specified with the parameter name, and index is specified with the subscript of the parameter list (starting at 0). By default, values are passed in turn, starting with the first parameter.

Value or ref specify the actual parameter value. Value can only specify the basic type (Spring container will automatically convert to the corresponding data type). Ref can only be specified as other Bean (specified as name or id of other Bean). Choose as needed. It can also be specified with child elements of or. The type property can specify the data type, which is very fancy and is rarely used.

It can be written in this form:

Still pass an argument, index/name can be defaulted.

Element, if the argument is String, char, without quotation marks. For example, Zhang San will automatically recognize the type, 2-character String. "Zhang San" is also a String, but the argument is 4 characters.

Only in the form of a single label.

Parameter can be an array type

Choose according to the situation.

Parameters can be of List, Map, Set types

Private List list; public Student (List list) {this.list=list;}

One passes an List, one represents a list item, and only Java base types can be passed. If it is another Bean, use:

It has the same effect as it does, either one is fine.

Set: the usage is the same as List.

Map:

One represents a key-value pair, key and value represent the base type, and if it is other Bean, use key-ref and value-ref.

Description:

Because the element corresponding data type is List, corresponding to Set, corresponding to Map, so the formal parameter can only be the type of List/Set/Map, not the type of subclass such as ArrayList/HashSet/HashMap, but you can use generics.

The same as the effect, the same as the effect.

If the parameter is the basic data type or other Bean, it can be written in the form of a single tag, and if the parameter is an array or collection, which has multiple data types, it needs to be written in the form of double tags.

Child elements of--

Pass parameters to the setter method, a setter method to set a property, and one to pass parameters to a setter method (pass a parameter).

If Bean has more than one setter method, you can use multiple passing parameters.

Name specifies the parameter name. Value specifies a value (can only be a Java base type), or specifies a different Bean with ref. Of course, you can also use the corresponding child elements.

Similarly, arrays and collections can be used in the same way.

Note:

Unlike, you can only use name, not index.

Because a parameter is passed to the constructor, the formal parameter table of the constructor is ordered and can be specified by index or name. Multiple setter methods of Bean are unordered and can only be specified through name.

The above is all the contents of the article "how to configure Bean in the xml file by Spring". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report