spring learning notes

Posted by james2010 on Wed, 05 Jan 2022 07:51:39 +0100

spring project construction

  • Create a new spring folder in the workspace, then open the folder with IDEA, and create the required module with maven
  • To create a webapp folder, you need to click file - > project structure, and then operate according to the following photos



spring quick start

Use the process (goal: let spring help us create object instances)

  • Coordinates of the imported spring development package

  • Write Dao interface and implementation class

  • Create spring core configuration file (applicationContext.xml)

  • Configure UserDaoImpl (implementation class of the interface) in the configuration file

  • Use spring's API to get the Bean instance (getBean("configuration file"))

Detailed description of configuration file

Bean tag


singleton (default): the object is created when the configuration file is loaded, and the object is created only when prototype: getBean

Dependency injection

  • set method

    Property: the property name of the object; ref: id of the referenced object Bean
  • Construction method

  • Ordinary data type injection (set method, which needs to have a set method in the class)

  • Injection of set (set method)
  • Code in configuration file
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" >
        <property name="strList">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>
        <property name="userMap">
            <map>
                <entry key="u1" value-ref="user1"></entry>
                <entry key="u2" value-ref="user2"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="pro1">aaa</prop>
                <prop key="pro2">bbb</prop>
                <prop key="pro3">ccc</prop>
            </props>
        </property>
    </bean>
    <bean id="user1" class="com.itheima.domain.User" >
        <property name="name" value="Zhang San"></property>
        <property name="addr" value="Beijing"></property>
    </bean>
    <bean id="user2" class="com.itheima.domain.User" >
        <property name="name" value="Li Si"></property>
        <property name="addr" value="Shanghai"></property>
    </bean>
  • Import external profile
  • Other API s

  • service layer (or other places to call code)

Annotation development

  • spring configures the data source (after configuration, you can obtain the connection in the form of getBean in the test file)
    • Load external configuration file in spring configuration file
    • Writing in XML file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--Load external properties file,It stores the relevant information of data source configuration, which can be used ${}To quote, eg: (jdbc.driver=XXX)-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
 </bean>
  • spring original annotation
    • When spring uses annotations, it places the annotations on the classes that need the container to generate instances, that is, it tells the spring container which classes need its help to generate components.
    • The scan component needs to be configured to use annotations

  • spring new annotation
  • spring integrated junit test environment
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml") / / use the configuration file
@ContextConfiguration(classes = {SpringConfiguration.class}) //Use configuration class
public class SpringJunitTest {
        @Autowired //After spring integrates junit, junit can understand annotations
        private UserService userService;

        @Autowired
        private DataSource dataSource;
        @Test
        public void test1() throws SQLException {
            userService.save();
            System.out.println(dataSource.getConnection());
        }
}

AOP

XML based approach

  • Rapid development
    • Import dependency
    • The aop configuration space needs to be referenced first
    • The target object contains the method to be enhanced (tangent point), and the tangent object contains the notification method
    • Tangent expression (written after pointcut)
    • Tangent expression extraction

Annotation based approach

  • quick get start
    • Step 4: configure the weaving relationship in the facet class
    • Step 5: component scanning and AOP agent

Topics: Java Spring