目录
创建Maven项目后,在pom.xml中添加Spring所必须的依赖。
- <dependencies>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.2.3.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-beansartifactId>
- <version>5.2.3.RELEASEversion>
- dependency>
- dependencies>
在 resource 文件夹下,创建spring-config.xml配置文件,写入以下内容:
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:content="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 https://www.springframework.org/schema/context/spring-context.xsd">
-
- <content:component-scan base-package="com.beans">content:component-scan>
- beans>
添加注解后,更加简单地实现了将一个对象存储到Spring当中
(默认会按类名自动扫描注入为小驼峰方式的bean name)