初识Spring IoC container

为了提升自身学习的效果,以下将记录Spring 3.2学习的点滴:

1、Spring是什么?

首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment等;

2、初识Spring IOC Container

org.springframework.beans和org.springframework.context两个包是Spring IOC容器的基础; org.springframework.context.ApplicationContext接口根据配置元数据实例化、配置和组装Beans,配置数据支持XML、java注解、java代码;下图为Spring IOC容器工作的示意图:



3、Spring IOC容器实例化bean的几种方法

1)通过构造函数实例化Bean,使用此方法构造Bean时,Bean必须有无参构造函数;

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="defualtCat" class="com.spring.model.Cat">
	</bean>
</beans>
com.spring.model.Cat入下:

package com.spring.model;

public class Cat {
	private String name;
	
	private String color;
	
	public Cat() {
		name = "defaultCat";
		color = "white";
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Cat Name:" + name +",Color:"+color;
	}
}

代码测试类如下:

package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.model.Cat;

public class TestSpring {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"bean.xml"});
		System.out.println(context.getBean("defualtCat", Cat.class));
	}
}
输入结果为:Cat Name:defaultCat,Color:white


2)通过Bean的静态方法初始化,注意初始化Bean的方法必须是static的;

通过改造上面的Cat类,增加一个静态初始化Cat对象的方法,代码如下:

public Cat(String name, String color) {
		this.name = name;
		this.color = color;
	}

	public static Cat createCat() {
		Cat cat = new Cat("methodCat", "black");
		return cat;
	}

配置文件增加一个Bean配置:

<bean id="methodCat" class="com.spring.model.Cat" factory-method="createCat">
	</bean>

测试代码System.out.println(context.getBean("methodCat", Cat.class));打印结果如下:

Cat Name:methodCat,Color:black

3)通过工厂类的方法构造,此构造方法不必是static;

构造工厂类如下:

package com.spring.test;

import com.spring.model.Cat;

public class CatFactory {
	
	public Cat createCat() {
		return new Cat("FactoryCat", "red");
	}
}

配置文件增加如下配置:

<bean id="catFactory" class="com.spring.test.CatFactory">
	</bean>
	
	<bean id="factoryCat" class="com.spring.model.Cat" factory-method="createCat" factory-bean="catFactory" >
	</bean>

测试代码System.out.println(context.getBean("factoryCat", Cat.class));打印结果如下: Cat Name:FactoryCat,Color:red



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值