Spring-IOC入门案例

推荐先阅读Spring全家桶

public interface BookDao {}
public class BookDaoImpl implements BookDao {}
public interface BookService {}
public class BookServiceImpl implements BookService {}
<?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标签标示配置bean
id属性标示给bean起名字
class属性表示给bean定义类型
-->
<bean id="bookDao" class="com.zx.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.zx.service.impl.BookServiceImpl"/>
</beans>
public class App {
public static void main(String[] args) {
// 获取IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取实例对象
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
BookService bookService = (BookService) ctx.getBean("bookService");
}
}