Spring之Ioc配置和使用

Ioc的配置使用

Bean的配置

IoC容器通过Bean的定义创建Bean,Bean的定义在容器内部用BeanDefinition对象表示,包含如下内容:

  • Bean的全限定类型,用于加载类
  • Bean的行为定义:定义了Bean在容器中的行为
  • Bean的创建方式定义:通过构造器还是工厂方法
  • Bean之间关系定义:依赖bean
配置项作用取值
scope作用范围prototype: 模板类型,每次getBean都返回一个新的对象。singleton: IoC容器中只保留一个同一ID的Bean。request: 每一次http请求都会产生一个bean,且只在此次request内有效。session: 每一次http请求都会产生一个bean,且只在此次session内有效,参考
lazy-init懒加载true、false

Bean的其它创建方式

某些SingletonBeanFactory接口的实现类允许将那些非BeanFactory创建、已有的用户对象注册到容器中。如:DefaultListableBeanFactory的registerSingleton()方法。

Bean的命名

不指定id

<bean class="com.kiikgu.HelloServiceImpl" />

ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");
HelloService helloService = context.getBean(HelloService.class);
  • 如果IoC容器中包含多个同类型的Bean,使用T getBean(Class requireType)会报错

指定id

<bean id="helloService" class="com.kiikgu.HelloServiceImpl" />

ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");
HelloService helloService = (HelloService) context.getBean("helloService");
  • ID必须Ioc容器唯一

指定name

<bean name="helloService" class="com.kiikgu.HelloServiceImpl" />

ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");
HelloService helloService = (HelloService) context.getBean("helloService");
  • name为标识符,必须Ioc容器唯一

指定id和name

<bean id="helloService" name="helloService4" class="com.kiikgu.HelloServiceImpl" />

ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");

HelloService helloService = (HelloService) context.getBean("helloService");

HelloService helloService4 = (HelloService) context.getBean("helloService4");
System.out.println(helloService == helloService4);
  • id为标识符,name为别名,id可以与name相同,但两者必须Ioc容器唯一

指定多个name,name之间以“,”、“ ”、“;”分割

  <bean name="helloService alias1;alias2,alias3" class="com.kiikgu.HelloServiceImpl" />


    ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");
    HelloService helloService = (HelloService) context.getBean("helloService");
    HelloService alias1 = (HelloService) context.getBean("alias1");
    HelloService alias2 = (HelloService) context.getBean("alias2");
    HelloService alias3 = (HelloService) context.getBean("alias3");
    System.out.println(helloService == alias1);
    System.out.println(alias1 == alias2);
    System.out.println(alias2 == alias3);

    String[] helloServiceAlias = context.getAliases("helloService");
    System.out.println(Arrays.toString(helloServiceAlias));
  • 第一个name为标识符,其它为别名,必须Ioc容器唯一

通过alias标签指定别名

  <bean name="helloService" class="com.kiikgu.HelloServiceImpl" />
  <alias name="helloService" alias="alias1" />
  <alias name="helloService" alias="alias2" />
  <alias name="helloService" alias="alias3" />
  • 别名也必须Ioc容器唯一

实例化Bean

Ioc容器获取Bean的配置信息(BeanDefinition),通过反射机制创建Bean。主要有一下几种方式实例化Bean:

使用构造器实例化Bean

默认构造器:

<bean name="helloService" class="com.kiikgu.HelloServiceImpl" />
  • 配置的类必须要有默认构造器

含参数构造器:

  <bean name="helloService" class="com.kiikgu.HelloServiceImpl">
    <constructor-arg name="message" value="hello world!!!" />
  </bean>

    ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");
    HelloService helloService = (HelloService) context.getBean("helloService");
    helloService.sayHello();

使用静态工厂实例化Bean

工厂类

public class HelloServiceFactory {

  public static HelloService getHelloService(String message) {
    System.out.println("HelloServiceFactory.getHelloService");
    return new HelloServiceImpl(message);
  }
}

配置:

  <bean name="helloService1" class="com.kiikgu.HelloServiceFactory" factory-method="getHelloService">
    <constructor-arg name="message" value="hello world!!!" />
  </bean>

调用:

    ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");
    HelloService helloService1 = (HelloService) context.getBean("helloService1");
    helloService1.sayHello();

使用实例工厂方法实例化Bean

实例工厂:

public class HelloServiceFactory {

  public HelloService getInstance(String message) {
    System.out.println("HelloService.getInstance");
    return new HelloServiceImpl(message);
  }
}

配置:

  <bean id="helloServiceFactory" class="com.kiikgu.HelloServiceFactory" />
  <bean name="helloService2" factory-bean="helloServiceFactory" factory-method="getInstance">
    <constructor-arg name="message" value="hello factory" />
  </bean>

调用:

    ApplicationContext context = new ClassPathXmlApplicationContext("spring-learn.xml");
    HelloService helloService2 = context.getBean("helloService2", HelloService.class);
    helloService2.sayHello();
  • 不用配置全限定类

参考

http://jinnianshilongnian.iteye.com/blog/1413857
http://www.cnblogs.com/qq78292959/p/3716827.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值