1, Jingdong interview questions
1. Implementation principle of @ Autowired
1. Injection method:
1. Constructor based dependency injection
public class UserServiceImpl implents UserService{ private UserDao userDao; @Autowire public UserServiceImpl(UserDao userDao){ this.userDao = userDao; } }
2. Dependency injection based on setter
public class UserServiceImpl implents UserService{ private UserDao userDao; @Autowire public serUserDao(UserDao userDao){ this.userDao = userDao; } }
3. Field based dependency injection
public class UserServiceImpl implents UserService{ @Autowire private UserDao userDao; }
2. How the @ Autowired annotation realizes automatic assembly:
@The reason why Autowired annotations can be automatically assembled mainly depends on the processor Autowired annotation beanpostprocessor provided by Spring, which adds the processing of @ Autowired, @ Inject and @ Value annotations during initialization;
The processor implements the interface instantiaawarebeanpostprocessor, so its @ Autowired members can be automatically assembled when the bean object is instantiated.
public AutowiredAnnotationBeanPostProcessor() { // Processing of @ Autowired, @ Inject, @ Value annotations is added this.autowiredAnnotationTypes.add(Autowired.class); this.autowiredAnnotationTypes.add(Value.class); try { this.autowiredAnnotationTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader())); logger.info("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring"); } catch (ClassNotFoundException ex) { // JSR-330 API not available - simply skip. } }
First, when was the processor added
When constructing a Spring container, Spring will register several built-in processor objects with the container, including AutowiredAnnotationBeanPostProcessor.
The source code can be viewed directly AnnotationConfigUtils.registerAnnotationConfigProcessors method.
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors( BeanDefinitionRegistry registry, @Nullable Object source) { // Omit code if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) { RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class); def.setSource(source); beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); } // Omit code return beanDefs; }
Second, when was the processor called
Spring will call the doCreateBean method when creating beans, and the populateBean method will be called in the doCreateBean method. The function of this method is to judge whether the bean object needs to be automatically assembled, and if so, traverse and call the processors injected by the spring container one by one.
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { if (hasInstAwareBpps || needsDepCheck) { if (pvs == null) { pvs = mbd.getPropertyValues(); } PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); if (hasInstAwareBpps) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; // Execute the post processor, fill in the attributes, and complete the automatic assembly pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } } }
There are several ways of automatic assembly:
Those who have not used xml configuration for injection may not be able to answer this question, but anyone who has read Spring practical books should know this knowledge point.
Spring currently supports three methods, namely
AUTOWIRE_BY_NAME automatically assembles bean properties by name
AUTOWIRE_BY_TYPE automatically assembles bean properties by type
AUTOWIRE_CONSTRUCTOR auto assemble by constructor
During xml configuration injection, you can switch by specifying the following types, for example:
(however, it is not recommended to specify the assembly method by xml configuration method.)
<bean id="userService" class="com.xxx.UserService" autowire="byName"></bean>
Expansion: our company does not recommend specifying the automatic assembly type in xml, because developers cannot know all beans in Spring applications, and specifying in this way will lead to uncertainty in the injected objects.
What kind of automatic assembly is specified with @ Autowired
The answer is AUTOWIRE_NO, that is, it is not specified.
But in fact, the implementation of the source code is first assembled through types. If there are multiple matched implementations, other strategies will be adopted.
If the @ autowritten annotated interface has multiple implementations, how does Spring handle it
When most people encounter this situation, they directly answer and report an error. In fact, they are not.
If the interface has multiple implementations, Spring has its own set of policies:
1. I'll see if there are any bean s with @ Primary annotation
2. Select the one with higher Priority according to the @ Priority annotation Priority.
3. Judge according to the name of the attribute and the beanName in Spring.
4. An error will be reported if it is not found again, that is, the NoUniqueBeanDefinitionException exception exception.
Specific source code implementation:
@What types of Autowired injection support:
Those with a short code age or who have not seen the source code cannot answer this question. Basically, they only know that @ Autowired injects an object.
In fact, @ Autowired not only registers a single object, but also supports injection of three types, namely array, collection and map.
Specific reference DefaultListableBeanFactory.resolveMultipleBeans method
@Autowired and @ Resource
2. What is the default scope of a Bean? Other scope of action?
3. What is the concept of index and what is its function?
4. What are the main index structures in MySQL? Comparison of hash index and B + tree index?
5. Talk about the principle of Java thread pool? What are the thread pools? What are the thread pool types of a thread pool factory and what are its thread pool parameters?
6. hashmap principle, which method is used to deal with hash conflict?
What are the differences and connections among BIO, NIO and AIO?
jvm memory model jmm knows all about
Talk about Java GC mechanism?
How does Java garbage collect? What object will enter the old age?
What are the garbage collection algorithms? Why does the new generation use replication algorithms?
Time complexity of HashMap? How are Hash conflicts resolved in HashMap? What are the changes in 1. HashMap in Java 8?
Red black trees need to be compared in size before they can be inserted. What is the basis for comparison? Other Hash conflict resolution methods?
What is the difference between hash and B + tree? What scenarios are applied to? Which is better?