Spring(XML方式)简单入门
环境准备
- maven
- jdk
- Spring
- Eclipse
项目创建




pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cyb</groupId>
<artifactId>spring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- spring 核心组件中的4个依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.7.RELEASE</version>
</dependency> <!-- 单元测试Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<finalName>spring</finalName>
<plugins>
<!-- 配置Maven的JDK编译级别 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="UserService" class="com.cyb.service.UserServiceImpl"></bean>
</beans>
UserService.java
package com.cyb.service;
public interface UserService {
void speak();
}
UserServiceImpl.java
package com.cyb.service;
public class UserServiceImpl implements UserService {
@Override
public void speak() {
System.out.println("陈彦斌博客:https://www.cnblogs.com/chenyanbin/");
}
}
TestUserService.java
package com.cyb.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cyb.service.UserService; public class TestUserService {
@Test
public void TestIoc() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 方式一
UserService service1 = context.getBean(UserService.class);
service1.speak();
// 方式二
UserService service2 = (UserService)context.getBean("UserService");
service2.speak();
}
}
项目结构

测试

Spring(XML方式)简单入门的更多相关文章
- Spring MVC之简单入门
一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...
- 基于注解的Spring MVC的简单入门——简略版
网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...
- Spring Cloud Feign 简单入门
Feign是一个生命是的web service 客户端,使用方式非常简单:接口+注解,spring cloud feign 对feign惊醒了增强使它支持了spring mcv注解. 示例(以下示例结 ...
- Spring Cloud Eureka简单入门
步骤: 1.创建父工程 2.创建EurekaServer工程 3.创建EurekaClient工程 父工程pom.xml <?xml version="1.0" encodi ...
- Spring Cloud Gateway简单入门,强大的微服务网关
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 见名知义,Spring Cloud Gateway是用于微服务场景的网关组件,它是基于Spring WebFlu ...
- XML第一次简单入门(Lab分析)
In this tutorial you will create a well-formed and verified XML file. Consider the XML document belo ...
- 6.Spring【AOP】XML方式
1.AOP术语 1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点):所谓切 ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- 【java开发系列】—— spring简单入门示例
1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...
- Spring Boot【快速入门】简单案例
Spring Boot[快速入门] Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point ...
随机推荐
- 当你用neovim的mason插件管理lsp config,并且配置好bash的bashls后,却没有正常工作的解决方式
刚开始遇到这个情况我百思不得其解,检查了neovim checkhealth,以为是npm包管理的问题,然后删了下删了下 不但没有解决还把包管理整乱了-- 后来发现是我没仔细看bash-languag ...
- TCP协议分析工具TcpEngine V1.2.0使用教程
概述 目前主流的网络数据分析工具主要有两类,一类是http协议分析工具,如fiddler,这类工具擅长对字符串类型协议分析:另一类是原始网络数据包的监听分析,如Wireshark,这类工具擅长分析网络 ...
- 抽丝剥茧:详述一次DevServer Proxy配置无效问题的细致排查过程
事情的起因是这样的,在一个已上线的项目中,其中一个包含登录和获取菜单的接口因响应时间较长,后端让我尝试未经服务转发的另一域名下的新接口,旧接口允许跨域请求,但新接口不允许本地访问(只允许发布测试/生产 ...
- 前瞻 PHP8.4 的新特性
前瞻 PHP8.4 的新特性 PHP 8.4 将于 2024 年 11 月 21 日发布.它将包括属性钩子.JIT 改进,以及在不需要额外括号的情况下链式调用方法.这是一个大变化! 属性钩子 RFC ...
- 基于 ESP8266_RTOS_SDK 驱动 HC-SR04
平台 芯片 ESP8266EX 模组 ESP-12F 开发板 NodeMCU SDK ESP8266_RTOS_SDK branch master commit 83517ba1f5e26b9413f ...
- 【题解】A19337.火星背包
\(\bf{用 CDQ 分治可以极大地提升程序运行的速度.}\) \(\bf{实测在本数据量下,可以在 \color{red}10ms\color{normal}} 内通过所有的测试点!\) 关于折半 ...
- CSS——引入方式
1.行内式 <div style="color: white;background-color: #369;text-align: center">行内设置</d ...
- Django——Media配置
settings.py中的配置: MEDIA_ROOT 和 MEDIA_URL MEDIA_ROOT 是指:用户上传文件的根路径 --> MEDIA_ROOT = os.path.join(BA ...
- redis 使用lua脚本 一次性获取多个hash key 字段值
客户端命令行代码: eval "local rst={};local field='schoolid'; for i,v in pairs(KEYS) do rst[i]=redis.cal ...
- java stream 简单函数
写在前面 本文为笔者学习的一些心得,如有问题,评论请轻喷 本文分为以下部分: 中间操作 终止操作 归纳 中间操作 对 list 进行操作,返回一个新的 list 主要函数 作用 filter 过滤操作 ...