【Spring】Spring的第一个程序HelloWord

29 篇文章 0 订阅
25 篇文章 0 订阅

总述:

  1. 导包
  2. 配置
  3. 测试

(一)导包:相当于创建一个容器所需要的包
在这里插入图片描述
注意:一定要配置路径

(二)配置

  1. 创建一个组件(类):
    在这里插入图片描述
    代码:
package com.atguigu.bean;

public class Person {
	private String lastName;
	private Integer age;
	private String gender;
	private String email;
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "Person [lastName=" + lastName + ", age=" + age + ", gender=" + gender + ", email=" + email + "]";
	}
	
	
	
	
}

2.将组件(类)配置到容器中(ioc在scr目录下)
在这里插入图片描述
代码:

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

***//一个bean注册一个组件***
	<bean id="person01" class="com.atguigu.bean.Person">
		<property name="age" value="18"></property>	
		<property name="email" value="com.atguigu@qq.com"></property>
		<property name="gender" value="男"></property>
		<property name="lastName" value="张三"></property>				
	</bean>

</beans>

(三)测试

  1. 创建一个测试类
    在这里插入图片描述
    代码:
package com.atguigu.test;

import static org.junit.Assert.*;

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

import com.atguigu.bean.Person;

public class iocTest {

	@Test
	public void test() {
	//新建一个容器
		ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
		Person bean = (Person) ioc.getBean("person01");
		System.out.println(bean);
	}

}

结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值