SSH笔记-Spring基本配置方法(注入方式、引用bean、级联属性、集合属性、Properties属性、公用的集合bean、p命名空间)

1、必须jar包:
commons-logging-x.x.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar

2、IOC:容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源
如:IOC容器中包含A、B两个对象,B中有A对一个属性,并且有对应set方法, IOC容器负责设置A、B的关联关系,获取的时候,只需要获取B对象就能拿到A和B两个对象了

3、DI:IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入

4、在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化

5、Spring提供了两种类型的IOC容器:BeanFactory(IOC容器的基本实现)、ApplicationContext(提供了更多的高级特性,是BeanFactory的子接口)

6、一般情况下,都会直接用ApplicationContext实现IOC容器

7、配置方式:①属性注入 ②构造器注入

8、注入区别:
①属性注入:通过bean的set()方法单独为某个属性注入值
②构造注入:通过index/name/type参数,整体地给bean的属性注入值

9、注入属性值细节:
①特殊字符需要用<![CDATA[]]>标签包着
②通过property或者constructor-arg的ref属性引用其他bean
③引用内部bean时,可以在property或者constructor-arg中配置一个bean
④使用级联属性为bean类赋值时,被级联的属性需要先被初始化
⑤集合属性,可以在可以在property或者constructor-arg中使用<list>或<set>或<map> 即可
⑥配置properties属性时,使用props和prop为Properties属性赋值,prop中的属性名不需要被定义
⑦配置公用集合bean时,使用<util:list> 来配置公用bean,调用时只需要使用ref属性即可
⑧通过p命名空间为bean属性赋值,需要先导入p命名空间,即:p:id 相当于 name=”id”

10、Spring使用步骤:
①导入jar包
②创建bean类
③创建配置文件
④在配置文件中通过<bean> 来配置之前创建的bean
⑤java代码中使用ApplicationContext创建IOC容器
⑤java代码中使用ClassPathXmlApplicationContext加载配置文件,并赋值给IOC容器
⑥实例化bean,并从IOC容器中获取Bean实例
⑦调用Bean中的方法或获取IOC容器储存在Bean里面对应属性的数据

11、demo中的类的作用
①Test.java:测试
②TestBean.java:bean类(里面包含另外一个bean,用来测试级联操作和普通bean配置)
③TestBean2.java:TestBean类关联的另一个Bean
④TestBeanMap.java:测试集合属性中的map属性
⑤TestCollection.java:测试公用的集合bean
⑥TestProperties.java:测试配置properties属性值
⑦applicationContext.xml:Spring配置文件

12、Test.java

package com.demo.sshtest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {

    /*
    1、必须jar包:
        commons-logging-x.x.jar
        spring-beans-4.3.9.RELEASE.jar
        spring-context-4.3.9.RELEASE.jar
        spring-core-4.3.9.RELEASE.jar
        spring-expression-4.3.9.RELEASE.jar

    2、IOC:容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源
        如:IOC容器中包含A、B两个对象,B中有A对一个属性,并且有对应set方法, IOC容器负责设置A、B的关联关系,获取的时候,只需要获取B对象就能拿到A和B两个对象了

    3、DI:IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入

    4、在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化

    5、Spring提供了两种类型的IOC容器:BeanFactory(IOC容器的基本实现)、ApplicationContext(提供了更多的高级特性,是BeanFactory的子接口)

    6、一般情况下,都会直接用ApplicationContext实现IOC容器
    */
    public static void main(String[] args) {
        PropertyInject();
        System.out.println("-----------");
        ConstructorInject();
        System.out.println("-----------");
        BeanQuote();
        System.out.println("-----------");
        Cascade();
        System.out.println("-----------");
        Collection();
        System.out.println("-----------");
        properties();
        System.out.println("-----------");
        PublicCollection();
        System.out.println("-----------");
        Pnamespaces();

    }

    public static void PropertyInject(){
        //属性注入

        //1、创建Spring的IOC容器对象
        //创建IOC容器对象时,会调用配置文件中配置的Bean的构造器,并且调用set方法对属性赋值(即:ApplicationContext 在初始化上下文时就实例化所有单例的 Bean)
        //ApplicationContext主要实现类(加载配置文件的方法):
            //(1)ClassPathXmlApplicationContext:表示从该类的类路径下加载配置文件
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            //(2)FileSystemXmlApplicationContext:表示从文件系统中加载配置文件
            //ApplicationContext applicationContext = new FileSystemXmlApplicationContext("E:\\workspace\\SSHnote_SpringBeanConfig1\\src\\applicationContext.xml");

        //2、从IOC容器中获取Bean实例
            //(1)利用id定位到IOC容器中的bean
            //getBean里面写配置文件中配置的bean时指定的id
            TestBean testBean = (TestBean)applicationContext.getBean("testBean1");
            //(2)利用类型返回IOC容器的Bean,要求IOC容器只存在一个该类型的bean
            //TestBean testBean=applicationContext.getBean(TestBean.class);
        //3、调用Bean中的方法或获取IOC容器储存在Bean里面对应属性的数据
            testBean.BeanMethod();
    }

    public static void ConstructorInject(){
        //构造器注入
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("for TestBean(Integer id, String name)");
        TestBean testBean = (TestBean)applicationContext.getBean("testBean2");
        testBean.BeanMethod();
        System.out.println("for TestBean(Integer id, int age)");
        testBean = (TestBean)applicationContext.getBean("testBean3");
        testBean.BeanMethod();
    }

    public static void BeanQuote(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestBean2 testBean2 = (TestBean2)applicationContext.getBean("testBean4");
        System.out.println(testBean2);
    }

    public static void Cascade(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestBean2 testBean2 = (TestBean2)applicationContext.getBean("testBean5");
        System.out.println(testBean2);
    }

    public static void Collection(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestCollection testCollection = (TestCollection)applicationContext.getBean("testCollect");
        System.out.println(testCollection);
    }

    public static void properties(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestProperties testProperties = (TestProperties)applicationContext.getBean("propertiesSource");
        System.out.println(testProperties);
    }

    public static void PublicCollection(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestCollection testCollection = (TestCollection)applicationContext.getBean("testCollectionPublic");
        System.out.println(testCollection);
    }

    public static void Pnamespaces(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestCollection testCollection = (TestCollection)applicationContext.getBean("testPnamespaces");
        System.out.println(testCollection);
    }

}

13、TestBean.java

package com.demo.sshtest;

public class TestBean {

    private Integer id;
    private String name;
    private String name2;
    private int age;

    public TestBean() {
        System.out.println("Enter TestBean's Constructor");
    } 
    public void BeanMethod(){
        System.out.println("BeanMethod()>>   id:"+id+"----"+"name:"+name+"----"+"name2:"+name2+"----"+"age:"+age);
    }
    public TestBean(Integer id, String name, String name2) {
        super();
        this.id = id;
        this.name = name;
        this.name2 = name2;
    }
    public TestBean(Integer id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName2() {
        return name2;
    }
    public void setName2(String name2) {
        this.name2 = name2;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "TestBean [id=" + id + ", name=" + name + ", name2=" + name2 + ", age=" + age + "]";
    }
}

14、TestBean2.java

package com.demo.sshtest;

public class TestBean2 {

    private int age;
    private String str;
    private TestBean testBean;

    public TestBean2(){
        System.out.println("age=" + age + ", str=" + str + ", testBean=" + testBean);
    }

    public TestBean2(int age, String str, TestBean testBean) {
        super();
        this.age = age;
        this.str = str;
        this.testBean = testBean;
    }

    @Override
    public String toString() {
        return "TestBean2 [age=" + age + ", str=" + str + ", testBean=" + testBean + "]";
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
    public TestBean getTestBean() {
        return testBean;
    }
    public void setTestBean(TestBean testBean) {
        this.testBean = testBean;
    }

}

15、TestBeanMap.java

package com.demo.sshtest;

public class TestBeanMap {

    private Integer id;
    private String name;
    private String name2;
    private int age;

    public void BeanMethod(){
        System.out.println("BeanMethod()>>   id:"+id+"----"+"name:"+name+"----"+"name2:"+name2+"----"+"age:"+age);
    }
    public TestBeanMap(Integer id, String name, String name2) {
        super();
        this.id = id;
        this.name = name;
        this.name2 = name2;
    }
    public TestBeanMap(Integer id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName2() {
        return name2;
    }
    public void setName2(String name2) {
        this.name2 = name2;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "TestBean [id=" + id + ", name=" + name + ", name2=" + name2 + ", age=" + age + "]";
    }
}

16、TestCollection.java

package com.demo.sshtest;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestCollection {

    private int age;
    private String str;
    private List<TestBean> testBeans;
    private Set<TestBean> testBeansSet;
    private Map<String,TestBean> testBeansMap;

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
    public List<TestBean> getTestBeans() {
        return testBeans;
    }
    public void setTestBeans(List<TestBean> testBeans) {
        this.testBeans = testBeans;
    }
    public Set<TestBean> getTestBeansSet() {
        return testBeansSet;
    }
    public void setTestBeansSet(Set<TestBean> testBeansSet) {
        this.testBeansSet = testBeansSet;
    }
    public Map<String, TestBean> getTestBeansMap() {
        return testBeansMap;
    }
    public void setTestBeansMap(Map<String, TestBean> testBeansMap) {
        this.testBeansMap = testBeansMap;
    }
    @Override
    public String toString() {
        return "TestCollection [age=" + age + ", str=" + str + ", testBeans=" + testBeans + ", testBeansSet="
                + testBeansSet + ", testBeansMap=" + testBeansMap + "]";
    }
}

17、TestProperties.java

package com.demo.sshtest;

import java.util.Properties;

public class TestProperties {

    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    @Override
    public String toString() {
        return "TestProperties [properties=" + properties + "]";
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

}

18、applicationContext.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"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <!-- 配置bean -->
    <!--
    1、id:需要配置的Bean的id值(在配置文件中bean标签的id必须是唯一的),给java调用ApplicationContext的getBean方法调用的
    2、class:需要配置的Bean的全类名,通过反射方式在IOC容器中创建Bean,Bean中一定要写无参构造器
    3、name:对应Bean类中的属性的set方法名(如:set方法为setStr(),则这里的name写str,即去掉set吧大写的S变成小写)
    4、value:需要为Bean属性设定的值
    -->

    <!-- 通过属性注入方法,给属性注入值 -->
    <bean id="testBean1" class="com.demo.sshtest.TestBean">
        <property name="id" value="1"></property>
        <property name="name" value="AA"></property>
    </bean>

    <!-- 通过构造器注入方法,整体给bean的属性注入值 -->
    <!--
    1、constructor-arg是构造器注入的关键字
    2、使用constructor-arg后,bean会自动去寻找bean中非空构造器进行注入操作
    3、constructor-arg的值,注入顺序是根据非空构造器的参数顺序
    4、如果需要指定constructor-arg的值注入到非空构造器的第几个参数,可以用index属性配置,index的值从0开始
    5、如果遇到重载构造函数,则需要根据数据类型区分
        (1)配置constructor-arg的时候,不同数据类型的那个属性,需要用type来设置注入的匹配条件,type值为java的数据类型全类名
        (2)index和type可以在同一个bean里面都使用
        (3)index和type不可以在同一个constructor-arg里面使用
    -->
    <bean id="testBean2" class="com.demo.sshtest.TestBean">
        <constructor-arg value="bb" index="1"></constructor-arg>
        <constructor-arg value="BB" index="2"></constructor-arg>
        <constructor-arg value="2" index="0"></constructor-arg>
    </bean>
    <bean id="testBean3" class="com.demo.sshtest.TestBean">
        <constructor-arg value="3" index="0"></constructor-arg>
        <!-- 属性值有特殊字符,则需要用CDATA包裹,且要使用constructor-arg的value子节点标签 -->
        <constructor-arg index="1">
            <value><![CDATA[DSFKAJJ23{}\|?]]></value>
        </constructor-arg>
        <constructor-arg value="13" type="int"></constructor-arg>
    </bean>

    <!-- 引用其它 Bean -->
    <!--
    1、作用:使 Bean 能够相互访问
    2、通过 property的ref元素或ref属性为Bean的属性或构造器参数指定对Bean的引用,即:首先要配置好需要引用的bean,然后用ref来为当前配置的bean联系需要配置的bean
    -->
    <bean id="testBean4" class="com.demo.sshtest.TestBean2">
        <property name="age" value="22"></property>
        <property name="str" value="dfdsfsfd"></property>
        <!-- 引用外部的bean 方法1: -->
        <property name="testBean" ref="testBean3"></property>
        <!-- 引用外部的bean 方法2: -->
        <!--
        <property name="testBean">
            <ref bean="testBean3"></ref>
        </property>
        -->
        <!-- 引用内部bean,即内部建立bean -->
        <!--
        1、这里property的name是写Bean类里面创建的类对象名
        2、跟外部的bean一样,只是不能被外部bean引用而已
        -->
        <!--
        <property name="testBean">
            <bean class="com.demo.sshtest.TestBean">
                <constructor-arg index="0" value="5"></constructor-arg>
                <constructor-arg value="d5" index="1"></constructor-arg>
                <constructor-arg value="15" type="int"></constructor-arg>
            </bean>
        </property>
        -->
    </bean>

    <!--级联属性的配置 -->
    <!-- 作用:为当前bean的Bean类里面创建的另外一个Bean对象通过级联操作赋值 -->
    <bean id="testBean5" class="com.demo.sshtest.TestBean2">
        <constructor-arg name="age" value="88"></constructor-arg>
        <constructor-arg name="str" value="88uuu"></constructor-arg>
        <constructor-arg ref="testBean3"></constructor-arg>
        <!-- 被级联的属性需要先初始化,才能被级联属性赋值,否则会有异常 -->
        <property name="testBean.id" value="66"></property>
        <property name="testBean.name" value="66yy"></property>
        <property name="testBean.age" value="67"></property>

    </bean>

    <!-- 集合属性 -->
    <bean id="testCollect" class="com.demo.sshtest.TestCollection">
        <property name="age" value="333"></property>
        <property name="str" value="33311qqaaass"></property>
        <!-- 使用list或set或map标签来配置集合属性 -->
        <property name="testBeans">
            <list>
                <ref bean="testBean1"></ref>
                <ref bean="testBean2"></ref>
                <ref bean="testBean3"></ref>
                <!-- 也可以直接在集合标签里面定义内部类 -->
                <bean class="com.demo.sshtest.TestBean">
                    <constructor-arg index="0" value="1111"></constructor-arg>
                    <constructor-arg index="1" value="222www"></constructor-arg>
                    <constructor-arg type="int" value="33333"></constructor-arg>
                </bean>
            </list>
        </property>
        <property name="testBeansSet">
            <set>
                <ref bean="testBean1"></ref>
                <ref bean="testBean2"></ref>
                <ref bean="testBean3"></ref>
            </set>
        </property>
        <!-- map类型的配置需要用到entry标签,这个跟list和set不一样 -->
        <property name="testBeansMap">
            <map>
                <entry key="A" value-ref="testBean1"></entry>
                <entry key="B" value-ref="testBean2"></entry>
                <entry key="C" value-ref="testBean3"></entry>
            </map>
        </property>
    </bean>

    <!-- 配置properties属性值,使用props和prop为Properties属性赋值 -->
    <bean id="propertiesSource" class="com.demo.sshtest.TestProperties">
        <property name="properties">
            <props>
                <prop key="userid">userid</prop>
                <prop key="password">password</prop>
                <prop key="filename">"filename"</prop>
                <prop key="filepaswd">filepaswd</prop>
            </props>
        </property>
    </bean>

    <!-- 配置独立的集合,给多个bean使用(引用),即:公用的集合bean -->
    <!-- 要在applicationContext.xml的Namespaces设置里面的XSD加上util -->
    <util:list id="TestCollectionPublic">
        <ref bean="testBean1"></ref>
        <ref bean="testBean2"></ref>
        <ref bean="testBean3"></ref>
    </util:list>
    <bean id="testCollectionPublic" class="com.demo.sshtest.TestCollection">
        <property name="age" value="333"></property>
        <property name="str" value="33311qqaaass"></property>
        <!-- 这里ref直接写公用集合的id -->
        <property name="testBeans" ref="TestCollectionPublic"></property>
    </bean>

    <!-- 通过p命名空间为bean属性赋值,需要先导入p命名空间 -->
    <!-- 要在applicationContext.xml的Namespaces设置里面的XSD加上p,不然会报错说p未绑定 -->
    <bean id="testPnamespaces" class="com.demo.sshtest.TestCollection"
        p:age="456" p:str="qweqwe" p:testBeans-ref="TestCollectionPublic"
    ></bean>

</beans>

19、项目目录
项目目录

20、注释都把需要知道的写得很清楚了,要看注释

21、demo
https://download.csdn.net/download/qq_22778717/10465952

猜你喜欢

转载自blog.csdn.net/qq_22778717/article/details/80616470