Getting to know Spring
Spring is a lightweight application framework with strong reusability and super adhesion. It provides IOC, AOP, ORM integration, WEB integration, etc
The main core is: control inversion, face section
IOC: it means to reverse resources to other threads. It is an object-oriented design idea
AOP: aspect oriented programming, which provides a crosscutting way to find common behaviors and extract them for unified processing
AOP is a supplement to object-oriented OOP. This horizontal processing method is easier to deal with the business between different objects and different modules
List:
- access control
- transaction management
- Performance test
IOC core operation attribute injection (DI)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> // Injected is the Person class, in which two properties are not assigned values <bean id="person" class="Person"> <property name="paper" value="A4 paper"/> <property name="perName" value="secrecy"/> </bean> </beans>
- Test class
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = context.getBean("person", Person.class); System.out.println(person);
AOP aspect oriented testing uses the corresponding getter method and setter method
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cn.chy.test1"/> <bean id="userDao" class="cn.chy.test1.UserInfoDaoImpl"/> <bean id="userService" class="cn.chy.test1.UserInfoServiceImpl"> <property name="userInfoDao" ref="userDao"></property> </bean> <bean id="theLogger" class="cn.chy.test1.UserInfoServiceLogger"></bean> <aop:config> <aop:pointcut id="pointcut" expression="execution(public void save())"/> <aop:aspect ref="theLogger"> <aop:before method="before" pointcut-ref="pointcut"/> <aop:after-returning method="after" pointcut-ref="pointcut" returning="res"/> </aop:aspect> </aop:config> </beans>
Concept of AOP fuzzy query method
expression property in aop:pointcut tag = '"
-
expression
-
public * add() return types of all types
-
public void * () indicates all method names
-
public void add(...) any number of parameters
*com.service.**. Match all methods under the class
com.service…*. . Match all methods under subclass
Question:
- Describe the benefits of IOC to program development according to your own meaning
- No need to modify the source code!!, Specify the code block to run in advance in the IOC configuration file. When injecting attributes, it is also not injected into java classes, but directly injected into the IOC XML file. In addition, do not instantiate with the new keyword, and you can directly call the code in the container
- The core idea of AOP
- Through the way of crosscutting transactions, find the public behavior and extract it to a unified place for processing
- List the types and characteristics of AOP enhancement processing
- Advice: aspect executes logical code at a specific connection point (five notification methods)
- Pre notification: execute a notification before the method needs to be executed
- Post notification: a notification is executed after the method needs to be executed
- Surround notification: a notification is executed before and after the method needs to be executed
- Exception notification: catch an exception and execute a notification after the method needs to be executed (condition: execute only when an exception occurs)
- Final notification: after the method needs to be executed, a notification is finally executed after the post and surround
- Advice: aspect executes logical code at a specific connection point (five notification methods)
Interview questions
Spring IOC mechanism and AOP mechanism
In Spring, IOC and AOP are the design soul of Spring. IOC provides attribute injection (DI) for control inversion, and AOP is aspect oriented
Main mechanisms:
IOC provides factory mode
AOP provides proxy mode
Spring's IoC container is the core of spring, and Spring AOP is an important part of the spring framework
Q: is inversion of control the same concept as dependency injection?
Dependency injection and inversion of control are different descriptions of the same thing
Dependency injection: from the perspective of the program: create the application container and inject the external resources it needs
Inversion of control: from the perspective of the container: the container reverses the injection of external resources required by the application
Q: the difference between bean s and Java objects:
The bean life cycle is created by the Spring container in the singleton pool
Beans are created by Spring, but objects are created by new