Detailed configuration and concepts of Spring

Detailed configuration and concepts of Spring

Spring quick start

  • 1. Import Spring development package coordinates
    • Import coordinates
  • 2. Write Dao interface and implementation class
    • Create Bean
  • 3. Create Spring core configuration file
    • Create applicationContext.xml
  • 4. Configure UserDaoImpl in the Spring core configuration file
    • Configure the Bean in the configuration file
     <bean id="userDao" class="com.xunmeng.impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="distory"></bean>
    
  • 5. Use the Spring API to obtain the Bean instance
    • Create ApplicationContext object getBean

There are three ways to instantiate spring beans

  • 1. Instantiation of parameterless construction method
<bean id="UserDao" class="com.xunmeng.day_02.dao.Impl.UserDaoImpl"></bean>
  • 2. Factory static method instantiation
    <bean id="UserDao_gcj" class="com.xunmeng.day_02.dao.Impl.UserDaoImpl" factory-method="getUserDao"></bean>
  • 3. Factory instance method instantiation

Three data types of Spring injected data

  • 1. Common data type
  • 2. Reference data type
  • 3. Collection data type

Spring introduces other configuration files

<import resource="applicationContext-xxx.xml"/>

Spring configuration data source

  • 1.1 development steps of data source
    • Import Spring framework
    • ① Import the coordinates of the data source and database driven coordinates
    • ② Create data source object
    • ③ Set the basic link data of the data source
    • Use the data source to obtain linked resources and return connected resources

Spring annotation

  • 1. Configure annotation scanning in spring XML
  • 2. Add annotation in bean
  • 3.Spring annotation
    • @Configuration: used to specify that the current class is a Spring configuration class
    • @ComponentScan: used to specify the package to be scanned by Spring's xml when initializing the container
    • @Bean: used on a method, the annotation stores the return value of the method in the Spring container
    • @PropetySource: the configuration used to load the. properties file
    • @Import: used to import other configuration classes

Spring integration Junit

  • Let Spring JUnit be responsible for creating the Spring container, but you need to tell it the name of the configuration file
  • The Bean that needs to be tested is injected directly into the test class
  • Spring integration Junit steps
    • ① Import the coordinates of Spring integrated Junit
    • ② Replace the original runner with the @ Runwith annotation
    • ③ Use @ ContextConfiguration to specify a profile or configuration class
    • ④ Inject the object to be tested with @ Autowired
    • ⑤ Create a test method to test

Spring integrated web environment

  • Steps to integrate the Spring web Environment
    • ① Configuring the ContextLoaderListener listener
    • ② Get application context using WebApplicationContextUtils

Spring MVC file upload

  • 1. Three elements of file upload
    • 1. Form item type = "file"“
    • 2. The form submission method is post
    • 3. The entype attribute of the form is a multi part form, and enctype = "multipart / form data"
  • 2. Single file upload steps
    • Import fileupload and io coordinates
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>
    
    • Profile upload parser
        <bean id="commonsMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="500000"></property>
    </bean>
    
    • Write file upload code

Spring MVC interceptor - handlerintercept

  • Key points of custom interceptor
    • ① Create an interceptor to implement the handlerreport interface
    • ② Configure the interceptor in the spring MVC configuration file
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.xunmeng.Interceport.MyhandleInterceport"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    • ③ Test the interception effect of the interceptor

Spring MVC exception handler

  • Treatment method
    • Configure simple exception handler SimpleMappingExceptionResolver
      – just add bean s to spring MVC
    • Custom exception handler
  • Custom exception handling steps:
    • ① Create an exception handler to implement HandleExceptionResolver
    • ② Configure the exception handler and directly configure the bean
    • ③ Write exception page
    • ④ Test abnormal jump
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="/error.jsp"/>
        <property name="exceptionMappings">
            <map>
                <entry key="ClassCastException" value="/error.jsp"></entry>
            </map>
        </property>
    </bean>
    

Spring AOP

  • Configuration steps using annotation
    • 1. Package aspectjweaver corresponding to the leader
    • 2. Create the corresponding target class and the interface implemented by the target class
    • 3. Create section class
    • 4. Annotation configuration:
      • 1. First add @ Component to the target class and put it into the Spring container
      • 2. Add @ Component and @ Aspect to the facet class, put them into the Spring container, and indicate that they are a pointcut
      • 3. Weave the tangent point and notification together, and add @ Before and other notification annotation marks on the notification function.
      1. test

Spring's declaration control based on XML

<!--    Configure connection pool-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"/>
<!--Configure platform transaction manager-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        configuration parameter-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

<!--    Enhancement of notification transactions-->
    <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

<!--    Configuring the weaving of transactions-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xunmeng.service.*.*(..))"></aop:advisor>
    </aop:config>

Spring annotation based declaration control

  • 1. Configuration steps
    • 1. Add the @ Component annotation to the service layer class and add it to the Spring container. Of course, if the service wants to call the dao layer, the dao layer class should also add the @ Component annotation to the Spring container so that it can be injected into the lead service.
    • 2. Configure the relevant platform transaction manager driver in spring.xml so that the program knows the driver.
    <!--Configure platform transaction manager-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--configuration parameter-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    • Use @ transactional to modify classes or methods that need transaction control. The attributes available for annotation are consistent with xml configuration methods, such as isolation level, propagation behavior, etc.
  • 2. Precautions:
    • If annotations are used on a class, all methods under the class are configured with the same set of annotation parameters.
    • In terms of methods, different methods can adopt different transaction parameter configurations.
    • The annotation driver to turn on the transaction in the xml configuration file
    <tx:annotation-driven transation-manager="{Platform transaction manager id}"/>
    

Keywords: Java JavaEE Spring

Added by amiller099 on Sun, 31 Oct 2021 22:01:58 +0200