1下载spring入门jar包(初学者够用)并导入对应项目WEB-INF/lib中
自制了百度云链接:https://pan.baidu.com/s/11XqWsZlhhXAVbXsbuR11jQ 密码:gpb4
2创建以下文件用于测试项目
代码:
//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创建其实例 -->
<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运行