A, Spring Starter operation
- IOC
- Create Object
<bean id="user" class="com.itheima.domain.User"></bean> = User user = new com.itheima.domain.User();
- Create objects in three ways
1 No argument constructor creates exclusive 2 static factory object is created. Factory class: com.itheima.factory.BeanFactory static methods: public static the User the createUser () {} Create Object <the bean class = "com.itheima.factory.BeanFactory "= factory-method" the createUser "/> . 3 normal factory object is created. factory class: com.itheima.factory.BeanFactory non-static method: public the User the createUser () {} create factory object <the bean ID =" beanFactory " class =" com.itheima.factory.BeanFactory "/> Create Object <bean factory-bean =" beanFactory "factory-method =" createUser "/>
- Factory object
Able to create objects of the class, called the factory class.
Static factory
method to create an object of a static method
ordinary factory
method to create objects that belong to non-static method
- Dependency Injection
1. Properties injection ---> call set method 2. The injection configured ---> argument constructor call with 3 annotated injection @ Autowired, @ Resource, @ Value
- Three ways of code dependency injection
<! - 1 Property injection: The method of call assignment set (requires corresponding attribute must set method) -> <the bean ID = "AccountService" class = "com.bky.service.impl.AccountServiceImpl"> <! - - property: representation using property injection calling setName name to assign name = "" : represents a set method to invoke omitted set, the first letter lowercase value = "little red" : assignment, value only for the basic data types and character string -> <Property name = "name" value = "Bob" /> <-! Age -> <Property name = "Age" value = "22 is" /> <!-- birthday ref="date": If you want the assignment of data is a Bean object, use the ref reference -> <Property name = "Birthday" ref = "DATE" /> </ bean> <-! A Date DATE = new new java.util.Date ( ) -> <the bean ID = "DATE" class = "java.util.Date"> </ the bean> <-! constructor injection -> <the bean ID = "accountService2" class = "com.bky.service .impl.AccountServiceImpl "> <-! constructor injection name =" parameters into the constructor specifies the name of " -> <constructor Arg-name =" name "value="小王" /> <constructor-arg name="age" value="25" /> <constructor Arg-name = "Birthday" REF = "DATE" /> </bean> <-! used constructor injection specified index -> <the bean ID = "accountService3" class = "com.bky.service.impl.AccountServiceImpl" > <constructor Arg-index = "2" = REF "DATE" /> <constructor Arg-index = "0" value = "calf" /> <constructor Arg-index = ". 1" value = "26 is" /> </ the bean> <-! used constructor injection specified data type -> <the bean ID = "accountService4" class = "com.bky.service.impl.AccountServiceImpl"> <constructor-arg type="java.lang.String" value="小明"/> <constructor-Arg of the type = "java.lang.String" value = "Shenzhen"/> <constructor-arg type="java.lang.Integer" value="27" /> <constructor Arg-type = "java.util.Date" REF = "DATE" /> </ the bean>
using injection annotations
@Autowired: preferred type of injection, if the same type, select the name of the injection (by of the type)
@Resource: preferred name inject, if the same name, select the type of injection (by name)
@Value: a profile
Two, Spring related notes
- Create an object: the same four notes role for creating an object
- @Component () if you do not write the name, id = default value class name, the first letter lowercase
@Component: generally used for ordinary objects
@Controller: General notes for the presentation layer.
@Service: general business for the annotation layer.
@Repository: generally used for annotation persistence layer.
- @Bean: Create Object
Acting on the method, the return value to the method of management container SpringIOC
- test
@RunWith (the SpringJUnit4ClassRunner. Class ) // create a test container, JUnit extension @ContextConfiguration () // specified configuration file or configuration class
Three, AOP
- AOP concepts -> aspect-oriented ->
Simple it is to repeat our program code extracted, when you need to perform, using dynamic proxy technology, without modifying the source code, based on
our existing methods enhanced.
- The underlying principle of AOP: Dynamic agent technology
/ ** * * Based on Dynamic Proxy reform * Dynamic Agent: * Features: bytecode used with the creation, as used with the load * Classification: based on dynamic agent interface, based on dynamic proxy subclasses * effect: does not modify the source code based on the method of enhancing * based on a dynamic proxy interface: * Contributors are: JDK official * requirements: minimum proxied class implements an interface. * Classes involved: Proxy * Create a proxy object method: newProxyInstance parameters * methods: * ClassLoader: class loader. Proxy object for loading bytecode. And a proxy object in the same class loader. Fixed writing. * Class []: bytecode array. To provide a method for proxy object. And a proxy object with the same method. And a proxy object implement the same interface, it will have the same method. Fixed wording * InvocationHanlder: to enhance the way. Here is an interface, we need to provide its implementation class. Usually it is written by an anonymous inner class. * Enhanced code used with the writing. * / ProxyProducer proxyProducer = (ProxyProducer) the Proxy.newProxyInstance ( Producer. Class .getClassLoader (), Producer. Class .getInterfaces (), new new of InvocationHandler () { / ** * performing any incorporated methods are proxied through the object's method, the method has the effect of intercepting * Parameter Meaning * Object proxy: the proxy object references. Generally do * Method method: a method currently being executed * Object [] args: parameters of the current required method * return value meaning * proxy objects and methods are the same return value * / @Override public Object Invoke (Object Proxy, method method,, Object [] args) throws the Throwable { // method parameter Money = float (float) args [0 ]; // Get the name of the method String methodName = method.getName (); // define an object, stores the result returned Object Result = null ; // sales commission% 25 IF (methodName.equals ( "saleProduct" )) { Result = Method.invoke (Producer, Money * 0.75f ); } the else IF (methodName.equals ( "saleProduct" )) { // sales commission% 10 Result = Method.invoke (Producer, Money 0.9F * ); } return result; } } );
- AOP advice / Enhanced
Front enhanced: the target method before execution, perform enhancement operations (call other methods)
Rear enhancements: after the target method execution is completed (after return), to perform the enhanced
abnormal enhancement: target method execution after an exception perform an enhanced
and ultimately enhance: target method execution , regardless of whether an abnormality occurs, will be executed
surround enhancement: analog throughout the execution of the dynamic and can be done to enhance front,
rear reinforcing, abnormal enhancement of final enhancement can also be modified to return data
- Declarative transaction: Based on AOP
1 configuration data source 2 . Configuration transaction manager 3 . Configuration Administration Regulations [transaction propagation characteristics] 4. establishment notification, the cut point designation execution of transaction control