Spring IOC related interview questions

The SpringIOC relevant knowledge put together again, to know what knowledge is more important. It's very simple. We will find out the relevant interview questions. If the interview questions are common, then this knowledge point is still relatively important!

1 what is spring?

Spring is an open source development framework for java enterprise applications. Spring is mainly used to develop Java applications, but some extensions are aimed at building web applications on the J2EE platform. The goal of the Spring framework is to simplify Java enterprise-level application development and promote good programming habits through a POJO-based programming model.

2 What are the benefits of using the Spring Framework?

  • Lightweight : Spring is lightweight, the basic version is about 2MB.
  • Inversion of control : Spring achieves loose coupling through inversion of control. Objects give their dependencies instead of creating or finding dependent objects.
  • Aspect-oriented programming (AOP): Spring supports aspect-oriented programming and separates application business logic and system services.
  • Container : Spring contains and manages the life cycle and configuration of objects in the application.
  • MVC framework : Spring's WEB framework is a well-designed framework and a good alternative to the Web framework.
  • Transaction management : Spring provides a continuous transaction management interface that can be extended from local transactions down to global transactions (JTA).
  • Exception handling : Spring provides a convenient API to convert specific technology-related exceptions (such as those thrown by JDBC, Hibernate or JDO) into consistent unchecked exceptions.

3What modules does Spring consist of ?

Simple can be divided into 6 major modules:

  • Core
  • AOP
  • SNAKE
  • DAO
  • Web
  • Spring EE

4BeanFactory implementation example

The Bean factory is an implementation of the factory pattern, which provides a control inversion function to separate the configuration and dependencies of the application from the real application code .

Before Spring 3.2, the most commonly used is XmlBeanFactory, but now it is abandoned and replaced by: XmlBeanDefinitionReader and DefaultListableBeanFactory

5 What is Spring 's dependency injection?

Dependency injection, an aspect of IOC, is a common concept, and it has multiple interpretations. The concept is that you do not need to create an object, but only need to describe how it is created. You do n't assemble your components and services directly in the code, but you need to describe which components need which services in the configuration file, and then a container ( IOC container) is responsible for assembling them.

6 What are the different types of IOC (Dependency Injection)?

  • Constructor Dependency Injection : Constructor Dependency Injection is implemented by the container triggering a class constructor, the class has a series of parameters, each parameter represents a dependency on other classes.
  • Setter method injection : Setter method injection is that after the container instantiates the bean by calling the parameterless constructor or parameterless static factory method, the setter method of the bean is called, which implements setter-based dependency injection.
  • Factory injection: This is left over and rarely used!

7. Which dependency injection method do you recommend, constructor injection, or Setter method injection?

You can use both dependency methods, constructor injection and Setter method injection. The best solution is to use constructor parameters to implement mandatory dependencies, and setter methods to implement optional dependencies .

8 What are Spring beans?

Spring beans are the java objects that form the backbone of Spring applications . They are initialized, assembled, and managed by the Spring IOC container. These beans are created from the metadata configured in the container. For example, it is defined in the form of <bean /> in the XML file .

There are four important ways to provide configuration metadata to the Spring container .

  • XML configuration file.
  • Annotation-based configuration.
  • Based on java configuration.
  • Groovy DSL configuration

9 explain the life cycle of beans in the Spring framework

  • The Spring container reads the definition of the bean from the XML file and instantiates the bean.
  • Spring populates all attributes according to the bean definition.
  • If the bean implements the BeanNameAware interface, Spring passes the bean ID to the setBeanName method.
  • If the bean implements the BeanFactoryAware interface, Spring passes the beanfactory to the setBeanFactory method.
  • If there are any BeanPostProcessors associated with the bean, Spring will call them in the postProcesserBeforeInitialization () method.
  • If the bean implements IntializingBean, call its afterPropertySet method. If the bean declares an initialization method, call this initialization method.
  • If there are BeanPostProcessors associated with beans, the postProcessAfterInitialization () methods of these beans will be called.
  • If the bean implements DisposableBean, it will call the destroy () method.

10 explain different ways of automatic assembly

  • no: The default method is not to perform automatic assembly, and perform assembly by explicitly setting the ref attribute.
  • byName: By auto-wiring the parameter name, the Spring container found that the bean's autowire attribute was set to byname in the configuration file, and then the container tried to match and assemble a bean with the same name as the bean's attribute.
  • byType :: Through parameter type autowiring, the Spring container found that the bean's autowire attribute was set to byType in the configuration file, and then the container tried to match, assemble, and bean attributes of the same type. If multiple beans meet the conditions, an error is thrown.
  • Constructor: This method is similar to byType, but it needs to be provided to the constructor parameters. If there is no determined parameter type of the constructor with parameters, an exception will be thrown.
  • autodetect: First try to use the constructor to automatically assemble, if it does not work, then use the byType method.

When only using annotations, annotations use byType by default !

11 What are the advantages of IOC ?

IOC or dependency injection minimizes the amount of application code. It makes the application easy to test, unit tests no longer need singletons and JNDI lookup mechanisms. Minimal cost and minimal intrusion enable loose coupling . The IOC container supports hungry-style initialization and lazy loading when loading services .

12 What are the important bean lifecycle methods? Can you overload them?

There are two important bean lifecycle methods. The first is setup , which is called when the container loads the bean. The second method is teardown,  which is called when the container unloads the class.

The bean tag has two important attributes (init-method and destroy-method ). With them you can customize the initialization and logout methods yourself. They also have corresponding annotations ( @PostConstruct and @PreDestroy ).

13 How to answer the interviewer: What is your understanding of Spring ?

14Spring singleton framework Beans are thread safe it?

The Spring framework does not perform any multi-threaded encapsulation of singleton beans. The thread safety and concurrency issues of singleton beans need to be solved by developers themselves. But in fact, most Spring beans do not have variable state (such as Serview class and DAO class), so to some extent Spring's singleton bean is thread safe. If your bean has multiple states (such as View Model objects), you need to ensure thread safety yourself .

The most obvious solution is to change the scope of the polymorphic bean from "singleton" to "prototype"

15 What is the difference between FileSystemResource and ClassPathResource ?

The relative path or absolute path of the spring-config.xml file in your project needs to be given in FileSystemResource. In ClassPathResource, spring will automatically search for configuration files in ClassPath, so ClassPathResource files should be placed under ClassPath.

If you save spring-config.xml in the src folder, you only need to give the name of the configuration file, because the src folder is the default.

In short, ClassPathResource reads configuration files in environment variables, and FileSystemResource reads configuration files in configuration files .

Guess you like

Origin www.cnblogs.com/lkylin/p/12720552.html