1.入门程序
1.基础包
下载地址:
https://repo.spring.io/libs-release-local/org/springframework/spring/4.3.6.RELEASE/
http://commons.apache.org/proper/commons-logging/download_logging.cgi
2.获取配置文件的约束信息:
位置:
3.约束信息+版本信息号(版本信息号就是下图中xsd但是要把它改成对应的版本即可不用在复制额外的行)
使用浏览器打开
package com.itheima.ioc;
public interface UserDao {
public void say();
}
package com.itheima.ioc;
public class UserDaoImpl implements UserDao {
@Override
public void say() {
// TODO Auto-generated method stub
System.out.println("userDao say hello World!");
}
}
<?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-4.3.xsd">
<!-- 将指定类配置给Spring,让Spring创建其对象的实例 -->
<bean id="userDao" class="com.itheima.ioc.UserDaoImpl"/>
</beans>
package com.itheima.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestIoC {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.初始化spring容器,加载配置文件
ApplicationContext applicationContext=
new ClassPathXmlApplicationContext("applicationContext.xml");
//2.通过容器获取userDao实例
UserDao userDao=(UserDao)applicationContext.getBean("userDao");
//3.调用实例中的say()方法
userDao.say();
}
}
运行结果: