Spring:入门程序编写

本文介绍了如何在IDEA中创建一个Maven工程,并添加Spring的核心库依赖。接着,创建了`Student`和`Teacher`两个Java类,并在`applicationContext.xml`配置文件中定义了它们,设置了成员变量。最后,编写了测试类`TestSpring`来加载配置并实例化Bean,调用其方法验证配置是否成功。
摘要由CSDN通过智能技术生成

1 创建maven工程

在IDEA创建Maven工程

在pom.xml文件中添加Spring核心容器的jar包和Spring依赖jar包

在这里插入图片描述

pom.xml添加的依赖如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>chapter06</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>


</project>

2 创建Bean

Student.java

代码如下:

package com.qfedu.bean;

public class Student {
    public Student() {
        super();
        System.out.println("Student对象创建了");
    }

//    创建一个成员变量,给出get/set方法
    private String msg = null;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
//    在study()方法中调用成员变量
    public void study(){
        System.out.println("学生在学习"+msg);
    }
}

Teacher.java

代码如下:

package com.qfedu.bean;

public class Teacher {
    public Teacher() {
        super();
        System.out.println("Teacher对象创建了");
    }
    private String msg = null;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void teach(){
        System.out.println("老师讲解"+msg+"知识");
    }
}

3 创建配置文件

配置文件applicationContext.xml需要放置在resources文件夹下,在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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 将指定的类配置给Spring -->
	<bean id="student" class="com.qfedu.bean.Student">
		<property name="msg">
			<value>Java</value>
		</property>
	</bean>
	<bean id="teacher" class="com.qfedu.bean.Teacher" lazy-init="true">
		<property name="msg">
			<value>Spring</value>
		</property>
	</bean>


</beans>

4 编写测试类

TestSpring.java

代码如下:

package com.qfedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qfedu.bean.Student;
import com.qfedu.bean.Teacher;
public class TestSpring {
	public static void main(String[] args) throws Exception  {
		//通过读取配置信息获取ApplicationContext对象
		ApplicationContext  applicationContext = new 
         	    ClassPathXmlApplicationContext("applicationContext.xml");
		//通过id值获取Student对象
		Student student = applicationContext.getBean("student",Student.class);
		//调用Student对象的方法
		student.study();
		//通过id值获取Teacher对象
		Teacher teacher = applicationContext.getBean("teacher",Teacher.class);
		//调用Teacher对象的方法
		teacher.teach();

	}
}

运行结果如下图:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值