Spring learning record

IoC -- tell spring about the class and let spring create objects of related classes when you need them

  • Chinese Name: control inversion
  • English Name: (Inversion of Control)
  • What IoC does: the programmer originally took the initiative to instantiate the object through new and handed it over to Spring
  • In control inversion, control refers to the object of control class
  • In inversion of control, inversion refers to handing over to Spring
  • The biggest function of IoC: decoupling
  • Programmers do not need to manage objects Decoupling between object management and programmers

Environment construction

  • Import jar package, four core packages and one logging package (commons logging)
  • Create a new ApplicationContext under src XML and tell yourself that the configuration information is finally saved in the ApplicationContext container.
  • spring configuration file is based on schema (. xsd file) DTD upgrade (syntax checker of xml file)
<?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">
    <bean id="user" class="com.xs.pojo.User"></bean>
</beans>

getBeanDefinitionNames()
It is created when the xml file is loaded

		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = ac.getBean("user", User.class);
        System.out.println(user);

Three ways to create objects

Create by construction method:

    <bean id="user" class="com.xs.pojo.User">
        <constructor-arg name="age" value="12"></constructor-arg>
        <constructor-arg name="id" value="4"></constructor-arg>
        <constructor-arg name="name" value="makka"></constructor-arg>
     </bean>
  • If the condition matches multiple construction methods, it defaults to the last construction method
  • Name -- attribute name
  • Value, ref -- attribute value, ref refers to other classes that need to be referenced
  • Index - parameter index
  • Type - parameter type

Instance factory and static factory

    <bean id="factory" class="com.xs.pojo.UserFactory"></bean>
    <bean id="user1" factory-bean="factory" factory-method="newInstance"></bean>
    
    <bean id="user2" class="com.xs.pojo.UserFactory" factory-method="newInstance"></bean>

Attribute injection to Bean

    <bean id="user3" class="com.xs.pojo.User">
        <property name="age" value="1"></property>
        <property name="id" value="2"></property>
        <property name="name" value="333"></property>
    </bean>

Common attributes are passed through < property > + value

        <property name="set">
            <set >
                <value>1</value>
                <value>2</value>
                <value>3</value>
                <value>0</value>
            </set>
        </property>

        <property name="list">
            <list>
                <value>makka</value>
                <value>Iggle</value>
                <value>Pakka</value>
            </list>
        </property>

        <property name="map">
            <map>
                <entry key="lo" value="Q"></entry>
                <entry key="v" value="S"></entry>
                <entry key="e" value="H"></entry>
            </map>
        </property>

map,set,list
Array < array > + value

  • Use < property > + < props > when the property is Properties. Since property stores information in key value pairs, it is in the form of < prop key = "key1" > value1 < / prop >.

DI (dependency injection)

  • Similar to Ioc
  • When a class needs another class, the process of assigning an object of another class to it is called dependency injection.
    <bean id="user4" class="com.xs.pojo.User">
        <property name="c" ref="computer"></property>
    </bean>
    <bean id="computer" class="com.xs.pojo.Computer">
        <property name="num" >
            <value>1</value>
        </property>
    </bean>

Note ref and id

Spring simplifies mybatis

1. Replace dataSource function

    <environments default="default">
        <!-- Declare available environments -->
        <environment id="default">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="000000"/>
            </dataSource>
        </environment>
    </environments>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="000000"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>

class in the bean is the object type to be created, and property injects relevant information for it (essentially calls the set method)

2. SqlSessionFactory required

    <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

3. < mappers >

    <mappers>
        <package name="com/xs/mapper"/>
    </mappers>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xs.mapper"></property>
        <property name="sqlSessionFactory" ref="factory"></property>
    </bean>
  • The packages to be scanned should be connected with the factory so that there are scanned packages in the factory.
  • After scanning, create an object for the corresponding interface (tell spring the object of the corresponding interface)

4. Configure unique serviceImpl

public class UserServiceImpl implements UserService {
    private UserMapper mapper;

    public UserMapper getMapper() {
        return mapper;
    }

    public void setMapper(UserMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public List<User> show() {
        return mapper.selAll();
    }
}

Since the UserMapper object is already in spring after the scanning package is completed, put the UserServiceImpl class under the < bean > tag and inject the created uerMapper object into it to be managed by spring.

    <bean id="userServiceImpl" class="com.xs.service.impl.UserServiceImpl">
        <property name="mapper" ref="userMapper"></property>
    </bean>

5. Load the configuration file when tomcat starts
Spring encapsulates a listener (under spring WEB) and configures it on the web XML

    <listener>
        <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
    </listener>

When we are on the web When configuring this class in XML, tomcat will help us instantiate this class, but tomcat also needs a context parameter.

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

tomcat stores the retrieved information in the webApplicationContext, so it takes out the service from it.

    @Override
    public void init(ServletConfig config) throws ServletException {
        ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        service = ac.getBean("userServiceImpl", UserServiceImpl.class);
    }

AOP

Aspect Oriented Programming

Add pre notification and post notification to one or more methods to form a cross section.

High scalability and improved logic

  • Original function: pointcut
  • Pre notification: the function performed before the tangent point before advice
  • Post notification: the function executed after the pointcut, after advice
  • If an exception occurs during pointcut execution, an exception notification will be triggered throws advice
  • All functions are collectively called facets
  • Two implementation methods -- AspectJ and schema based

Schema-based

1. Import jar package

2. New notification class

Advance notice:

  • arg0: pointcut Method object
  • arg1: tangent method parameter
  • arg2: in which object is the tangent point

Post notification:

  • arg0: return value of pointcut method
  • arg1: pointcut Method object
  • arg2: tangent method parameter
  • arg3: the object of the pointcut method
public class MyBeforeAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("Front front");
    }
}


public class MyAfterAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("Post Post");
    }
}

3. Configuring spring profiles

  • AOP namespace xmlns: AOP=“ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

  • Configure notification class < bean >
    <bean class="com.xs.advice.MyBeforeAdvice" id="before"></bean> <bean class="com.xs.advice.MyAfterAdvice" id="after"></bean>

  • Configure tangent (tangent point and tangent plane)

    <aop:config>
        <aop:pointcut id="mypoint" expression="execution(* demo.Demo.demo01())"/>
        <aop:advisor advice-ref="before" pointcut-ref="mypoint"></aop:advisor>
        <aop:advisor advice-ref="after" pointcut-ref="mypoint"></aop:advisor>
    </aop:config>
  • Match all parameter methods (..)

Configure exception notification

Intercept the service method (to throw an exception)

AspectJ mode steps

  • Any method under any class
  • xml to configure this class < bean > and pointcut bean
  • ref in < AOP: aspect > points to the bean
  • (after spring knows the class where the exception notification is located, it also needs to know its method name.) while configuring the pointcut, < AOP: after throwing > configures its method name. Pointcut ref points to the pointcut and is configured on the pointcut
  • If you want to know the exception information, you need to give the formal parameter when writing the method, and use throwing to tell the formal parameter name
    <bean id="mythrow" class="com.xs.mythrow.MyThrowAdvice"></bean>
    <bean id="demo" class="demo.Demo"></bean>
    <aop:config>
        <aop:aspect ref="mythrow">
            <aop:pointcut id="mypoint" expression="execution(* demo.Demo.demo01())"/>
            <aop:after-throwing method="myexception" pointcut-ref="mypoint" throwing="e"></aop:after-throwing>
        </aop:aspect>
    </aop:config>

Schema based mode

  • Any class name, afterThrowing method name, and the parameters can only be 1 or 4 (for specific official documents, search ThrowsAdvice)
  • Note that the exception name thrown by afterThrowing is the same as the exception type thrown by pointcut
  • Configuration xml
    <bean id="demo" class="demo.Demo"></bean>
    <bean id="mythrow" class="com.xs.mythrow.MyThrow"></bean>
    <aop:config>
        <aop:pointcut id="mypoint" expression="execution(* demo.Demo.demo01())"/>
        <aop:advisor advice-ref="mythrow" pointcut-ref="mypoint"></aop:advisor>
    </aop:config>
public class MyThrow implements ThrowsAdvice {
    public void afterThrowing(Exception ex) throws Throwable{
        System.out.println("Abnormal abnormal");
    }
}

Surround configuration

Be careful not to lead the wrong bag
be careful. proceed(); method

public class MyAround implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("Front");
        Object proceed = methodInvocation.proceed();
        System.out.println("Postposition");
        return proceed;

    }
}

Simple xml configuration

    <bean id="myaround" class="com.xs.around.MyAround"></bean>
    <bean id="demo" class="demo.Demo"></bean>

    <aop:config>
        <aop:pointcut id="mypoint" expression="execution(* demo.Demo.demo01())"/>
        <aop:advisor advice-ref="myaround" pointcut-ref="mypoint"></aop:advisor>
    </aop:config>

Implemented using AspectJ

Pay attention to the configuration of parameters. Once the tangent point is configured with parameters, all notifications must have formal parameters

    <aop:config>
        <aop:aspect ref="myadvice">
            <aop:pointcut expression="execution(* demo.Demo.demo01(String,String)) and args(msg,msg2) " id="mypoint"/>
            <aop:before method="mybefore1" pointcut-ref="mypoint" arg-names="msg,msg2" ></aop:before>
            <aop:after method="myafter" pointcut-ref="mypoint" ></aop:after>
<!--            <aop:after method="myafter" pointcut-ref="mypoint" arg-names="msg,msg2" ></aop:after>-->
        </aop:aspect>
    </aop:config>
    <bean id="demo" class="demo.Demo"></bean>
    <bean id="myadvice" class="com.xs.advice.MyAdvice"></bean>

The essence is to take the parameters of the tangent point to the notification, so that the notification can operate on it.

    public void mybefore1(String msg,String msg2){
        System.out.println("1:"+msg);
        System.out.println("2"+msg2);
    }

    public void myafter(String msg,String msg2){
        System.out.println("after");
    }

proxy pattern

JDK dynamic agent

public class Test {
    public static void main(String[] args) {
        MyProxy proxy = new MyProxy();
        //Class loader, which interface is implemented, and which object's invoke method is called
        //Return interface object (in fact, a proxy object implementing the interface is created, which is the returned interface object)
        //When the Method is called by the interface object, the Method is passed into the Method parameter of invoke and the invoke Method is called. In this Method, the relevant methods of Real object are called by using invoke(r,args))
        Impl impl = (Impl) Proxy.newProxyInstance(Test.class.getClassLoader(), new Class[]{Impl.class}, proxy);
        impl.sing();
    }
}
public class MyProxy implements InvocationHandler {
    private Real r = new Real();

    @Override
    //Proxy created proxy object Method method (reflected Method) args parameter
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Front");
        Object result = method.invoke(r, args);
        System.out.println("Postposition");
        return result;
    }
}

cglib dynamic proxy

Remember to guide the bag

  • Based on bytecode, subclasses of real objects are generated, which has high operation efficiency
        System.out.println("Front");
        //The generated subclass object calls the method that needs proxy
//        Object result = method.invoke(o, objects);// report errors?!!!!!! To be solved
        //The generated subclass object calls the Super method of the method requiring proxy, that is, the method in the previous line
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("Postposition");
        return result;
        //The subclass is generated according to the class and object conveyed
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Real.class);
        enhancer.setCallback(new Proxy());

        Real r = (Real) enhancer.create();
        r.sing();

Using annotations

Component scan

<context:component-scan base-package="com.xs.advice"></context:component-scan>
  • @The default Component name is lowercase Component id
  • @Component @Aspect configuration notification
  • Since annotations are based on cglib, we need cglib dynamic proxy
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

Automatic injection

  • Default: the default value, according to the global default autowire = "value By default, when both global and local configurations are not configured, it is equivalent to no
  • No: no automatic injection
  • byName: Auto injection by name Find the id of the class in the Spring container
  • byType: inject by type Two bean s of the same type cannot appear in the spring container
  • Constructor: the constructor that provides the corresponding parameters according to the constructor injection (the constructor parameter contains the one corresponding to the injection) uses byName at the bottom. The constructor parameter name is the same as the id of other bean s
  • Use default autowire globally

Load properties file

Multiple files are separated by commas

    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

Automatic injection will affect the injection of sqlSessionFactory. It needs to be changed to the following form

<property name="sqlSessionFactoryBeanName" value="factory"></property>

Assign a value to a property using the @ Value(${}) annotation

Remember to add the label scan of the scanning package

Scope properties

  • bean properties
  • Effective range of control object
  • singleton default value, single example
  • Multiple instances of prototype, re instantiated each time
  • Request re instantiate each request
  • Session within each session object, the object is singleton
  • Application is a singleton in the application object
  • An object launched by global session spring, which depends on spring webmvc portlet, is similar to session

Declarative transaction

  • The transaction control code has been written by spring The programmer only needs to declare which methods need transaction control and how to do transaction control
  • The transaction manager is based on advice
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="insert"/>
        </tx:attributes>
    </tx:advice>

Same as configuration section

  1. name = "which methods need transaction control
    1.1 support * wildcards
  2. Whether readonly = "boolean" is a read-only transaction
    2.1 if true, tell the database that this transaction is read-only Data optimization will improve performance to a certain extent, so as long as it is a query method, it is recommended to use this data 2.2 if it is false, the transaction needs to be committed It is suggested to add, delete and modify
  3. Propagation controls the behavior of transaction propagation
    3.1 how to manage a transaction when a method with transaction control is called by another method with transaction control (create a new transaction? Execute in the transaction? Suspend the transaction? Report an exception?)
    3.2 REQUIRED (default): if there is a transaction, it will be executed in the transaction. If there is no transaction, a new transaction will be created
    3.3 SUPPORTS: if there is a transaction, it will be executed in the transaction. If there is no transaction, it will be executed in the non transaction state
    3.4 MANDATORY: it must be executed within the transaction. If there is a transaction, it will be executed in the transaction. If there is no transaction, an error will be reported
    3.5 REQUIRES_NEW: must be executed in a transaction. If there is no transaction at present, create a new transaction. If there is a transaction at present, suspend the current transaction
    3.6 NOT_SUPPORTED: it must be executed under non transaction. If there is no transaction at present, it will be executed normally. If there is a transaction at present, the current transaction will be suspended
    3.7 NEVER: it must be executed in a non transaction state. If there is no transaction at present, it will be executed normally. If there is a transaction at present, an error will be reported
    3.8 NESTED: must be executed in transaction status If there is no transaction, create a new transaction. If there is a current transaction, create a nested transaction
  4. Isolation = "transaction isolation level
    4.1 how to ensure the integrity of accessed data under multithreading or concurrent access
    4.2 dirty reading:
    4.2.1 when A transaction (A) reads uncommitted data in another transaction (B), the data in another transaction may be changed. At this time, the data read by transaction A may be inconsistent with the data in the database. At this time, the data is considered dirty data, and the process of reading dirty data is called dirty reading
    4.3 non repeatable reading:
    4.3.1 it is mainly aimed at a certain line of data (or a column in a row)
    4.3.2 the main operation is modification
    4.3.3 two reads are in the same transaction
    4.3.4 after transaction A reads the transaction for the first time, transaction B modifies the Shujun read by transaction A. the data read again in transaction A is inconsistent with the data read before, and the process cannot be read repeatedly
    4.4 unreal reading:
    4.4.1 the main operations are adding or deleting
    4.4.2 results of two transactions
    4.4.3 transaction A finds the result according to specific conditions, and transaction B adds A piece of qualified data If the data queried in transaction A is inconsistent with the data in the database, transaction A seems to have an illusion, which is called Unreal reading
    4.5 DEFAULT: the default value. The underlying database automatically determines what isolation domain should be used
    4.6 READ_UNCOMMITTED: uncommitted data can be read, dirty reading may occur, no repeated reading, unreal reading
    4.6.1 highest efficiency
    4.7 READ_COMMITTED: only committed data of other transactions can be read It can prevent dirty reading, which may lead to unrepeatable reading and unreal reading
    4.8 REPEATABLE_READ: the read data is locked to prevent other transactions from modifying the data, which can prevent non repeated reading Dirty reading may lead to unreal reading
    4.9 SERIALIZABLE: queue operation to add locks to the whole table When a transaction is operating data, another transaction can only operate the table after the transaction operation is completed
    4.9.1 safest
    4.9.2 the one with the lowest efficiency
  5. Rollback for = "exception type fully qualified path"
    5.1 rollback is required in case of any exception
    5.2 suggestion: give the attribute value
    5.2.1 the attribute value must be given when throwing an exception manually
  6. No rollback for = "6.1 do not roll back the transaction in case of any exception

Tags: Java Spring SSM

Posted by naboth_abaho on Thu, 05 May 2022 16:22:45 +0300