超简单的Spring入门案例制作,快来看看吧!
2020-07-16 10:21
197 查看
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名字,这个名字可以自己随便取,但一般都写接口名”)
相关文章推荐
- spring 入门实战(二)一个简单的使用案例
- Spring扫盲和简单入门案例
- 用C++制作一个简单的文字游戏,纯属C++入门,有兴趣的可以看看
- ActiveMQ的入门案例以及整合Spring的简单使用
- SpringBoot整合SSM简单案例
- Spring简单入门
- Springboot案例入门
- springboot rabbitmq 入门案例
- Spring框架汇总(Spring AOP——基于XML的简单案例)
- ASP.NET Spring.Net 入门2 简单入门示例--属性与构造函数注入
- Java Socket 多线程简单入门案例
- Maven+SpringMVC+Dubbo 简单的入门demo配置
- Spring 基于XML配置的IOC入门案例
- 一、SpringIOC入门案例(程序间的耦合)
- spring 结合 Redis 例子,简单入门例子
- SpringBoot入门系列:eclipse如何构建一个最简单的springBoot项目。
- Dubbo 简单的入门【Maven+SpringMVC+Dubbo 】
- spring整合cxf入门案例
- 如何简单的构建一个Notification(入门的可以看看!!)
- springboot+thymeleaf入门案例-模拟豆瓣网