要想看懂 spring 的配置文件:ApplicationContext.xml 第一步

要想看懂 spring 的配置文件:ApplicationContext.xml 就得先从简单的来,一步一步的看才会理解的清楚明白。
首先得知道什么叫ioc---依赖注入,注入的方式一般有2个;设值注入和构造注入。可参考如下链接:

详详细细,明明白白的讲解什么叫依赖注入,什么叫IOC

然后就知道这个配置文件是干嘛的,具体怎么配置的。
还是得多看,看多了,就知道啦,具体怎么弄啦。

对于spring的配置文件里面bean的配置,

当每个bean的很多属性对应的不同类型的时候,怎么在配置文件里面配置。

当然这是简单的配置,就是展示一下用法。

做个如下测试,当个例子。

首先是Java代码,主要是要注入到spring容器的Java bean ,和测试代码。

package com.x.x.model.configcenter;

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

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

class Student {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void say() {
        System.out.println("hello");
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Person {
    //第一类:基本数据类型(虽String不是基本数据类型,也归为此类)
    private Long pid;
    private String pName;
    private String ss;

    //第二类:引用
    private Student student;

    //第三类:集合
    private List lists;
    private Set sets;
    private Map maps;
    private Properties properties;

    /**
     * 因为下面重写的构造方法,所以默认构造方法就不存在啦
     * 所以要显示的再次声明无参构造方法
     * 为了在设置注入时候调用:设置注入就是调用默认构造方法来注入的。
     */
    public Person() {
    }

    public Person(String pName, String ss, Student student) {
        this.pName = pName;
        this.student = student;
        this.ss = ss;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Set getSets() {
        return sets;
    }

    public void setSets(Set sets) {
        this.sets = sets;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public Properties getProperties() {
        return properties;
    }

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

    public String getSs() {
        return ss;
    }

    public void setSs(String ss) {
        this.ss = ss;
    }

    @Override
    public String toString() {
        return "Person{" +
                "pid=" + pid +
                ", pName='" + pName + '\'' +
                ", ss='" + ss + '\'' +
                ", student=" + student +
                ", lists=" + lists +
                ", sets=" + sets +
                ", maps=" + maps +
                ", properties=" + properties +
                '}';
    }
}

/**
 * Created by lxk on 2016/9/17
 */
class ApplicationContextText {
    public static void main(String[] args) {
        ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContextText.xml");
        Person p = (Person) ctx.getBean("person");
        System.out.println(p.toString());
    }
}


下面的是配置文件:applicationContextText.xml,演示在Javabean里面的不同类型怎么在配置文件里面配置。如下:

<?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">

    <!-- 把person和student纳入spring容器中-->
    <bean id="person" class="com.x.x.model.configcenter.Person">
        <!--
            property代表Person类的一个属性
               name 为属性的名称
               value 属性的值
               String类型和基本类型(基本类型)是用value来进行赋值的
         -->
        <property name="pid" value="1181415316"/>
        <property name="pName" value="大师兄当回老师怎么样"/>
        <property name="ss" value="习惯就好,误人子弟."/>
        <!-- <ref bean="student"/> 等价于ref="student" 推荐下面写法-->
        <property name="student" ref="student"/>
        <property name="lists">
            <list>
                <value>海贼王</value>
                <value>写bug</value>
                <ref bean="student"/>
            </list>
        </property>

        <property name="sets">
            <set>
                <value>火影</value>
                <value>bug king</value>
                <ref bean="student"/>
            </set>
        </property>
        <property name="maps">
            <map>
                <entry key="entry1" value="aaa"/>
                <entry key="entry2" value="bbb"/>
                <entry key="entry3" value-ref="student"/>
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="prop1">prop1</prop>
                <prop key="prop2">prop2</prop>
            </props>
        </property>
    </bean>

    <bean id="student" class="com.x.x.model.configcenter.Student">
        <property name="name" value="我是小明,老师,老规矩,我懂的,!"/>
    </bean>
</beans>


下面上测试结果:如下图。

具体数据格式,如下:(给调成json格式了,看着好看些。)

Person{
    pid=1181415316,
    pName='大师兄当回老师怎么样',
    ss='习惯就好,误人子弟.',
    student=Student{
        name='我是小明,老师,老规矩,我懂的,!'
    },
    lists=[
        海贼王,
        写bug,
        Student{
            name='我是小明,老师,老规矩,我懂的,!'
        }
    ],
    sets=[
        火影,
        bugking,
        Student{
            name='我是小明,老师,老规矩,我懂的,!'
        }
    ],
    maps={
        entry1=aaa,
        entry2=bbb,
        entry3=Student{
            name='我是小明,老师,老规矩,我懂的,!'
        }
    },
    properties={
        prop2=prop2,
        prop1=prop1
    }
}

知道这个怎么配置的就当复习啦吧,当然要是有部对的呢,也可以指出来嘛,毕竟大师兄还在个新手。
要是不知道的呢,你看了这个简单的例子,那么在看自己公司项目的applicationContext.xml的时候,
当然这个估计就是 公司项目的真正的配置文件啦,就可以依葫芦画瓢,看懂了吧。举一反三还是可以的吧。

IntelliJ IDEA 中如何构建spring测试环境,请参考如下链接:

IntelliJ IDEA 中如何构建spring测试环境

  • 32
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值