spring01- Quick Start

spring


  1. What is the spring
    • Spring is an open source framework
    • spring can simplify enterprise application development
    • a spring is in IoC (Inverse Of Control: inversion control) and AOP (Aspect Oriented Programming: Oriented Programming) as the core framework
  2. Feature
    • Lightweight:. Spring-based non-invasive application spring development objects may not depend on the spring API
    • Dependency injection: (DI ---> dependency injection, IOC)
    • Aspect Oriented Programming (AOP)
    • Container: spring is a container because it contains and manages the life cycle of the application object
      ...
  3. spring module
    spring module

hello


  1. ready
    • The introduction of dependence

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.8.RELEASE</version>
      </dependency>
    • Create a Hello class

      public class Hello {
          public void hello() {
              System.out.println("hello spring");
          }
      }
    • Write configuration 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="hello" class="cn.ann.Hello"/>
      </beans>
      • Behind a big lump beans are fixed wording tag, copy and paste like anencephaly
      • bean tags, id is a unique identifier, usually the class name first letter lowercase, class is full class name of the class
  2. Write test classes Test

    @Test
    public void demo01() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello = (Hello) ac.getBean("hello");
        hello.hello();
    }

    operation result:
    operation result

IOC & OF


  1. What is?
    • IOC: Ioc-Inversion of Control, that is, "inversion of control", not a technical, but a design idea. In Java development, Ioc means that you designed objects to the container control, rather than the traditional direct control over your internal object. This design idea is to reduce the coupling procedure
    • DI: Dependency Injection, namely "dependency injection": is the dependencies between components at runtime is determined by the vessel, said the image that will be injected by the container dynamic dependencies into a component.
    • Want to know more can see Iteye Tao opened the technical understanding of the cattle IOC Spring framework, and attach links: http://jinnianshilongnian.iteye.com/blog/1413846

Profiles type of analysis and testing


  1. Test category code analysis
    • The first line of the test class new ClassPathXmlApplicationContext(配置文件路径)is initialized IOC container, which may receive a return value or ApplicationContext BeanFactory
      1. BeanFactory: When you create an object using lazy loading strategy, namely: to create only the first acquisition target
      2. ApplicationContext: When you create an object using an immediate loading strategies, namely: after finished reading the configuration file to load the object
    • The second line test class ac.getBean("hello")may acquire the specified object IOC container, id parameter may be a value, it may be a Class object
      1. Parameters are id: Gets the bean id value map
      2. Class object parameters are: obtain the appropriate bean, bean but only a container class, that is: a container only class attribute for that type of bean tag
  2. spring management details of the bean
    1. Three ways to create the bean
      1. Created with the default constructor
        <bean id="user" class="cn.ann.User"/>
        • Subsequent use of the label in spring bean configuration file, together with the id and class, and no other label use is the default constructor to create a bean object, not a class default no-argument constructor, the object can not be created
      2. Use ordinary factory methods to create

        <bean id="userFactory" class="cn.ann.UserFactory"/>
        <bean id="user" factory-bean="userFactory" factory-method="getUser"/>
      3. Created using a static factory method
        <bean id="user" class="cn.ann.StaticFactory" factory-method="getUser"/>
    2. Scope bean object
      • scope attribute bean tag
        1. singleton: singleton (default)
        2. prototype: prototype is more cases
        3. request: the request applied to the web application range
        4. session: session scope is acting in web application
        5. global-session: role in cluster environments session scope (global session scope), when not a cluster, the session is
    3. bean object's life cycle
      1. singleton:
        • Create: Create a container created with
        • Destruction: With the destruction of container and destruction
        • Summary: the same as the life cycle of container
      2. prototype:
        • Creation: The first time objects
        • Destruction: recovered by the Java garbage collector
  3. spring dependency injection
    1. What is?
      • Other objects of the class need to use the current class, there are spring offers us, we only need to maintain dependencies in the configuration file can be
    2. Can inject data: There are three
      1. The basic data types and String
      2. Other types of bean
      3. Complex types, collection types
    3. Injection method: There are three (Note, constantly changing data is not suitable for injection)
      1. Using Constructors
      2. Using the set method
      3. Use annotations
    • Use constructor injection

      <bean id="user" class="cn.ann.User">
          <constructor-arg name="" value="" type="" index=""></constructor-arg>
      </bean>
      • Constructor-arg need to use tags, attributes:
        • Type of data to be inserted specified type, the constructor parameter is one or some of the parameters: type
        • index: an index for the data specifying the insert position, zero
        • name: Specifies the name of the parameter assignment for the constructor
        • The above three parameters to the constructor is used to specify which parameter assignment, but the most commonly used when the name
        • value: for providing basic types and other data types
        • ref: used to refer to other types of bean, the container must be some IOC bean object

        • Advantage: If the object does not provide a no-argument constructor, then get the bean, when such data is a must, otherwise the object can not be created successfully
        • Drawbacks: If the data is not required, it can not create object
    • Use injection set (more common)

      <bean id="user" class="cn.ann.User">
          <property name="name" value="zs"/>
          <property name="age" value="23"/>
          <property name="birthday" ref="date"/>
      </bean>
      • Need to use property tags, attributes:
        • name: the name of the attribute used to specify injection
        • value: a value of the specified property injection
        • ref: used to refer to other types of bean, the container must be some IOC bean object

        • Advantage: directly call the default constructor to create objects like, there is no clear limit
        • Drawbacks: getting the object set method may not be performed
    • Complex type injection
      • bean property (omitted getter, setter and toString):

        public class CollectionDemo {
            private String[] strings;
            private List<String> list;
            private Set<String> set;
            private Map<String, String> map;
            private Properties prop;
        }      
      1. array type (String [])

        <property name="strings">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </array>
        </property>
      2. List type

        <property name="list">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>
      3. Set Types

        <property name="set">
            <set>
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </set>
        </property>
      4. Map Types

        <property name="map">
            <map>
                <entry key="key01" value="val01"/>
                <entry key="key02" value="val02"/>
                <entry key="key03" value="val03"/>
            </map>
        </property>
      5. Properties types

        <property name="prop">
            <props>
                <prop key="prop01">val01</prop>
                <prop key="prop02">val02</prop>
                <prop key="prop03">val03</prop>
            </props>
        </property>

    • Note: array, list, and can be set to the injection list structure; entry and props can be injected to the Map Structure


Code links: here 's spring01-quickStart

Guess you like

Origin www.cnblogs.com/ann-zhgy/p/11776253.html