您的位置:首页 > 编程语言 > Java开发

配置bean

2017-11-20 16:57 106 查看
1)在application中配置bean

    <bean id="helloworld" class="com.yc.spring.beans.HelloWorld">

    <property name="name" value="Spring"></property>

  </bean>

注:class 是bean 的全类名,通过反射在IOC容器中创建Bean,所以要求Bean中必须具有无参数构造函数

    id:唯一

2)ApplicationContext:代表IOC容器

3)Spring 提供两种类型的IOC容器

  --BeanFactory 

 ---ApplicationContext:是BeanFactory的子接口

4)Applicationcontext的主要实现类,在初始化上下文时就实例化所有单例的Bean

  1.ClassPathXmlApplicationContext

  2. FilSystemXmlApplicatioContext

 5)从ApplicationContext中获取Bean的方法

   1.通过id获取bean  ctx.getbean()

   2.通过类型获取  ctx.getBean(HelloWorld.class)  要求:IOC容器中必须只能有一个该类型的Bean

6)注入方式

   属性注入:最常用的方式

   构造方法注入:<constructor-arg> 通过传入不同的类型进行区分不同的构造方法

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring bean