Bean 基本语法

1.1 Bean基本语法
id: bean的唯一标识,类似于身份证号
name: bean的名字,类似于人名,可以有重复的名字。
当出现id和name相同时,Ioc容器会自动消除重复。

为什么id和name同时存在?都是标识,只使用一个呢??
—使用id的好处就是可以很方便的使用XML的id属性提前得知bean是否存在以及是否重名。
—那直接使用id就好了,为什么还要使用name属性呢?

alias:别名
import:引入其他bean

singleton 与 non-singleton:
默认的bean是singleton的模式,即只有一个共享的实例存在,即对所有该bean请求返回这个唯一的实例。

如果为non-singleton,每次对bean的请求都会创建一个新的bean实例。即 new 操作。

depends-on
depends-on属性用来指定初始化该bean之前,先初始化依赖的bean。在销毁该bean之前先销毁依赖的bean。

1.2 Ioc如何实例化Bean
传统的应用程序实例化bean时利用 new 或 反射机制来实现。
Ioc容器则利用Bean中声明的类名通过反射机制来进行实例化。
大多数框架会使用反射。

Ioc实现三种实例化Bean的方式:
通过构造器实例化bean

  1. create a class include default constructor and other constrctor
 package com.myspring.helloworld;
  public class HelloConstrustor implements HelloApi{
    private  String message;
    public HelloConstrustor(){
        this.message = "hello default construstor!";
    }

    public HelloConstrustor(String message){
        this.message = message;
    }

    @Override
    public void sayHelo() {
        System.out.println(message);

    }
}
  1. create 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"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

    <!-- id 表示你这个组件的名字,class表示组件类-->  
    <!-- 使用默认的构造器-->
    <bean name="default_constructor" class="com.myspring.helloworld.HelloConstrustor"> </bean>  

    <!-- 使用带参数的构造器-->
    <bean name="argu_constructor" class="com.myspring.helloworld.HelloConstrustor"> 
    <!--指定构造器的参数-->
          <constructor-arg index="0" value = "hello argu construstor"/>  
    </bean>
 </beans>
  1. create test class
BeanFactory factory = new ClassPathXmlApplicationContext("helloconstructor.xml");
        //面向接口编程
        HelloApi helloapi = factory.getBean("default_constructor", HelloApi.class);
        helloapi.sayHelo(); 
        HelloApi helloapi1 = factory.getBean("argu_constructor", HelloApi.class);
        helloapi1.sayHelo();`

通过静态工厂的方式实例化bean

  1. create static factory
  package com.myspring.helloworld;

public class HelloStaticFactory {
    public static HelloApi newInstance(String message){
        return new HelloConstrustor(message);       
    }
}  
  1. create 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"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

    <!-- id 表示你这个组件的名字,class表示组件类-->  
    <!-- 使用静态工厂方式-->
    <bean id="static_factory" class="com.myspring.helloworld.HelloStaticFactory" factory-method="newInstance">
    <!--指定构造器的参数-->
          <constructor-arg index="0" value = "hello static factory construstor"/>  
    </bean>
 </beans>
  1. create test class
BeanFactory factory = new ClassPathXmlApplicationContext("hellostaticfactory.xml");
        //面向接口编程
        HelloApi helloapi = factory.getBean("static_factory", HelloApi.class);
        helloapi.sayHelo();

通过实例工厂的方式实例化bean

  1. create instance factory class
package com.myspring.helloworld;
public class HelloInstanceFactory {
    public HelloApi newInstance(String message){
        return new HelloConstrustor(message);       
    }
}
  1. create 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"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

    <!—1、定义实例工厂Bean -->  
    <bean id="beanInstanceFactory"  class="com.myspring.helloworld.HelloInstanceFactory"/> </bean>

    <!—2、使用实例工厂Bean创建Bean -->  
    <bean id="instance_factory"  factory-bean="beanInstanceFactory"  factory-method="newInstance">  
     <constructor-arg index="0" value="Hello instance factory!"></constructor-arg>  
    </bean>
 </beans>
  1. create test class
        BeanFactory factory = new ClassPathXmlApplicationContext("helloinstancefactory.xml");
        //面向接口编程
        HelloApi helloapi = factory.getBean("instance_factory", HelloApi.class);
        helloapi.sayHelo();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值