Spring编程<二>

Spring编程<二>

用纯JAVA方式演示—-容器与配置文件

导入需要用到的包:
这里写图片描述

写一个配置文件:

//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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 默认是单例-->
    <bean id="p1" class="cn.hncu.domain.Person" >
        <property name="name" value="Tom"></property>
        <property name="age" value="22"></property>
    </bean>
    <!-- prototype原型,每次拿的都是新的 -->
     <bean id="p2" class="cn.hncu.domain.Person" scope="prototype">
        <property name="name" value="Jack"></property>
        <property name="age" value="20"></property>
    </bean>
 </beans>  

演示代码1:

@Test
    public  void demo1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
        Person p1 = context.getBean("p1", Person.class);
        System.out.println(p1);
        Person p2 = context.getBean("p1", Person.class);//参数1为XML的Bean中的ID属性
        System.out.println(p2);
        System.out.println(p1==p2);// Spring默认是单例,所以是true
    }
    @Test
    public  void demo2() {
        ApplicationContext ctx =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Person p1 = ctx.getBean("p2", Person.class);
        Person p2 = ctx.getBean("p2", Person.class);
        System.out.println(p1);
        System.out.println(p1==p2);
    }

演示结果1:
demo1:
这里写图片描述
demo2:
这里写图片描述

演示如何在配置文件中导入其他配置文件

这里写图片描述

<!--demo2.xml -->
        <!-- 属性变量是一个Bean -->
        <!-- ref 依赖注入 -->
        <property name="cat" ref="cat2"></property>
<!-- 导入其他配置文件 -->
    <import resource="cat.xml" />

必须写SET,GET函数,空参构造

//cat.java
    private String name;
    private String hobby;

    public Cat(){}
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    @Override
    public String toString() {
        return "Cat [name=" + name + ", hobby=" + hobby + "]";
    }
<!-- cat.xml -->
    <bean id="cat1" class="cn.hncu.demo2.Cat">
        <property name="name" value="Tom"></property>
        <property name="hobby" value="上天"></property>
    </bean>
    <bean id="cat2" class="cn.hncu.demo2.Cat">
        <property name="name" value="R"></property>
        <property name="hobby" value="唱歌"></property>
    </bean>

Demo2:
不需要在里面New对象Cat

@Test
    public void t1(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/demo2/demo2.xml");
        User u = ctx.getBean("user", User.class);
        System.out.println(u);
    }

演示结果:
这里写图片描述

配置文件中复杂属性变量:List等。

<!-- 属性变量是一个list,map,set -->
        <property name="addrs">
            <list>
                <value>益阳</value>
                <value>长沙</value>
                <value>湘潭</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="name" value="pwd" />
                <entry key="age" value="20" />
            </map>
        </property>
        <property name="set">
            <set>
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </set>
        </property>
        <!-- 属性值是一个Object[]的数组,集合嵌套 -->
        <property name="objs">
            <array>
                <value>一个字符串</value>
                <ref bean="cat1" />
                <!-- 内部bean (匿名) -->
                <bean class="cn.hncu.demo2.Cat">
                    <property name="name" value="恶汉猫"></property>
                    <property name="hobby" value="PK"></property>
                </bean>

            </array>
        </property>

模拟MVC框架DAO与Service注入:

这里写图片描述

以前注入都是New一个,现在全部写NULL,并写好SET,GET函数
这里写图片描述

配置文件:

 <!-- 全部用关系ref,连起来 -->
    <bean id="mysqlDao" class="cn.hncu.demo3.dao.StudDaoJdbc" >
    </bean>
    <bean id="oracleDao" class="cn.hncu.demo3.dao.StudDaoOracle" >
    </bean>
    <!-- 依赖注入 -->
    <bean id="service" class="cn.hncu.demo3.service.StudServiceImpl">
    <property name="dao" ref="mysqlDao"></property>
    </bean>
    <bean id="action" class="cn.hncu.demo3.action.StudAction">
    <property name="service" ref="service"></property>
    </bean>

Spring下载地址:

http://repo.springsource.org/libs-release-local/org/springframework/spring/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值