Spring(注解方式)简单入门
环境准备
- 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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.cyb.service"></context:component-scan>
</beans>
UserService.java
package com.cyb.service;
public interface UserService {
void speak();
}
UserServiceImpl.java
其他注解:@Component、@Controller、@Service、@Repository
package com.cyb.service; import org.springframework.stereotype.Service; @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();
}
}
项目结构

测试

Spring(注解方式)简单入门的更多相关文章
- spring注解方式在一个普通的java类里面注入dao
spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...
- (转)使用Spring注解方式管理事务与传播行为详解
http://blog.csdn.net/yerenyuan_pku/article/details/52885041 使用Spring注解方式管理事务 前面讲解了怎么使用@Transactional ...
- Spring 注解方式引入配置文件
配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件: 一.引入Spring XML的注解是@ImportResource @Retention(R ...
- Spring MVC之简单入门
一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...
- Spring 注解方式 实现 IOC 和 DI
注:以下所有测试案例(最后一个除外)的测试代码都是同一个: package cn.tedu.test; import org.junit.Test; import org.springframewor ...
- Spring注解方式配置说明
1.<context:annotation-config/>与<context:component-scan base-package=”XX.XX”/> 在基于主机方式配置S ...
- ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听
对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...
- spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法
做单元测试的时候,抛出异常 Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' ...
- 基于注解的Spring MVC的简单入门——简略版
网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...
- ehcache整合spring注解方式
一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件 ...
随机推荐
- MySQL面试必备二之binlog日志
本文首发于公众号:Hunter后端 原文链接:MySQL面试必备二之binlog日志 关于 binlog,常被问到几个面试问题如下: binlog 是什么 binlog 都记录什么数据 binlog ...
- Chrome 浏览器插件 Manifest.json V3 中权限(Permissions)字段解析
一.权限(Permissions) 再使用拓展程序的 API 时,大多数的时候,需要在 manifest.json 文件中声明 permissions 字段. 一.权限类型 在 V3 版本中可以声明以 ...
- python基础环境
刚开始接触并学习一门开发语言,带着不求甚解的想法,其实也挺有好处的:我并不是所有的东西都知道,但是代码跑起来了. 但是时间久了,还是带着这种想法,可能就会遇到一些棘手的问题.比如电脑上不知不觉已经安装 ...
- 《最新出炉》系列入门篇-Python+Playwright自动化测试-47-自动滚动到元素出现的位置
1.简介 在我们日常工作中或者生活中,经常会遇到我们的页面内容较多,一个屏幕范围无法完整展示内容,我们就需要滚动滚动条去到我们想要的地方,如下图页面,我们虽然在豆瓣首页,但是内容并不完整,如果我们想要 ...
- wpf 空间坐标系下,画一个立方体轮廓
代码: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Worignpt ...
- SpringMVC在处理Ajax请求后返回void导致前台Ajax回调函数不执行
问题简述 在一次SpringMVC项目中,前台通过Ajax请求,准备修改数据库中的数据.因为Ajax请求只是让后台更改数据,所以Controller方法根本不需要返回给前台任何数据,所以我们将Cont ...
- Linux之top命令分析
第一行: top - 04:25:26 当前系统时间 up 3 min, 系统已经运行的时间(不间歇的运行) 1 user, 当前登录系统的用户数 load average: 0.01, 0.03, ...
- Android 12(S) ALooper AHandler AMessage(二)
来写个demo试试看到底是ALooper AHandler AMessage是怎么运行的,源文件以及Android.bp如下: // EvenHandler.h #ifndef __MESSAGE_T ...
- LLM 大模型学习必知必会系列(三):LLM和多模态模型高效推理实践
LLM 大模型学习必知必会系列(三):LLM和多模态模型高效推理实践 1.多模态大模型推理 LLM 的推理流程: 多模态的 LLM 的原理: 代码演示:使用 ModelScope NoteBook 完 ...
- vue3 KeepAlive
在Vue.js 3中,<keep-alive> 是一个抽象组件,用于保留其子组件状态,防止在切换组件时销毁它们.这对于在页面间切换时保留组件的状态或避免重复渲染特定组件非常有用.<k ...