2:Bean的基本管理(ApplicationContext BeanFactory)

BeanFactory负责定义文件,管理对象的加载,生成,维持与对象的依赖关系,负责bean的生命周期
自己根据书上的解释,写了一些例子:
一:常用方法
1:boolean containsBean()

package testbean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class BeanFactoryTest {
Resource rs = new ClassPathResource("beans-config.xml");
BeanFactory bean = new XmlBeanFactory(rs);
boolean isnull = bean.containsBean("hellobean");//containsBean()判断在beans-config.xml中是否包含名称为hellobean的bean.返回值类型为布尔

public static void main(String args[]){
boolean isnull = new BeanFactoryTest().isnull;
System.out.print(isnull);
}
}

2:Object getBean()
在第一个spring中的例子中,就能找到getBean()的使用.

package test;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class SpringDemo {

public static void main(String args[]){

Resource rs = new ClassPathResource("beans-config.xml");//获取xml路径
BeanFactory factory = new XmlBeanFactory(rs);//建立BeanFactory实例
HelloWorld hello = (HelloWorld) factory.getBean("hellobean");//使用getBean方法获得HelloWorld类的实例
// System.out.print(hello.getHellobean());


Business business = (Business) factory.getBean("business");//根据bean的id获取xml中指向的对象
business.sava();//执行Business中的save()方法
}
}

]
3:...未完待续

二:使用ApplicationContext取代BeanFactory管理bean
//推荐使用

优点:
1:提供更好的访问资源文件的方式
2:提供解析文字消息的方法
3:支持国际化消息
4:可以发布事件

package testbean;

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

import test.HelloWorld;

public class Applicationtest {


public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
HelloWorld hello = (HelloWorld)context.getBean("hellobean");
System.out.print(hello.getHellobean());
}

}

[color=red]注意:[/color]使用ApplicationContext可以实现同时读取多个资源文件(需要添加spring jar包中的spring-context.jar,使用myeclipse的话已自动添加)
(1):数组形式

ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans-config.xml","beans-config2.xml"});

(2):路径
可以使用file:/ classpath*: http:// 等URL

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:beans-config.xml");

表示所有的classpath为前置路径的都匹配
(3):使用*字符

ApplicationContext context = new ClassPathXmlApplicationContext("beans*.xml");

表示所有以beans开头的文件都匹配,需要注意的是只在实际文件系统中有效,如资源文件在jar文件中则无效.
另一种实现一次读多个资源文件的方式:
在一个xml文件中,引用其他xml文件,实现调用该xml文件,顺带调用其引用的xml文件

<beans>
<import resource="beans-config2.xml"/>
<import resource="beans-config3.xml"/>

<bean id="hellobean"
class="test.HelloWorld">
<property name="hellobean">
<value>hello!</value>
</property>
</bean>
</beans>

[color=red]需要注意:使用此种方法,需要将import放在beans标签之间,且一定要放在bean标签之前[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值