zoukankan      html  css  js  c++  java
  • Spring 基础知识

    一、Spring的概述

      Spring是一种简化了企业级开发的MVC框架。spring要学习的内容:

         1,IOC(Inverse of Control控制反转)--掌握
         2,AOP -- 了解(理解代理思想)
         3,对持久层的支持(JDBC,Hibernate)
         4,SSH的整合
         5,SpringMVC

    二、环境搭建

      1.Spring的应用

         Spring可以用在任何工程中(Java Project,web Project)。

      2.配置文件

        applicationContext.xml -- 默认放在src根目录。该文件的位置可以随意存放(任何一个包结构下);文件的名字可以随便取,数量任意多个。

    三、IOC——控制反转(Inverse of Control)

      把对象的创建权交给Spring容器。

      1.未使用IOC时对象创建

          public class UserDao{

          }
          public class UserService{
              UserDao ud = new UserDao();
          }  

                其中,UserDao对象的创建要依赖于UserService

      2.使用IOC时

           public class UserService{
              UserDao ud;//属性--创建交给Spring容器
           }

        其中,把对象的创建交给Spring容器叫做依赖注入。注入包含两步,Spring容器创建对象--实例化;容器还会对属性初始化。

    四、注入的实现方式

    1,set注入--最常用的注入方式

      1.简单类型的注入

        简单类型的注入包括8种基本数据类型和String。

           a.配置文件:

           <beans>
                  <bean>--配置JavaBean(创建对象)
                     属性:
                id:唯一标示(引用)
                name:跟id作用相似(有了id,就不用name)
                class:类全名

              子标签:
                <property name="类的属性名"></property>
                配置简单类型的属性
              </bean>
           </beans>

      例如:同包结构目录下的SomeBean.java和some.xml为例的代码

    public class SomeBean {//SomeBean.java
        private String name;
        private int age;
        private double salary;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public double getSalary() {
            return salary;
        }
        public void setSalary(double salary) {
            this.salary = salary;
        }        
    }

       <bean id="some" class="包名.SomeBean">//some.xml
         <property name="name" value="zhangsan"></property>
         <property name="age">
           <value>30</value>
         </property>
         <property name="salary" value="12.36"></property>
       </bean>

        b.应用程序:

          1,实例化Spring容器

            方法一:ApplicationContext ac = new FileSystemXmlApplicationContext("src/包名(url形式)/配置文件名.xml");

            方法二:ApplicationContext ac = new ClassPathXmlApplicationContext("包名(url形式)/配置文件名.xml");(常用的)

            例如配置文件为some.xml的实例化:ApplicationContext ac = new ClassPathXmlApplicationContext("com/ioc/set/simple/some.xml");

          2,从容器中获得bean对象

            SomeBean sb = (SomeBean) ac.getBean("bean标签的id属性的值");//默认的是单例模式

            例如:SomeBean sb = (SomeBean) ac.getBean("some");//默认的是单例模式

      2.对象类型注入

        a.内嵌

                   把一个bean嵌在另一个bean中.问题:不能直接通过getBean()获得bean对象。

         例如:相同的包结构中,SomeBean.java和OtherBean.java及some.xml文件

    //SomeBean.java

    public class SomeBean {
        private String name;
        private int age;
        private OtherBean other;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public OtherBean getOther() {
            return other;
        }
        public void setOther(OtherBean other) {
            this.other = other;
        }
    } 

    //OtherBean.java

    public class OtherBean {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }  

    //some.xml部分关键

       <bean id="some" class="包名/SomeBean">
         <property name="name" value="zhangsan"></property>
         <property name="age">
           <value>30</value>
         </property>
         <property name="other">
            <bean id="other" class="包名/OtherBean">
               <property name="name" value="otherBean"></property>
            </bean>
         </property>

     </bean>

    //APP关键代码

          ApplicationContext ac = new ClassPathXmlApplicationContext("包名/some.xml");

          SomeBean sb = (SomeBean) ac.getBean("some");//默认的是单例模式

        b.分离

          给外部的属性单独写一个<bean>。在外部bean中加上<ref local="外部bean的id值"/>。
          ref 属性:
               local="bean的id"   -- 从同一个配置文件中寻找bean
               bean="bean的id" -- 从其它配置文件中寻找bean

      前提:实例化容器的时候,必须把用到的所有的配置文件都加载进来

             例如:相同的包结构中,SomeBean.java、OtherBean.java、other.xml及some.xml文件

    方法一:其他配置文件中找bean

    //SomeBean.java、OtherBean.java同上

    //some.xml部分关键

      <bean id="some" class="包名.SomeBean">
         <property name="name" value="zhangsan"></property>
         <property name="age">
           <value>30</value>
         </property>

      <property name="other">
            <ref bean="other"/>
         </property>
       </bean>

    //other.xml部分关键

     <bean id="other" class="包名/OtherBean">
           <property name="name" value="otherBean"></property>
      </bean>

     

    //APP关键代码

    String[] paths = {"包名/some.xml","包名/other.xml"};

     

          ApplicationContext ac = new ClassPathXmlApplicationContext("包名/some.xml");

     

          SomeBean sb = (SomeBean) ac.getBean("some");//默认的是单例模式

     

    方法二:同一配置文件中找bean

     

    //some.xml部分关键

     

      <bean id="some" class="包名.SomeBean">
         <property name="name" value="zhangsan"></property>
         <property name="age">
           <value>30</value>
         </property>

     

      <property name="other">
            <ref local="other"/>
         </property>
       </bean>

     <bean id="other" class="包名.OtherBean">
           <property name="name" value="otherBean"></property>
      </bean>

     

    //APP关键代码

     

          ApplicationContext ac = new ClassPathXmlApplicationContext("包名/some.xml");

     

          SomeBean sb = (SomeBean) ac.getBean("some");//默认的是单例模式

     

      3.集合注入

        a.List注入

        b.Set注入

        c.Map注入

      例如:同包结构下的some.xml、SomeBean.java及OtherBean.java文件

    //SomeBean.java

    public class SomeBean {
        private List listAttr;
        private Set setAttr;
        private Map mapAttr;
        public List getListAttr() {
            return listAttr;
        }
        public void setListAttr(List listAttr) {
            this.listAttr = listAttr;
        }
        public Set getSetAttr() {
            return setAttr;
        }
        public void setSetAttr(Set setAttr) {
            this.setAttr = setAttr;
        }
        public Map getMapAttr() {
            return mapAttr;
        }
        public void setMapAttr(Map mapAttr) {
            this.mapAttr = mapAttr;
        }        
        public void printListAttr(){
            for(Object o:listAttr){
                System.out.println(o+","+o.getClass());
            }
        }
        public void printSetAttr(){
            for(Object o:setAttr){
                System.out.println(o+","+o.getClass());
            }
        }
        public void printMapAttr(){
            Set keySet = mapAttr.keySet();
            for(Object o:keySet){
                System.out.println(o+","+mapAttr.get(o));
            }
        }
    }

    //OtherBean.java

    public class OtherBean {
        private String name;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    //some.xml部分关键

       <bean id="some" class="包名/SomeBean">
          <property name="listAttr">
             <list>
                <value>hello</value>
                <value>30</value>
                <ref local="other"/>
             </list>
          </property>  
          <property name="setAttr">
            <set>
               <value>hello</value>
                <value>30</value>
                <ref local="other"/>
            </set>
          </property>         
          <property name="mapAttr">
            <map>
              <entry>
                 <key>
                   <value>k1</value>
                 </key><!-- key标签中不允许出现文本,只能是标签 -->
                 <value>v1</value>
              </entry>         
              <entry>
                <key>
                  <value>k2</value>
                </key>
                <ref local="other"/>
              </entry>         
              <entry>
                <key>
                  <ref local="other"/>
                </key>
                <value>hello</value>
              </entry>
            </map>
          </property>
        </bean>    
        <bean id="other" class="包名/OtherBean">
           <property name="name" value="otherBean"></property>
        </bean>

    //APP关键代码

            ApplicationContext ac =
                  new ClassPathXmlApplicationContext("包名/some.xml");        
            SomeBean sb = (SomeBean) ac.getBean("some");//默认的是单例模式
            System.out.println(sb);
            sb.printListAttr();
            System.out.println("-----------");
            sb.printSetAttr();
            System.out.println("-----------");    
            sb.printMapAttr();

     

      注:涉及的包名在class属性中必须为包的展开的URL形式,如:com.spring.set ——> com/spring/set

    2,构造器注入

          其中,set方法不一定需要。不同的构造方法(参数不一样,参数列表不一样)。

       a).构造器注入需要配置的标签为<constructor-arg></constructor-arg>

        标签里为构造方法的参数。每一次只能对一个参数进行注入。即多个参数需要多个<constructor-arg>标签。

        
          b).Spring注入的规则:

        *有不同参数个数的多个构造方法(函数),根据参数的个数选择构造方法(函数);
                *有相同的参数个数而类型或数据类型不同的多个构造函数,根据参数的书写罗列前后顺序(类型)选构造函数;
                *默认使用<constructor-arg>标签的时候,该标签的顺序要和构造方法参数的顺序一致。

          c).可以控制顺序来决定该参数为参数列表的第几个参数

        制定参数的位置index(从0开始)。该方法用于不按参数列表写<constructor-arg>标签时,给参数加个属性index。

        <constructor-arg index="1">
                   <value>30</value>
               </constructor-arg>
          d).数量相同、顺序相同、类型不同,可以通过type属性来约束:

         *基本类型--直接写,如:int、char、long等
                   *对象类型--包名.类名,如:java.lang.String

      <constructor-arg type="int">
            <value>30</value>
         </constructor-arg>

      <constructor-arg type="java.lang.String">
           <value>zhang</value>
         </constructor-arg>

    3,自动装配

      让Spring自己选择合适的方式注入。只是对该bean的对象(自定义)类型的属性自动装配。

      自动装配的规则:

    a).byName--根据属性名进行自动装配(set注入)
           类中属性名和<bean>标签的id属性一致。
    b).byType -- 根据类型进行自动装配(set注入)
          类中属性的类型和<bean>标签的class属性一致;
          不要求<bean>标签的id和类的属性名一致。
    c).constructor -- 根据构造器进行自动装配
           类中要有一个带有该属性的构造方法。
    d).autodetect -- 全自动
          自己选择规则:先构造,再set。

  • 相关阅读:
    Learning to Segment Every Thing(偏监督学习)
    特征金字塔-Feature Pyramid Networks for Object Detection
    Mask R-CNN翻译
    04.卷积神经网络_第一周卷积神经网络
    对极几何
    hdu 3572 Task Schedule【 最大流 】
    uva 11624 Fire! 【 BFS 】
    hdu 3549 Flow Problem 【最大流】
    codeforces 277 A Learning Languages 【DFS 】
    poj 2828 Buy Tickets【线段树 单点更新】
  • 原文地址:https://www.cnblogs.com/liuzhenyou/p/4687251.html
Copyright © 2011-2022 走看看