标签:getbean actor ret ring commons pac epo 工厂 3.2
开发一个简单的Spring项目:
一、开发环境搭建:
1、引入源码jar :3.0以前的版本含有依赖jar包,3.0以后只有spring的核心jar
包
core的jar包
commons-logging-1.1.3.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
jar包可以在官网下载:http://repo.spring.io/release/org/springframework/spring/
2、创建一个xml文档,命名为applicationContext.xml
导入必须的schema
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd"> 10 11 </beans>
这些都需要,就不必删减了。留着吧。
3、创建例子
user
1 package com.liuyang.spring; 2 3 public class User { 4 String _id; 5 String _username; 6 7 public User() { 8 System.out.println("========创建类========"); 9 } 10 11 public String get_id() { 12 return _id; 13 } 14 15 public void set_id(String _id) { 16 this._id = _id; 17 } 18 19 public String get_username() { 20 return _username; 21 } 22 23 public void set_username(String _username) { 24 this._username = _username; 25 } 26 }
4、创建xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd"> 10 <bean id="user" class="com.liuyang.spring.User"></bean> 11 12 13 14 </beans>
5、创建测试类
1 @SuppressWarnings({ "resource", "deprecation" }) 2 public class TestApplication { 3 4 // 通过工厂类创建IOC工程类对象 5 @Test 6 public void test1() { 7 // 把对象创建交给IOC 8 Resource resource = new ClassPathResource("com/liuyang/spring/applicationContext.xml"); 9 // 创建器对象 10 BeanFactory beanFactory = new XmlBeanFactory(resource); 11 // 的到容器对象 IOC=工厂类+ApplicationContext.xml 12 User user = (User) beanFactory.getBean("user"); 13 14 System.out.println(user); 15 } 16 17 // 2.直接获取ApplicationContext.xml()方便 18 @Test 19 public void test2() { 20 21 ApplicationContext aContext = new ClassPathXmlApplicationContext( 22 "com/liuyang/spring/applicationContext.xml"); 23 24 // 的到容器对象 IOC=工厂类+ApplicationContext.xml 25 User user = (User) aContext.getBean("user"); 26 27 System.out.println(user); 28 } 29 30 @Test 31 public void test3() { 32 /* 33 * (1)修改 34 * <bean id="user" class="com.liuyang.spring.User" scope="singleton"></bean> 35 * // scope="singleton" 加不加无影响,默认是单例模式 36 * (2)[service/dao/工具类] 只需创建一次 scope="prototype" 多例 37 * [action对象]选有多次创建对象时用 用到对象时创建 启动容器之前,整个应用只有一个 38 * (3)是否延迟创建 39 * scope="singleton" 才有效 40 * (4)懒加载问题 lazy-init="false" 默认为false, 41 * 不延迟创建, 42 * 即在启动时候就创建对象 lazy-init="true" 延迟初始化, 在用到对象的时候才创建对象 43 * 44 * 45 * (5) lazy-init="true"+scope="singleton" == scope="prototype" 46 */ 47 ApplicationContext aContext = new ClassPathXmlApplicationContext( 48 "com/liuyang/spring/applicationContext.xml"); 49 System.out.println("========创建容器========"); 50 // 得到容器对象 IOC=工厂类+ApplicationContext.xml 51 User user1 = (User) aContext.getBean("user"); 52 User user2 = (User) aContext.getBean("user"); 53 54 System.out.println(user1); 55 System.out.println(user2); 56 } 57 58 }
标签:getbean actor ret ring commons pac epo 工厂 3.2
原文地址:http://www.cnblogs.com/liuyangfirst/p/6533174.html