Home  >  Article  >  Java  >  Spring bean basic interview questions and answers

Spring bean basic interview questions and answers

(*-*)浩
(*-*)浩Original
2019-11-27 14:09:292407browse

Spring bean basic interview questions and answers

Is the singleton bean in the Spring framework thread-safe?

The singleton bean in the Spring framework is not thread-safe. (Recommended study: java interview questions)

What are Spring’s internal beans?

When a bean is only used as an attribute of another bean, it can be declared as an inner bean. In order to define the inner bean, in Spring's XML-based configuration metadata, you can The element is used within the or element. Internal beans are usually anonymous, and their Scope is usually prototype.

What is bean autowiring?

There is no need to describe the dependencies between javaBeans in the Spring configuration file (such as configuring , ). The IOC container will automatically establish the association between javabeans.

Different ways of automatic assembly.

There are five automatic assembly methods that can be used to guide the Spring container to use automatic assembly for dependency injection.

no: The default method is not to perform automatic assembly, but to perform assembly by explicitly setting the ref attribute.

byName: Through automatic assembly of parameter names, the Spring container finds that the bean's autowire attribute is set to byname in the configuration file, and then the container tries to match and assemble a bean with the same name as the bean's attribute.

byType:: Through automatic assembly of parameter types, the Spring container finds that the autowire attribute of the bean is set to byType in the configuration file, and then the container attempts to match and assemble beans of the same type as the bean's attributes. If more than one bean meets the criteria, an error is thrown.

constructor: This method is similar to byType, but it must be provided to the constructor parameter. If there is no determined constructor parameter type with parameters, an exception will be thrown.

autodetect: First try to use constructor for automatic assembly. If it doesn't work, use byType method.

How to inject a java collection in Spring?

Spring provides the following configuration elements for collections:

type is used to inject a list of values, allowing the same value.

type is used to inject a set of values, and the same value is not allowed.

type is used to inject a set of key-value pairs. Both keys and values ​​can be of any type.

The type is used to inject a set of key-value pairs. Both keys and values ​​can only be of type String.

The above is the detailed content of Spring bean basic interview questions and answers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn