首先新建一個maven項目
然後再pom.xml中導入spring所需要的依賴
<dependencies>
<!--注入spring所需要的依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
隨便寫一個想要注入的類
把這個類交給spring.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時,com.service.HelloWord表示在哪個包的哪個類
而bean的id="hello"表示HelloWord中的方法hello
-->
<bean id="hello" class="com.service.HelloWord">
</bean>
</beans>
用此時類來檢驗自己寫的類是否交給了spring管理
控制檯上成功輸出,表示被管理