创建一个spring项目,从创建一个Java项目开始的整个流程

1.导包

先导入核心包
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
spring运行时还依赖一个日志包;没有就报错
commons-logging-1.1.jar
五个包缺一不可

2.写配置

spring的配置博客文件中,集合了spring的IOC容器管理的所有组件
创建一个Spring Bean Configuration File(Spring的bean配置文件)
参考博客:基础步骤:eclipse新建文件没有Spring Bean Configuration File,下载spring插件(完善版)

整个流程

1.导入jar包
在这里插入图片描述
细节:先拷贝到lib文件夹下,然后点击build path 导入到库里

2.创建bean的对象
在这里插入图片描述
在组件Person中写一些基本信息,如姓名,性别等等
然后生成get和set方法

package com.guigu.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 + "]";
	}
}

3.创建ioc.xml配置文件
在这里插入图片描述
里面的配置,先写bean标签,然后在class后面写person所在的包,对这个bean命名存在id里
在bean标签里配置值,property,name,value

<?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标签可以注册一个组件
		class:需要注册的组件的全类名
		id:这个对象的唯一标识
	 -->
	<bean id="Person01"  class="com.guigu.bean.Person">
	<!-- 使用property标签为Person对象的属性赋值 
		name:指定属性名
		value:为这个属性赋值
	-->
		<property name="lastname" value="张三"></property>
		<property name="age" value="18"></property>
		<property name="gender" value="男"></property>
		<property name="email" value="1095304735@qq.com"></property>
		
	</bean>
	
</beans>

4.创建一个junit.test.case
在这里插入图片描述
在这里插入图片描述
在IOCTest方法里的test函数里,创建一个ioc容器,配置xml的路径
然后使用getbean方法获得由容器帮我们创建的对象,person只是用于接收

	ApplicationContext ioc = new ClassPathXmlApplicationContext("IOC.xml");
		Person bean = (Person) ioc.getBean("Person01");

两句关键代码

package com.guigu.test;

import static org.junit.Assert.*;

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

import com.guigu.bean.Person;

public class IOCTest {
	/*
	 * 从容器中拿到这个组件
	 */
	@Test
	public void test() {
		//ApplicationContext代表IOC容器
		//ClassPathXmlApplicationContext当前应用的xml配置文件在 ClassPathXml路径下
		//根据spring配置文件得到IOC对象
		ApplicationContext ioc = new ClassPathXmlApplicationContext("IOC.xml");
		Person bean = (Person) ioc.getBean("Person01");
		//容器帮我们创建好了对象
		System.out.println(bean);
	}

}


打印运行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你在狗叫什么、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值