一:实例化bean
构造方法实例化
package com.spring.chapter2.service;
public interface Fruit {
public void eat();
}
package com.spring.chapter2.service.impl;
import com.spring.chapter2.service.Fruit;
public class Apple implements Fruit {
public void eat() {
System.out.println("吃苹果!");
}
}
package com.spring.chapter2.service.impl;
import com.spring.chapter2.service.Fruit;
public class Orange implements Fruit {
private String name;
public Orange() {
super();
}
public Orange(String name) {
super();
this.name = name;
}
@Override
public void eat() {
System.out.println("吃桔子");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
配置文件: //chapter2.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-2.5.xsd">
<bean id="fruit" class="com.spring.chapter2.service.impl.Orange" />
</beans>
测试类:
public class Test {
public static void main(String[] args) {
ApplicationContext acx = new ClassPathXmlApplicationContext(
"chapter2.xml");
Fruit fruit =(Fruit)acx.getBean("fruit");
fruit.eat();
}
}
spring容器在实例化对象的时候,需要用到无参的构造方法.记住,提供一个无参的构造.
二:使用静态工厂方法实例化
public class FruitFactory {
public static Fruit getInstanceApple() {
return new Apple();
}
public static Fruit getInstanceOrange() {
return new Orange();
}
public static Fruit getInstance(int type) {
if(type == 1){
return new Apple();
}else if (type == 2){
return new Orange();
}else{
return null;
}
}
}
配置文件: //chapter2.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-2.5.xsd">
<bean id="apple" class="com.spring.chapter2.model.FruitFactory" factory-method="getInstanceApple" />
<bean id="orange" class="com.spring.chapter2.model.FruitFactory" factory-method="getInstanceOrange" />
<bean id="fruit" class="com.spring.chapter2.model.FruitFactory" factory-method="getInstance">
<constructor-arg type="int" value="2" />
</bean>
</beans>
测试类:
public class Test {
public static void main(String[] args) {
ApplicationContext acx = new ClassPathXmlApplicationContext(
"chapter2.xml");
Apple apple =(Apple)acx.getBean("apple");
apple.eat();
Orange orange =(Orange)acx.getBean("orange");
orange.eat();
Fruit fruit =(Fruit)acx.getBean("fruit");
fruit.eat();
}
}
三:Bean的作用域
作用域 | 描述 |
singleton | 在每个Spring IoC容器中一个bean定义对应一个对象实例。 |
prototype | 一个bean定义对应多个对象实例。 |
request | 在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。 |
session | 在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。 |
global session | 在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。 |
singleton单例示例
<bean id="fruit" class="model.Apple" scope="singleton"/>
scope="singleton"是默认值,设于不设都可以,每次容器都会给同一个对象给你
prototype非单例示例
<bean id="fruit" class="model.Apple" scope="prototype"/>
配置此范围后,每次向容器索取对象时,容器会创建一个新的对象给你.
四:延迟初始化bean
lazy-init="true"
lazy-init="false"
<bean id="fruit" class="model.Apple" lazy-init="false" />
不要和scope属性一起使用.
true:延时,不立即创建
false:不延时,立即创建
五:类初始化和销毁自动调用的方法
<bean id="fruit" class="model.Apple" init-method="init"
destroy-method="destroy" />
init-method="init":指定方法名,在对象实例化以后,自动调用
destroy-method="destroy":指定方法名,在容器关闭以后,自动调用