使用xml方式实现Spring IOC

1、创建IStudentDao接口,并定义save方法,add方法,update方法;

1. package com.aiit.dao;
2.
3.public interface IStudentDao {
4.    void save();
5.
6.    void add();
7.
8.    void update();
9.}

2、创建IStudentDao对应的实现类StudentDaoImpl,重写里面所有方法;

1.package com.aiit.dao.impl;
2.
3.import com.aiit.dao.IStudentDao;
4.
5.public class StudentDaoImpl implements IStudentDao {
6.    @Override
7.    public void save() {
8.        System.out.println("这是StudentDaoImpl中的save方法!");
9.    }
10.
11.    @Override
12.    public void add() {
13.        System.out.println("这是StudentDaoImpl中的add方法!");
14.    }
15.
16.    @Override
17.    public void update() {
18.        System.out.println("这是StudentDaoImpl中的update方法!");
19.    }
20.}

3、创建IStudentService接口,并定义save方法,add方法,update方法;

1.package com.aiit.service;
2.
3.public interface IStudentService {
4.
5.    void save();
6.
7.    void add();
8.
9.    void update();
10.
11.}

4、创建IStudentService对应的实现类StudentServiceImpl,重写里面所有方法;

1.package com.aiit.service.impl;
2.
3.import com.aiit.dao.IStudentDao;
4.import com.aiit.service.IStudentService;
5.
6.public class StudentServiceImpl implements IStudentService {
7.
8.    private IStudentDao studentDao;
9.
10.    public IStudentDao getStudentDao() {
11.        return studentDao;
12.    }
13.
14.    public void setStudentDao(IStudentDao studentDao) {
15.        this.studentDao = studentDao;
16.    }
17.
18.    @Override
19.    public void save() {
20.        studentDao.save();
21.        System.out.println("这是StudentServiceImpl中的save方法!");
22.    }
23.
24.    @Override
25.    public void add() {
26.        studentDao.add();
27.        System.out.println("这是StudentServiceImpl中的add方法!");
28.    }
29.
30.    @Override
31.    public void update() {
32.        studentDao.update();
33.        System.out.println("这是StudentServiceImpl中的update方法!");
34.    }
35.
36.
37.
38.}

5、注入StudentDao对象(通过set方法注入);


1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans xmlns="http://www.springframework.org/schema/beans"
3.       xmlns:context="http://www.springframework.org/schema/context"
4.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5.       xsi:schemaLocation="http://www.springframework.org/schema/beans
6.        http://www.springframework.org/schema/beans/spring-beans.xsd
7.        http://www.springframework.org/schema/context
8.        http://www.springframework.org/schema/context/spring-context.xsd">
9.
10.    <bean id="studentDao" class="com.aiit.dao.impl.StudentDaoImpl"/>
11.
12.    <bean id="studentService" class="com.aiit.service.impl.StudentServiceImpl" >
13.        <property name="studentDao" ref="studentDao"/>
14.    </bean>
15.
16.
17.</beans>

6、创建测试类,调用StudentServiceImpl里面的方法;

1.package com.aiit;
2.
3.import com.aiit.service.IStudentService;
4.import org.junit.Test;
5.import org.springframework.context.ApplicationContext;
6.import org.springframework.context.support.ClassPathXmlApplicationContext;
7.
8.public class MyTest {
9.
10.    @Test
11.    public void test1(){
12.        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
13.        IStudentService studentService = (IStudentService) context.getBean("studentService");
14.        studentService.save();
15.        studentService.add();
16.        studentService.update();
17.    }
18.
19.}

结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值