我的第一个Spring程序(一)

初学者的第一个Spring程序:由于初学所以没有具体分层,都在一个包下。

springframework 的四个jar包下下载,
在这里插入图片描述
在这里插入图片描述

logging.jar 下载
在这里插入图片描述

1.创建项目

在 Eclipse 中创建 Web 项目 bin_spring01,将 Spring 框架所需的 JAR 包复制到项目的 lib 目录中,并将添加到类路径下,如图:

在这里插入图片描述
在这里插入图片描述
2.创建PersonDao接口

package com.bin.ioc;

public interface PersonDao {
	public void add();

}

3. 创建接口实现类 PersonDaoImpl

package com.bin.ioc;

public class PersonDaoImpl implements PersonDao{

	@Override
	public void add() {
		// TODO Auto-generated method stub
		System.out.println("save()执行完成。。。。。");
	}

}

4. 创建 Spring 配置文件

<?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-3.2.xsd">
    <!-- 由 Spring容器创建该类的实例对象 -->
    <bean id="personDao" class="com.bin.ioc.PersonDaoImpl" />
    
    <bean id="personService" class="com.bin.ioc.PersonServiceImpl">
    <!-- 将personDao 实例注入personService实例中 -->
    <property name="personDao" ref="personDao"/>
    </bean>
</beans>

5. 编写测试类

package com.bin.ioc;

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

import com.sun.org.apache.bcel.internal.util.ClassPath;

public class FirstTest {
	@Test
	public void test1() {
		//定义Spring配置文件的路径
		String xmlPath="applicationContext.xml";
		//初始化Spring容器,加载配置文件
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
		//通过容器获取personDao实例
		PersonDao personDao=(PersonDao) applicationContext.getBean("personDao");
		//调用personDap的add()方法
		personDao.add();
		
	}
	}

运行测试:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值