preface
The term Spring is familiar to Java developers. You probably use Spring every day and enjoy the services provided by the Spring ecosystem. Many Internet companies are now evaluating Spring as one of the most important aspects of a job interview.
There are benefits at the end of the article
What are the three general Spring interview philosophies? Why is that? How does it work?
First share a Spring knowledge mind map to everyone
I. An overall introduction to Spring Framework functions
1. Core
2. Beans
3.Context
4.Expression Language
2. Use of the underlying annotations of the Spring IOC container
1. Define Bean information based on XML form
<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! Class =" com.demo.component.car "></ Bean ></ beans>Copy the code
public static void main( String[] args )
{
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getBean("person"));
}
Copy the code
2. Define Bean information based on reading configuration classes
@Configuration public class MainConfig { @Bean public Person person(){ return new Person(); }}Copy the code
public static void main( String[] args )
{
AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(MainConfig.class);
System.out.println(ctx.getBean("person"));
}
Copy the code
Spring Ioc container source code analysis
The core process for IOC container startup
i0:>org.springframework.context.support.AbstractApplicationContext#refresh
IOC container refresh process
i1>org.springframework.context.support.AbstractApplicationContext#prepareRefresh
i2> ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
i3>org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory
i4>org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory
i5>org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcess
i6>org.springframework.context.support.AbstractApplicationContext#registerBeanPostProcessors
i7>org.springframework.context.support.AbstractApplicationContext#initMessageSource
i8>org.springframework.context.support.AbstractApplicationContext#initApplicationEventMulticaster
i9>org.springframework.context.support.AbstractApplicationContext#onRefresh
i10>org.springframework.context.support.AbstractApplicationContext#registerListeners
i11>org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization
i12:org.springframework.context.support.AbstractApplicationContext#finishRefresh
How does Spring solve loop dependencies
1. Code example:
//getter/setter
public class InstanceA {
private InstanceB instanceB;
}
public class InstanceB {
private InstanceA instanceA;
}
<bean id="instanceA" class="com.tuling.circulardependencies.InstanceA">
<property name="instanceB" ref="intanceB"></property>
</bean>
<bean id="intanceB" class="com.tuling.circulardependencies.InstanceB">
<property name="instanceA" ref="instanceA"></property>
</bean>
Copy the code
2. Possible problems:
3. Solutions:
Five, Spring Aop source code analysis
AOP core Concepts
1. Crosscutting concerns (which methods to cut into)
Which methods are intercepted, and how are they handled? These concerns are called crosscutting concerns
2. Aspect (extracting the non-business code originally mixed in the business logic code and putting the same functions into a class to form a aspect)
A class is an abstraction of object features, and a section is an abstraction of crosscutting concerns
3. Joinpoint (need to cut into the point)
The intercepted point. Since Spring only supports method type join points, join points in Spring refer to the method being intercepted. In fact, join points can also be fields or constructors
4. Pointcut
Definition of interception of join points
5. Advice
Notification refers to the code to be executed after intercepting the join point, and notification is divided into five categories: pre-notification, post-notification, abnormal, final, and circular notification
6. Target people
The target object of the proxy
7. Weave
The process of applying a facet to a target object and resulting in the creation of a proxy object
8. Introduction
Imports can dynamically add methods or fields to a class at run time without modifying the code
A simple case
Public interface Calculate {/** * add * @param numB * @param numB * @return */ int add(int numA,int numB); /** * numA * @numam numB * @return */ int numA,int numB; /** * @numa * @numb * @return */ int numA (int numA,int numB); /** ** @numa * @numam numB * @return */ int numA,int numB; }Copy the code
= = = = = = = = = = implementation class
public class TulingCalculate implements Calculate { public int add(int numA, int numB) { return numA+numB; } public int reduce(int numA, int numB) { return numA-numB; } public int div(int numA, int numB) { return numA/numB; } public int multi(int numA, int numB) { return numA*numB; }}
Copy the code
= = = = = = = = = cut class
@Aspectpublic class TulingLogAspect { @Pointcut("execution(* com.tuling.TulingCalculate.*(..) )") public void pointCut(){}; @Before(value = "pointCut()") public void methodBefore(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println(" Array.asList (joinPoint.getargs ())) "); } @After(value = "pointCut()") public void methodAfter(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); System.out.println(" Array.asList (joinPoint.getargs ())); } @AfterReturning(value = "pointCut()") public void methodReturning(JoinPoint joinPoint ) { String methodName = joinPoint.getSignature().getName(); Println (" Array.asList (joinPoint.getargs ())) + array.asList (joinPoint.getargs ())); } @AfterThrowing(value = "pointCut()") public void methodAfterThrowing(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); Println (" Array.asList (joinPoint.getargs ())) + array.asList (joinPoint.getargs ())); }}Copy the code
= = = = = = = = = = configuration class
@Configuration@EnableAspectJAutoProxy public class MainConfig { @Bean public Calculate calculate() { return new TulingCalculate(); } @Bean public TulingLogAspect tulingLogAspect() { return new TulingLogAspect(); }}
Copy the code
Spring transaction source code analysis
1. What are things?
2. ACID
What is ACID?
(1) Atomicity
(2)Consistency
(3)Isolation
(4)Durability
I don’t have enough space to cover everything else here, but the Spring core comes in a 176-page PDF document
Get this summary of Spring core knowledge