相关视频下载地址:链接:http://pan.baidu.com/s/1sjJTFyP 密码:sl81
Java爱好者交流群: 369508920 免费获取项目资源
class="java"><?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-3.2.xsd">
<!-- Spring IOC 依赖注入, 它就像一个大型的工厂,可以配置任何的bean,不仅可以解决对象的创建问题,而且解决创建数量问题 -->
<!-- xml文件的功能,类似前面的properties文件 -->
<bean id="bus" class="cn.it.pattern.demo06.Bus" />
<bean id="car" class="cn.it.pattern.demo06.Car" lazy-init="false" scope="prototype" />
</beans>
public class PatternDemo {
/*
* 通过Spring的IOC来实现对象的创建,而且可以解决对象创建的数量问题
*
* lazy-init="false" : 立即创建, lazy-init="true" 用的时候才创建, 但是仅仅创建一次
* scope="prototype": 多例模式,每次创建都是一个新的对象, 注意:多例模式永远都是用的时候才创建
*/
public static void main(String[] args) {
// 1: 加载spring配置文件,默认是在scr跟目录, 默认加载配置文件就会创建对象,而且是单例模式
// 2: 创建的bean对象,存放到了spring的缓存中, 以后获取对象先从缓存中查找,如果能找到则不会创建新对象
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
System.out.println("-----配置文件加载完毕-------");
System.out.println(context.getBean("car"));
System.out.println(context.getBean("car"));
}
}
?