栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实验一 Spring IOC 基本操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实验一 Spring IOC 基本操作

实验一 Spring IOC 基本操作 实验任务一:创建项目,引入坐标

坐标代码

  
    
      junit
      junit
      4.11
      test
    
    
      org.springframework
      spring-context
      4.3.6.RELEASE
    
    
    org.springframework
    spring-beans
    4.3.6.RELEASE
  
    
      org.springframework
      spring-core
      4.3.6.RELEASE
    
    
      org.springframework
      spring-expression
      4.3.6.RELEASE
    
    
      commons-logging
      commons-logging
      1.2
    
  
实验任务二:配置Spring框架配置文件

applicationContext.xml




    
    


实验任务三:创建UserDao接口
package com.axx.ioc;

public interface UserDao {
    public void say();
}

实验任务四:创建UserDao接口实现类UserDaoImpl
package com.axx.ioc;

public class UserDaoImpl implements UserDao{
    @Override
    public void say() {
        System.out.println("userDao say hello World!");
    }
}

实验任务五:创建IOC测试类
package com.axx.ioc;

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

public class TestIoC {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        UserDao userDao = (UserDao)applicationContext.getBean("userDao");
        userDao.say();
    }

}

运行如图

实验任务六 :创建UserService接口
package com.axx.io;

public interface UserService {
    public void say();
}

实验任务七:创建UserService接口实现类UserServicempl
package com.axx.io;

import com.axx.ioc.UserDao;

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    public void setUserDao(UserDao userDao){
        this.userDao=userDao;
    }

    @Override

    public void say() {
        this.userDao.say();
        System.out.println("userService say hello World!");
    }
}

实验任务八:更改Spring配置文件



    
    
        
    


实验任务八:创建DI测试类
package com.axx.ioc;

import com.axx.io.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDI {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        UserService userService = (UserService)applicationContext.getBean("userService");
        userService.say();
    }
}

运行如图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756014.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号