Spring开发入门环境搭建篇

1) 源码, jar文件:spring-framework-3.2.5.RELEASE

commons-logging-1.1.3.jar           日志

spring-beans-3.2.5.RELEASE.jar        bean节点

spring-context-3.2.5.RELEASE.jar       spring上下文节点

spring-core-3.2.5.RELEASE.jar         spring核心功能

spring-expression-3.2.5.RELEASE.jar    spring表达式相关表

以上是必须引入的5jar文件


2) 核心配置文件: applicationContext.xml  

Spring配置文件:applicationContext.xml / bean.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- ###############对象创建############### -->

	<bean id="user" class="cn.itcast.b_create_obj.User"></bean>

	
</beans>    

3Api

public class App {

	// 1. 通过工厂类得到IOC容器创建的对象
	@Test
	public void testIOC() throws Exception {
		// 创建对象
		// User user = new User();
		
		// 现在,把对象的创建交给spring的IOC容器
		Resource resource = new ClassPathResource("cn/itcast/a_hello/applicationContext.xml");
		// 创建容器对象(Bean的工厂), IOC容器 = 工厂类 + applicationContext.xml
		BeanFactory factory = new XmlBeanFactory(resource);
		// 得到容器创建的对象
		User user = (User) factory.getBean("user");
		
		System.out.println(user.getId());
		
	}
	
	//2. (方便)直接得到IOC容器对象 
	@Test
	public void testAc() throws Exception {
		// 得到IOC容器对象
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/a_hello/applicationContext.xml");
		// 从容器中获取bean
		User user = (User) ac.getBean("user");
		
		System.out.println(user);
	}
}

Spring配置文件的加载可以在tomcat启动的时候加载。配置在web.xml中:

 <!-- 监听器,初始化Spring容器对象 -->
  <listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 加载Spring核心配置文件 -->
  <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

bean对象创建的细节:

/**

 * 1) 对象创建: 单例/多例

 * scope="singleton",默认值, 即 默认是单例service/dao/工具类】

 *  scope="prototype", 多例; Action对象】

 *

 * 2) 什么时候创建?

 *   scope="prototype"  在用到对象的时候,才创建对象。

 *    scope="singleton"  在启动(容器初始化之前), 就已经创建了bean,且整个应用只有一个。

 * 3)是否延迟创建

 *   lazy-init="false"  默认为false,  不延迟创建,即在启动时候就创建对象

 *   lazy-init="true"   延迟初始化, 在用到对象的时候才创建对象

 *    (只对单例有效)

 * 4) 创建对象之后,初始化/销毁

 *   init-method="init_user"       【对应对象的init_user方法,在对象创建爱之后执行 】

 *    destroy-method="destroy_user"  【在调用容器对象的destriy方法时候执行,(容器用实现类)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值