zoukankan      html  css  js  c++  java
  • Spring学习笔记--Spring配置文件和依赖注入

    Spring配置文件

    1.alias:设置别名,为bean设置别名,并且可以设置多个别名;

    <!-- 设置别名 -->
           <alias name="user" alias="user1"/>

    2.bean的配置;

        <!--id是bean的标识符,要唯一,如果没有配置id,name默认为标识符 
             如果配置了id,又配置了name,那么name就是别名
            name可以设置多个别名并且别名可以是空格 逗号 分号
            class是bean的全限定名=包名+类名
            如果不配置id和name,则可以根据applicationContext.getBean(Class)获取对象
        -->
        <bean id="h1" name="hello h2,h3;h4" class="cn.sxt.bean.Hello">
            <property name="name" value="张三"></property>
        </bean>

    3.团队协作开发,通过import来实现;

    在我们的beans.xml中就可以引入和使用entity.xml文件中的bean配置(代码中config/spring为我们新建的包config.spring)

    <import resource="config/spring/entity.xml"/>

    而entity.xml中进行bean的详细配置:

        <bean id="h1" name="hello h2,h3;h4" class="cn.sxt.bean.Hello">
            <property name="name" value="张三"></property>
        </bean>

    Bean的作用域

    scope指bean的作用域,在配置bean时,有scope属性来配置bean的作用

    注意:在整合struts和spring时,需要将action设为scope="prototype";

     <bean id="addr" class="cn.sxt.vo.Address" scope="singleton">
        <!-- scope:bean的作用域 (默认是singleton):
        singleton:单例(用计数器记录),整个容器中只有一个对象的实例;
        prototype原型:每次获取bean都产生一个新的对象;
        request:每次请求时,创建一个新的对象;
        session:在回话的范围内是一个对象(servlet的session);
        global session: 只在portlet下有用,表示是application
        application:在应用范围内,只有一个对象;
        -->

    Bean的自动装配(spring4)

    是用来简化spring的配置文件,在配置bean时,可以配置bean的autowire属性,用于指定装配类型

    <bean id="userDao" class="cn.sxt.dao.impl.UserDaoMySqlImpl"></bean>
        <!-- autowire:自动装配,用于简化spring的配置
             no     不使用自动装配
             byname 根据名称(set方法名)来查找相应的bean,如果有则装配上去
               使用形式:xml头文件最后面加上default-autowire="byName"
             byType 根据类型进行装配,不同去管bean的id(bean的id可以写任意)
                     但是同一种类型的bean只能有一个(尽量慎用byType)
             constructor 当使用构造器实例化bean时,适用byType的方式装配构造方法
         -->
        <bean id="service" class="cn.sxt.service.impl.UserServiceImpl" autowire="constructor"></bean>

    可以配置全局的自动装配类型,在xml文件的头部:default-autowire="byname"

    【注意】推荐不使用自动装配,而使用annotation

    依赖注入 DI
    1.依赖注入--dependency injection;

     依赖:指bean对象创建依赖于容器,Bean对象的依赖资源

     注入:指bean对象依赖的资源由容器来设置和装配

    2.spring注入--构造器注入

       见IOC创建对象,也就是上一篇笔记中IOC构造方法创建对象的内容(有参和无参)

    3.spring注入--setter注入(重点)

       要求被注入的属性必须有set方法。set方法的方法名由set+属性(属性首字母大写),如果属性是boolean类型,没有get方法(是is);

     3.1 常量注入

     Student.java:

    package cn.sxt.vo;
    
    public class Student {
    	private String name;
    	public void setName(String name) {
    		this.name = name;
    	}
    	public void show(){
    		System.out.println("name="+name);
    	}
    }
    

      beans.xml(主要是这里):

    <?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="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
        </bean>
    </beans>

        Test.java:

    package cn.sxt.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.sxt.vo.Student;
    
    public class Test {
    
    	public static void main(String[] args) {
    		ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
    		Student stu=(Student)ac.getBean("student");
    		stu.show();
    	}
    
    }
    

     3.2 Bean注入

    新建一个类Address:

    package cn.sxt.vo;
    
    public class Address {
    	private String address;
    
    	public String getAddress() {
    		return address;
    	}
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    	
    }

    Student类中引用Address类的对象(Student的实例变量是另外一个类Address的对象):

    private Address addr;
    	public void setAddr(Address addr) {
    		this.addr = addr;
    	}

    beans.xml:

     <bean id="addr" class="cn.sxt.vo.Address">
            <property name="address" value="北京优衣库"/>
        </bean>
        <bean id="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
            <property name="addr" ref="addr"></property>
        </bean>

     3.3 数组注入
    Student类继续添加一个实例变量books以及books的set方法:

    private String[] books;
         public void setBooks(String[] books) {
    	this.books = books;
          }
    

    beans.xml中做以下修改:

        <bean id="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
            <property name="addr" ref="addr"></property>
            <property name="books">
                <array>
                    <value>傲慢与偏见</value>
                    <value>仲夏夜之梦</value>
                    <value>雾都孤儿</value>
                </array>
            </property>
        </bean>

     3.4 List注入

    Student类继续添加实例变量及其set方法:

    private List<String> hobbies;
           public void setHobbies(List<String> hobbies) {
                    this.hobbies = hobbies;
            }

    beans.xml做以下配置:

    <bean id="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
            <property name="addr" ref="addr"></property>
            <property name="books">
                <array>
                    <value>傲慢与偏见</value>
                    <value>仲夏夜之梦</value>
                    <value>雾都孤儿</value>
                </array>
            </property>
            <property name="hobbies">
                <list>
                    <value>羽毛球</value>
                    <value>乒乓球</value>
                    <value>玻璃球</value>
                    <value>排球</value>
                </list>
            </property>
        </bean>

        3.5 Map注入

    Student继续添加实例变量Map<String,String> cards;

    private Map<String, String> cards;
    	public void setCards(Map<String, String> cards) {
    		this.cards = cards;
    	}
    

    beans.xml做以下配置:

        <bean id="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
            <property name="addr" ref="addr"></property>
            <property name="books">
                <array>
                    <value>傲慢与偏见</value>
                    <value>仲夏夜之梦</value>
                    <value>雾都孤儿</value>
                </array>
            </property>
            <property name="hobbies">
                <list>
                    <value>羽毛球</value>
                    <value>乒乓球</value>
                    <value>玻璃球</value>
                    <value>排球</value>
                </list>
            </property>
            <property name="cards">
                <map>
                    <entry key="中国银行" value="1545615345415"></entry>
                    <entry>
                        <key><value>农业银行</value></key>
                        <value>54654861231543</value>
                    </entry>
                </map>
            </property>
            
        </bean>

       3.6 Set注入

    Student类添加实例变量Set<String>games及其set方法;

    private Set<String> games;
    public void setGames(Set<String> games) {
    	this.games = games;
    }
    

    beans.xml做以下配置(都是些重复的步骤):

        <bean id="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
            <property name="addr" ref="addr"></property>
            <property name="books">
                <array>
                    <value>傲慢与偏见</value>
                    <value>仲夏夜之梦</value>
                    <value>雾都孤儿</value>
                </array>
            </property>
            <property name="hobbies">
                <list>
                    <value>羽毛球</value>
                    <value>乒乓球</value>
                    <value>玻璃球</value>
                    <value>排球</value>
                </list>
            </property>
            <property name="cards">
                <map>
                    <entry key="中国银行" value="1545615345415"></entry>
                    <entry>
                        <key><value>农业银行</value></key>
                        <value>54654861231543</value>
                    </entry>
                </map>
            </property>
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>dota</value>
                    <value>cs</value>
                    <value>dnf</value>
                    <value>cf</value>
                    
                </set>
            </property>
            
        </bean>

      3.7 Null注入 
    Student类添加实例变量String wife及其set方法;

    private String wife;
    public void setWife(String wife) {
    	this.wife = wife;
    }
    

    beans.xml做以下修改:

    <bean id="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
            <property name="addr" ref="addr"></property>
            <property name="books">
                <array>
                    <value>傲慢与偏见</value>
                    <value>仲夏夜之梦</value>
                    <value>雾都孤儿</value>
                </array>
            </property>
            <property name="hobbies">
                <list>
                    <value>羽毛球</value>
                    <value>乒乓球</value>
                    <value>玻璃球</value>
                    <value>排球</value>
                </list>
            </property>
            <property name="cards">
                <map>
                    <entry key="中国银行" value="1545615345415"></entry>
                    <entry>
                        <key><value>农业银行</value></key>
                        <value>54654861231543</value>
                    </entry>
                </map>
            </property>
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>dota</value>
                    <value>cs</value>
                    <value>dnf</value>
                    <value>cf</value>
                    
                </set>
            </property>
            <property name="wife"><null/></property>
        </bean>

       3.8 Properties 注入

    Student继续添加实例变量properties info;

    private Properties info;
    public void setInfo(Properties info) {
    	this.info = info;
    }
    

    beans.xml做以下修改:

        <bean id="student" class="cn.sxt.vo.Student">
            <property name="name" value="张三丰"></property>
            <property name="addr" ref="addr"></property>
            <property name="books">
                <array>
                    <value>傲慢与偏见</value>
                    <value>仲夏夜之梦</value>
                    <value>雾都孤儿</value>
                </array>
            </property>
            <property name="hobbies">
                <list>
                    <value>羽毛球</value>
                    <value>乒乓球</value>
                    <value>玻璃球</value>
                    <value>排球</value>
                </list>
            </property>
            <property name="cards">
                <map>
                    <entry key="中国银行" value="1545615345415"></entry>
                    <entry>
                        <key><value>农业银行</value></key>
                        <value>54654861231543</value>
                    </entry>
                </map>
            </property>
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>dota</value>
                    <value>cs</value>
                    <value>dnf</value>
                    <value>cf</value>
                    
                </set>
            </property>
            <property name="wife"><null/></property>
            <property name="info">
            <props>
                <prop key="学号">2015534001</prop>
                <prop key="sex"></prop>
                <prop key="name">张三</prop>
            </props>
            </property>
        </bean>

     3.9 p命名空间注入

    beans.xml头文件添加以下内容:

    我们新建了一个User类:包含name和age属性

    beans.xml配置形式如下所示:

     <bean id="user" class="cn.sxt.vo.User" p:name="风清扬" p:age="230"></bean>

       3.10 c注命名空间入(构造函数注入)
    beans.xml头文件添加以下内容:

    c命名空间注入要求有对应参数的构造方法:

    public User(String name, int age) {
    	super();
    	this.name = name;
    	this.age = age;
    }
    

     beans.xml做以下配置:

        <bean id="u1" class="cn.sxt.vo.User" c:name="coco" c:age="16"></bean>

    总结我们之前学习的内容:

    spring--桥梁

    spring--轻量级,易学,ioc,aop,事务,整合框架等

    spring--ioc:控制反转(权限的反转)

    spring--Di(和ioc一个概念,只不过是站在不同的角度)

  • 相关阅读:
    校验文件数字签名的合法性(VerifyPE)
    windbg help
    VIM的常用操作
    使用Jsonp解决跨域数据访问问题
    Linux下不同文件编码的转换
    Segoe Font 介绍
    关于框架的一些思考
    Linux下Aptana Plugin For Eclipse的破解
    增强网站易用性的10个设计技巧
    Firebug中的console tab使用总结
  • 原文地址:https://www.cnblogs.com/ysw-go/p/5991030.html
Copyright © 2011-2022 走看看