004_Bean标签

1. <bean>标签的id和name的配置

1.1. id: 使用了约束中的唯一约束。里面不能出现特殊字符的。

1.2. name: 没有使用约束中的唯一约束(理论上可以出现重复的,但是实际开发不能出现的)。里面可以出现特殊字符。

2. Bean的生命周期的配置

2.1. init-method: Bean被初始化的时候执行的方法。

2.2. destroy-method: Bean被销毁的时候执行的方法(Bean是单例创建, 工厂关闭)。

3. Bean的作用范围scope的配置

3.1. singleton: 默认的作用范围, Spring会采用单例模式创建这个对象。

3.2. prototype: 多例模式。

3.3. request: 应用在web项目中, Spring创建这个类的对象以后, 将这个对象存入到request范围中。

3.4. session: 应用在web项目中, Spring创建这个类的对象以后, 将这个对象存入到session范围中。

3.5. globalsession: 应用在web项目中, 必须在porlet环境下使用。但是如果没有这种环境, 相当于session。

4. Bean标签例子

4.1. 新建一个名为SpringBeanTag的Java项目, 拷入Spring相关包

4.2. 新建UserDaoImpl.java

package com.lywgames.dao.impl;

public class UserDaoImpl {
	public UserDaoImpl() {
		System.out.println("UserDaoImpl实例化构造函数。");
	}
	
	public void init() {
		System.out.println("UserDaoImpl初始化了。");
	}
	
	public void login() {
		System.out.println("用户登录成功。");
	}

	public void destroy() {
		System.out.println("UserDaoImpl销毁了。");
	}
}

4.3. 新建UserAddressDaoImpl.java

package com.lywgames.dao.impl;

public class UserAddressDaoImpl {
	public UserAddressDaoImpl() {
		System.out.println("UserAddressDaoImpl实例化构造函数。");
	}
	
	public void init() {
		System.out.println("UserAddressDaoImpl初始化了。");
	}
	
	public void add() {
		System.out.println("添加用户收获地址成功。");
	}

	public void destroy() {
		System.out.println("UserAddressDaoImpl销毁了。");
	}
}

4.4. 新建Test.java

package com.lywgames.beantag;

import java.util.Timer;
import java.util.TimerTask;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lywgames.dao.impl.UserAddressDaoImpl;
import com.lywgames.dao.impl.UserDaoImpl;

public class Test {
	public static void main(String[] args) {
		// 类路径加载配置文件
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

		Timer timer = new Timer();

		// 设定指定的时间time, 此处为2000毫秒
		timer.schedule(new TimerTask() {
			public void run() {
				System.out.println("----------2秒后----------");
				UserDaoImpl userDao1 = (UserDaoImpl) context.getBean("userDao");
				UserDaoImpl userDao2 = (UserDaoImpl) context.getBean("userDao");
				userDao1.login();
				userDao2.login();
			}
		}, 2000);

		// 设定指定的时间time, 此处为5000毫秒
		timer.schedule(new TimerTask() {
			public void run() {
				System.out.println("----------3秒后----------");
				UserAddressDaoImpl userAddressDao1 = (UserAddressDaoImpl) context.getBean("userAddressDao");
				UserAddressDaoImpl userAddressDao2 = (UserAddressDaoImpl) context.getBean("userAddressDao");
				userAddressDao1.add();
				userAddressDao2.add();
			}
		}, 5000);

		// 设定指定的时间time, 此处为10000毫秒
		timer.schedule(new TimerTask() {
			public void run() {
				System.out.println("----------5秒后----------");
				context.close();
				System.exit(-1);
			}
		}, 10000);

		while (true) {

		}
	}
}

4.5. 在src目录下创建applicationContext.xml

4.6. 运行项目, 一加载applicationContext.xml文件, 就创建了和初始化了UserDaoImpl这个单例模式的类实例。2秒后, 我们获取了2次id为userDao这个Bean, 并没有再次给我们创建实例, 说明单例模式类, 在加载applicationContext.xml文件时就会创建实例, 之后获取的都是同一实例。再过3秒, 我们获取了2次id为userAddressDao这个Bean, 给我们创建了2实例, 并调用了初始化接口, 说明多例模式的类, 每获取一次Bean, 就会创建一个实例。再过5秒, 我们销毁了应用程序上下文, 只有单例模式的类调用了销毁方法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值