BATJ Internet company must ask knowledge points: Spring ten interview topics and answers

1. What is Inversion of Control (IOC)? What is dependency injection?

Inversion of control is applied in the field of software engineering. It is a programming technique to bind coupled objects by assembler objects at runtime . The coupling relationship between objects is usually unknown at compile time. In the traditional programming method, the flow of business logic is determined by the objects in the application that have already been set up . In the case of using inversion of control, the flow of business logic is determined by the object relationship diagram, which is instantiated by the assembler . This implementation can also abstract the definition of the relationship between objects . The binding process is achieved through "dependency injection".

Inversion of control is a design paradigm aimed at giving more control to the target components in the application, and it has played an effective role in our actual work .

Dependency injection is a mode of instantiating functional objects that other objects depend on when the required function is not known from which class at the compilation stage . This requires a mechanism to activate the corresponding components to provide specific functions, so dependency injection is the basis of inversion of control. Otherwise, if the component is not controlled by the framework, how does the framework know which component to create?

There are still three ways to achieve injection in Java:

  1. Constructor injection
  2. Setter method injection
  3. Interface injection

2. Please explain the IoC in the Spring framework?

Spring in the org.springframework.beanspacket and the org.springframework.contextpacket forms the basis of the Spring framework IoC container.

BeanFactoryThe interface provides an advanced configuration mechanism that makes the configuration of any type of object possible.

ApplicationContexInterface to BeanFactory(a sub-interface) is extended, in the BeanFactoryadditional functionality was added on the basis of the Spring AOP, such as easier integration, but also provides processing message resourcemechanism (for internationalization), event propagation as well as special application layer Configuration , such as for web applications WebApplicationContext.

org.springframework.beans.factory.BeanFactoryIt is a concrete implementation of the Spring IoC container , used to package and manage the various beans mentioned earlier. BeanFactoryThe interface is the core interface of the Spring IoC container .

IOC: The creation, initialization, and destruction of objects are managed by spring instead of being controlled by the developer to achieve inversion of control .

3. What is the difference between BeanFactory and ApplicationContext?

BeanFactoryCan be understood as a factory class containing a collection of beans . BeanFactoryContains the definition of a bean so that the corresponding bean can be instantiated when a client request is received.

BeanFactoryIt can also generate the relationship between the collaboration classes when the object is instantiated. This will free the bean itself and the configuration of the bean client. BeanFactoryIt also contains the control of the bean life cycle, calling the client's initialization method ( initialization methods) and destruction method ( destruction methods).

On the surface, application contextas bean factoryas defined having bean, bean disposed relationship, the distribution function of the bean upon request. However, applicationcontexton this basis, also provides other functions.

  1. Provides text messages that support internationalization
  2. Unified resource file reading method
  3. Events of beans that have been registered in the listener

The following are three of the more common ApplicationContextways:

  1. ClassPathXmlApplicationContext: From classpaththe XML configuration file is read context and generating a context definition. Application context from program environment variables
ApplicationContext context = new ClassPathXmlApplicationContext(“bean.xml”);
  1. FileSystemXmlApplicationContext : Read the context from the XML configuration file in the file system.
ApplicationContext context = new FileSystemXmlApplicationContext(“bean.xml”);
  1. XmlWebApplicationContext: Read the context from the XML file of the Web application.
  2. AnnotationConfigApplicationContext(Start the container based on the Java configuration)


This article is for reference only!
The content of the article is excerpted from "Spring Interview Topics and Answers"
. Students who need to add assistant VX: C18173184271Get it for free!

4. How many configuration methods does Spring have?

There are three ways to configure Spring into application development:

  1. XML-based configuration
  2. Annotation-based configuration
  3. Java-based configuration

5. Please explain the life cycle of Spring Bean?

Spring BeanThe life cycle is simple and easy to understand. When a bean instance is initialized, a series of initialization operations need to be performed to reach a usable state. Similarly, when a bean is no longer called, it needs to be destructed and removed from the bean container.

Spring bean factoryResponsible for managing the life cycle of beans created in the spring container. The life cycle of a bean consists of two sets of callback methods .

  1. Callback method called after initialization .
  2. The callback method called before the destruction .

The Spring framework provides the following four ways to manage bean life cycle events:

  • InitializingBeanAnd DisposableBeancallback interface
  • For other special acts AwareInterface
  • Bean profile Custom init()method and the destroy()method
  • @PostConstructAnd @PreDestroyannotation method

Use customInit()and customDestroy()code samples bean life cycle management method is as follows:

<beans> 
 <bean id="demoBean" class="com.somnus.task.DemoBean" initmethod="customInit" destroy-method="customDestroy"></bean> 
</beans>

6. What is Spring inner beans?

In the Spring framework, whenever a bean is used, only one attribute is called. A wise approach is to declare this bean as an inner bean. Inner beans can be implemented by means of setter injection "property" and construction method injection "construction parameters".

For example, in our application, a Customer class references a Person class, what we have to do is to create an instance of Person, and then use it inside the Customer.

public class Customer{
    
     
 	private Person person; 
 	//Setters and Getters
}
public class Person{
    
     
 	private String name; 
 	private String address; 
 	private int age; 
 	//Setters and Getters 
}

The declaration of the inner bean is as follows:

<bean id="CustomerBean" class="com.somnus.common.Customer"> 
	 <property name="person"> 
		 <!-- This is inner bean --> 
		 <bean class="com.howtodoinjava.common.Person"> 
			 <property name="name" value="lokesh" /> 
 			 <property name="address" value="India" /> 
 			 <property name="age" value="34" /> 
 		 </bean> 
 	 </property> 
</bean>

7. Please give an example of how to inject a Java Collection in Spring?

Spring provides the following four types of configuration elements:

  • : This tag is used to assemble repeatable list values.
  • : This tag is used to assemble set values ​​that are not repeated.
  • : 该标签可用来注入键和值可以为任何类型的键值对。
  • : This tag supports injection of key-value pairs whose keys and values ​​are both string types.

Let's look at a specific example:

<beans> 
 	<!-- Definition for javaCollection --> 
 	<bean id="javaCollection" class="com.howtodoinjava.JavaCollection"> 
 		<!-- java.util.List --> 
 		<property name="customList"> 
 		<list> 
 			<value>INDIA</value> 
 			<value>Pakistan</value> 
 			<value>USA</value> 
 			<value>UK</value> 
 		</list> 
 	</property> 
 
 	<!-- java.util.Set --> 
 	<property name="customSet"> 
 		<set> 
 			<value>INDIA</value> 
 			<value>Pakistan</value> 
 			<value>USA</value> 
 			<value>UK</value> 
 		</set> 
 </property> 
 
 <!-- java.util.Map --> 
 	<property name="customMap"> 
 		<map> 
 			<entry key="1" value="INDIA"/> 
 			<entry key="2" value="Pakistan"/> 
 			<entry key="3" value="USA"/> 
 			<entry key="4" value="UK"/> 
 		</map>
 		</property> 
 
 <!-- java.util.Properties --> 
 	<property name="customProperies"> 
 		<props> 
 			<prop key="admin">admin@nospam.com</prop> 
 			<prop key="support">support@nospam.com</prop> 
 		</props> 
 	</property> 
 
 	</bean> 
</beans>

8. Please explain the difference in automatic assembly mode?

There are 5 kinds of automatic assembly in the Spring framework, let us analyze them one by one.

  1. no: This is the default setting of the Spring framework. Under this setting, automatic assembly is turned off. Developers need to clearly set the dependencies in the bean definition with tags.
  2. byName: This option can set the dependency relationship based on the bean name. When automatically assembling a property to a bean, the container will automatically query a matching bean in the configuration file based on the bean name. If found, it will assemble this attribute, if not found, an error will be reported.
  3. byType: This option can set the dependency relationship according to the bean type. When automatically assembling a property to a bean, the container will automatically query a matching bean in the configuration file according to the bean type. If found, it will assemble this attribute, if not found, an error will be reported.
  4. constructor: The automatic assembly of the builder is similar to the byType mode, but only applies to beans with the same parameters as the constructor. If no bean with the same parameter type as the constructor is found in the container, an exception will be thrown.
  5. autodetect: This mode automatically detects using constructor auto-assembly or byType auto-assembly. First of all, it will first try to find a suitable constructor with parameters. If it finds it, it will be automatically assembled with the constructor. If the corresponding constructor or no-parameter constructor is found inside the bean, the container will automatically select byTpe's auto-assembly the way.

9. How to turn on automatic assembly based on annotations?

To use @Autowired, you need to register AutowiredAnnotationBeanPostProcessor, which can be achieved in the following two ways:
1. Import the download in the configuration file<context:annotation-config>

<beans> 
 <context:annotation-config /> 
</beans>

2. Directly import in the bean configuration file AutowiredAnnotationBeanPostProcessor

<beans> 
 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotati
onBeanPostProcessor"/> 
</beans>

10. Briefly describe the concepts of AOP and IOC

AOP : Aspect Oriented Program, programming section facing (aspect) ; the Filter (filter) . AOP is also an AOP a new methodology, is the traditional of OOP ( Object-Oriented Programming, object-oriented programming) supplementary main AOP is a programming object. Cut ( aspect) , while the aspect is modularized and cross-cutting concerns. Examples can be illustrated through transactions.

IOC : Invert Of Control, Inversion of Control has become. DI (Dependency Injection) The idea is to reverse the direction of resource acquisition traditional way to find the resources required component sends a request to locate the container resource response, the timely return of the container resources and the application of... after the IOC, the container is actively pushed to the resources it manages components, components do is to select an appropriate way to accept resources. this behavior is also known as a passive form of lookup .


This article is for reference only!
The content of the article is excerpted from "Spring Interview Topics and Answers"
. Students who need to add assistant VX: C18173184271Get it for free!

Guess you like

Origin blog.csdn.net/Java_Caiyo/article/details/112872928