[JAVAEE]實驗01:Spring初步認識之開發(fā)環(huán)境的搭建

1下載spring入門jar包(初學(xué)者夠用)并導(dǎo)入對應(yīng)項目WEB-INF/lib中
自制了百度云鏈接:https://pan.baidu.com/s/11XqWsZlhhXAVbXsbuR11jQ 密碼:gpb4

2創(chuàng)建以下文件用于測試項目


文件結(jié)構(gòu)

代碼:

//Test.java
package dao;
public interface TestDao {
    public void sayHello();
}
//TsetDaoImpl.java
package dao;
public class TestDaoImpl implements TestDao{
    
    public void sayHello() {
        System.out.println("hello,topus");
        
    }
}
<!-- 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">

    <!-- 將指定的TestDaoImpl配置給Spring,讓Spring創(chuàng)建其實例 -->
    <bean id="test" class="dao.TestDaoImpl">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
</beans>

這一段來自官方參考文檔:
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-metadata

//Test.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.TestDao;
public class Test {
    public static void main(String[] args){
        //初始化Spring容器ApplicationContext,加載配置文件
        ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDao tt=(TestDao)appCon.getBean("test");//Test 為配置文件中的id
        tt.sayHello();
    }
}

3運行


結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容