java-IDEA中搭建spring框架

首先新建一個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管理
在這裏插入圖片描述
控制檯上成功輸出,表示被管理
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章