Spring入门(配置文件方式)

一 概述

这里写图片描述

二.代码

1.导入jar包
2.src(类路径)下创建配置文件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"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="helloJava" class="com.blueSky.test.HelloJava">
        <property name="content" value="你好,java" />
    </bean>
    <bean id="helloWorld" class="com.blueSky.test.HelloWorld">
        <property name="content" value="你好,世界" />
        <property name="helloJava" ref="helloJava"></property>
    </bean>

</beans>
3.编写java类
package com.blueSky.test;

public class HelloJava {
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


}
package com.blueSky.test;

public class HelloWorld {
    private String content; //输出的内容
    private HelloJava helloJava;

    public HelloJava getHelloJava() {
        return helloJava;
    }

    public void setHelloJava(HelloJava helloJava) {
        this.helloJava = helloJava;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /*
     * 输出content 和 helloJava对象的content
     * */
    public void printContent(){
        System.out.println(content);
        System.out.println(helloJava.getContent());
    }
}
4.applicationContext.xml文件中配置< bean >标签
<?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"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="helloJava" class="com.blueSky.test.HelloJava">
        <property name="content" value="你好,java" />
    </bean>
    <bean id="helloWorld" class="com.blueSky.test.HelloWorld">
        <property name="content" value="你好,世界" />
        <property name="helloJava" ref="helloJava"></property>
    </bean>

</beans>
5.测试
package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.blueSky.test.HelloWorld;

public class SpringTest {

    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
        helloWorld.printContent();
    }
}

这里写图片描述

6.错误分析

这里写图片描述
web.xml配置

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值