超简单的Spring入门案例制作,快来看看吧!

本文档提供了一个超简单的Spring入门案例,包括构建接口、实现类、测试方法的步骤,以及如何导入spring-context坐标,创建并加载applicationContext.xml配置文件。
摘要由CSDN通过智能技术生成

Spring入门案例制作

1.构建方法:接口,实现类,测试方法
接口:
package com.itheima.service;

public interface UserService {
    public void save();
}
实现类:
package com.itheima.service.imple;

import com.itheima.service.UserService;

public class UserServiceImpl implements UserService { 
    public void save() {        
         System.out.println("hello");    
    }
}
测试方法:
import com.itheima.service.UserService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserApp { 
    public static <applicationConmtext> void main(String[] args) { 
        //加载配置文件       
   ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");       
       //获取资源      
   UserService userService = (UserService) ctx.getBean("UserService");  
        userService.save();   
    }
}
创建路径:
2.导入坐标:spring-context
<dependencies>    
   <dependency>        
       
       <groupId>org.springframework</groupId>        
       <artifactId>spring-context</artifactId>        
   	   <version>5.1.9.RELEASE</version>    
   
    </dependency>
</dependencies>
3.创建配置文件 applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"       
xsi:schemaLocation="http://www.springframework.org/schema/beans        
https://www.springframework.org/schema/beans/spring-beans.xsd                
http://www.springframework.org/schema/context        
https://www.springframework.org/schema/context/spring-context.xsd">   

<!--创建Spring控制的资源-->    
  <bean id="duj" class="com.itheima.service.imple.UserServiceImpl"/>  
    
</beans>

在这里插入图片描述

4.加载配置文件:

ApplicationContext ctx = new ClassPathXmlApplicationContext(“配置文件名”)

获取资源: ctx.getBean(“里边填写的是配置文件里边的id名字,这个名字可以自己随便取,但一般都写接口名”)

ntext(“配置文件名”)

获取资源: ctx.getBean(“里边填写的是配置文件里边的id名字,这个名字可以自己随便取,但一般都写接口名”)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值