SpringAop Dependency Injection
It is a lightweight and IOC (DI) and AOP container framework:
- IOC: Control Inversion The way to create beans is to leave them to spring for us to create. (understand)
- DI: Dependency injection spring is a container framework, a bean (object-loaded) container framework. After spring helps me create the object, inject the object we created into the corresponding class.
- AOP: For tangential programming, the AOP mechanism of the Spring framework allows developers to extract common functions in business processes and write function code separately. During the execution of business processes, the Spring framework will automatically enter the appropriate position of the process according to the requirements of the business process.
AOP usage scenarios:
- Transaction Management
- Log Management
- Performance Control
DI dependency injection
2.1. Inject according to the constructor:
- index index injection
<bean class="._01_.MyBean"> <constructor-arg index="0" value="666" /> <constructor-arg index="1" value="Stone Doll" /> </bean>
- name parameter name injection
<bean class="._01_.MyBean"> <constructor-arg name="id" value="1" /> <constructor-arg name="name" value="Shi Dawa" /> </bean>
- Type type injection
<bean class="._01_.MyBean"> <constructor-arg type="" value="1" /> <constructor-arg type="" value="Shi Erwa" /> </bean>
2.2 Property injection in class 2
<property name="id" value="20"></property> <!-- name --> <property name="name" value="Feng Zengxin"></property> <!-- StringArray--> <property name="arrys" value="1999,12,05"></property> <!--list set--> <property name="list"> <list> <value>air jordan 1</value> <value>obsidian</value> </list> </property> <property name="set"> <set> <value>adiads</value> <value>Yeezy750</value> </set> </property> <property name="helloBeanList"> <list> <ref bean="hb"></ref> <bean class="._01di.HelloBean"></bean> </list> </property> <property name="helloBeanSet"> <set> <ref bean="hb"></ref> <bean class="._01di.HelloBean"></bean> </set> </property> <!-- map--> <property name="hp"> <map> <entry key="star" value="Star"></entry> <entry key="sunshine " value="Sunlight"></entry> </map> </property> <!--Properties Not supported in Chinese --> <property name="pro1"> <value> driverClassName= username=yuJade </value> </property> <!-- Support Chinese--> <property name="pro2"> <props> <prop key="driverClassName"></prop> <prop key="username">Jaylen</prop> </props> </property>
Automatic injection of 2.3 XML version
default-autowire="byType" default-autowire="byName"
byType according to type, byName according to name
2.4XML version annotation introduction
<context:component-scan base-package="._03_autoxml"></context:component-scan> <!-- Scan the annotation: @Component, @Repository, @Service,@Controller -->
If an interface has two implementations:
- Solution 1: When an interface has multiple implementations, distinguish them by name.
- Solution 2: Through Resource annotation
the difference:
- @Autowired and @Qualifier are both annotations belonging to spring
- Resource's annotation using jdk It is recommended to use Autowired and Qualifier, which can be seamlessly connected with spring
- Autowired is matched by type by default. If the type matches not, matches by name.
- Resource matches according to the name by default. If the name does not match, the type will be matched.
If you are using Spring annotations, try to use Spring tags as much as possible
- Both AutoWired and Resource represent injecting objects
- AutoWired is annotation provided by SPring, @Resource is annotation provided by sun company
- AutoWired is matched by type by default. If the type matches not, matches by name.
- It is recommended that everyone use Autowried because it is annotated by Spring, which can seamlessly integrate with Spring.
- Resource matches according to the name by default. If the name does not match, the type will be matched.
The AOP function inside
3.1 AOP brief description
- Definition: Oriented programming (Attached programming)
- Spring uses dynamic proxy weaving.
- Spring supports us to add enhanced code before and after the method.
- Aop's function is one of the two cores of Spring, and it can also be a very important function of Spring.
3.2 Implementation of aop in Spring
1. If the target object implements several interfaces, spring uses JDK class proxy.
2. If the target object does not implement any interface, spring uses the CGLIB library to generate a subclass of the target object.
3. Pay attention to using this plan:
- Creating a proxy for an interface is better than creating a proxy for a class because it creates a more loosely coupled system. Class proxy allows legacy systems or third-party class libraries that cannot implement interfaces can also be notified. This solution should be an alternative solution.
- Methods marked final cannot be notified. spring is to generate subclasses for the target class. Any method that needs to be notified is repeated and the notification is weaved. The final method is not allowed to be rewrite.
3.3 Springaop configuration
<context:component-scan base-package="._04_aopxml"></context:component-scan> <aop:config> <aop:pointcut expression="execution(* ._04_aopxml.*Service.*(..))"></aop:pointcut> <!--Configuration Notification--> <aop:aspect ref="txManager"> <!--Pre-Notice--> <!--<aop:before method="begin" pointcut-ref="pointcut"></aop:before>--> <!--&lt;!&ndash;Post-Notice&ndash;&gt;--> <!--<aop:after-returning method="commit" pointcut-ref="pointcut"></aop:after-returning>--> <!--&lt;!&ndash;Close notification&ndash;&gt;--> <!--<aop:after method="close" pointcut-ref="pointcut"></aop:after>--> <!--&lt;!&ndash;abnormal&ndash;&gt;--> <!--<aop:after-throwing method="rollback" throwing="e" pointcut-ref="pointcut"></aop:after-throwing>--> <!--Surround notification--> <aop:around method="surround" pointcut-ref="pointcut"></aop:around>
3.4 Annotation method configuration
@Component @Aspect public class TxManager { @Pointcut("execution(* ._05_aopanno.*Service.*(..))") public void pointcut(){}
How to create
4.1 Bean creation method one
Create beans through parameterless construction method -- the most common way
Configuration
<bean class="._06_bean.Mybean"></bean>
4.2 Bean creation method two
FactoryBean
public class MybeanFactoryBean implements FactoryBean<Mybean> { public Mybean getObject() throws Exception { return new Mybean("Shi Chuyu", "The Light of the Immortal Peach"); } public Class<?> getObjectType() { return ; } //Is it a single case public boolean isSingleton() { return true; }
4.3 How to create a bean
Get beans by defining static methods in the class
public class MybeanFactory { public static Mybean getBean(){ return new Mybean(); } }
bean class="._06_bean.MybeanFactory" factory-method="getBean"></bean>
4.4 How to create a bean
public class MybeanFactory1 { //Ordinary public Mybean getBean1(){ return new Mybean(); } }
<bean class="._06_bean.MybeanFactory1" ></bean> <bean factory-bean="mybeanFactory1" factory-method="getBean1"></bean>
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.