javaSpring study concluded day_01

This article summarizes their learning for knowledge, inadequate or wrong with the clear understanding. bean.xml file read mode:    the ClassPathXmlApplicationContext: It is only under the loading profile classpath          recommended 1. Load Profile:       the ClassPathXmlApplicationContext the ClassPathXmlApplicationContext new new AC = ( "bean.xml");     2. Gets the id of the object according bean       CS1 = ICustomerService (ICustomerService) ac.getBean ( "the CustomerService");    FileSystemXmlApplicationContext: it is possible to load the disk configuration files anywhere


    




Bean created two rules: 
  BeanFactory:
Provided is a delay in loading idea to create the bean object. Namely: bean objects when to use, when to create
    ApplicationContext:
Provided is a load immediately thought to create the bean object. Namely: Once we are able to resolve a configuration file, you create a bean object
      1. Obtain the container 
      the ApplicationContext the ClassPathXmlApplicationContext new new AC = ( "bean.xml");
      2. The acquisition target of bean ID
      ICustomerService CS1 = (ICustomerService) ac.getBean ( "CustomerService");
      cs1.saveCustomer ();
Bean's scope:
it is possible to adjust the scope by way of configuration
attributes configuration: scope attribute of the bean tag
values:
Singleton: Singleton (default)
prototype: multiple cases of (when we let the spring to take over the struts2 when action is created, action must configure this value)
request: scope is a request, and the current request forwarded
session: a session scope is
globalsession: scope is a global session
bean's life cycle:
two bean properties designed labels :
the init-Method,
the destroy-Method,
singleton:
born: create a container object born
alive: as long as the container, the object in
death: the destruction of container, objects demise of
many cases of:
birth: each use when creating objects
Alive: as long as the object is in use, has been living
death: when the object is not used for a long time, and no other object reference by the java garbage collected
Bean created three ways: 
The first way: call the default no-argument constructor to create commonly used
by default, if there is no default no-argument constructor, the creation fails, an exception will be reported

    bean.xml in configuration:
  <bean the above mentioned id = " customerService "class =" spring.service.impl.CustomerServiceImpl "> </ bean>
  The second way: to create an object using a static factory method in the
need to use bean factory-method attribute tag, specify static factory method to create an object in
    the configuration bean object created using a static factory
to create an object using the factory-method class, with id extracted

    bean.xml configuration:
      <the bean ID = "staticCustomerService" class = "spring.factory.staticFactory" Factory-Method = "getCustomerService"> </ the bean>
    staticFactory.java中配置:
      public static ICustomerService getCustomerService(){ return new CustomerServiceImpl();
  The third way: creating usage example of the plant.
    Tag required bean factory-method property specifies exemplary factory method to create an object 
configuration bean object factory to create Example
    bean.xml中配置:
  <bean id="instanceFactory" class="spring.factory.instanceFactory"></bean>
  <bean id="instanceCustomerService" class="spring.factory.instanceFactory" factory-method="getCustomerService"></bean>

   instanceFactory.java中配置:
      public static ICustomerService getCustomerService(){ return new CustomerServiceImpl(); }
Spring's dependency injection:
  the injection of three ways:

    first: constructor injection using
    a second: injection method using the set
    third: annotations injection

  using constructor injection:
    Label according to: constructor-arg 
    Properties tab:
   type: Specifies the type of the parameter
   index: index position specified parameters, starting from 0
   name: Specifies the name of the parameter (conventional)
   ======== above three properties are assigned to which parameter assignment, the following two properties to specify what is the value assigned ========
   value: specifies the basic data types or data type String
   ref: specify other bean types of data
    location tag appear: bean tag internal     bean.xml configuration:
    
      <bean id="customerService" class="spring.service.impl.CustomerServiceImpl">
       <constructor-arg name="driver" value="com.mysql.jdbc.Driver"></constructor-arg>
       <constructor-arg name="port" value="3306"></constructor-arg>
      <constructor-arg name="today" ref="now"></constructor-arg>
      </bean>
      由于上文的配置使用了ref,所以需要在bean.xml中配置ref所用的类型:
      <bean id="now" class="java.util.Date"></bean>
  Using the set injection method:
    Label according to: property 
    tag attributes:
   name: the specified parameter set method name
   value: Specifies the basic data types or String data
   ref: specify other bean types of data
    location tag appear: bean Tags Internal Note: using the set method of injection required It has set the target injection method, otherwise the configuration error. (Spring injection error can check here)    bean.xml configuration file:
    
    
    <bean id="customerService" class="spring.service.impl.CustomerServiceImpl">
     <property name="driver" value="com.mysql.jdbc.Driver"></property>
     <property name="port" value="3307"></property>
     <property name="today" ref="now"></property>
    </bean>
    <bean id="now" class="java.util.Date"></bean>

    CustomerServiceImpl.java中需要有:
    public void setDriver(String driver) { this.driver = driver; }
    public void setPort(Integer port) { this.port = port; }
    public void setToday(Date today) { this.today = today; }

   Use annotations injection: Introduction day_02 the 

  data type of injection:
    the injection data types are 3 categories:

      the first category: the basic type of type String and
      second type: other bean types (must be appeared in the spring configuration file bean )
      The third category: complex type (collection)
      <the bean ID = "CustomerService" class = "spring.service.impl.CustomerServiceImpl"> 
       <Property name = "Driver" value = "com.mysql.jdbc.Driver"> </ Property>
       <Property name = "Port" value = "3307"> </ Property>
       <Property name = "Today" REF = "now"> </ Property>
      </ the bean>
      <the bean ID = "now" class = "java.util.Date"> </ the bean >       the above example: value of type String is in the first category, the second category is the ref, ref since other bean types, it is necessary to configure this type of bean now in ref below.
    
    The third category: a complex type injection 
  the same configuration, the label can be interchangeable
  , such as: map and properties interchangeable
   list, array, set exchange can twenty-two
        <bean id="customerService" class="spring.service.impl.CustomerServiceImpl">
    <property name="myStrs">
  <array>
   <value>AAA</value>
   <value>BBB</value>
   <value>CCC</value>
   </array>
   </property>

   <property name="myList">
   <list>
   <value>AAA</value>
   <value>BBB</value>
   <value>CCC</value>
   </list>
   </property>

   <property name="mySet">
   <set>
   <value>AAA</value>
   <value>BBB</value>
   <value>CCC</value>
   </set>
   </property>

   <property name="myMap">
   <map>
   <entry key="testD" value="DDD"></entry>
   <entry key="testE">
   <value>EEE</value>
   </entry>
   <!--上面两种表示方法都可以-->
   </map>
   </property>

   <property name="myProps">
   <props>
   <prop key="testF">FFF</prop>
   <prop key="testG">GGG</prop>
   </props>
   </property>
    </bean>

交换标签后

     <property name="myStrs">
     <list>
     <value>AAA</value>
     <value>BBB</value>
     <value>CCC</value>
     </list>
     </property>

     <property name="myList">
     <array>
     <value>AAA</value>
     <value>BBB</value>
     <value>CCC</value>
     </array>
     </property>

     <property name="myMap">
     <map>
     <props>
     <prop key="testF">FFF</prop>
     <prop key="testG">GGG</prop>
     </props>
     </map>
     </property>

     <property name="myProps">
     <entry key="testD" value="DDD"></entry>
     <entry key="testE">
     <value>EEE</value>
     </entry>
     </property>

Guess you like

Origin www.cnblogs.com/shallowcmz/p/11686811.html