Spring IOC用法入门

Spring属性载器的用法

 

1、在配置文件里配置类以及properties的位置如下

 

 

<!-- 属性加载器的使用 -->

<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">

<value>SpringConfig.parameter.properties</value>

</property>

</bean>

 

2、在Bean里使用美元符获取获取的参数

 

<bean id="initializingBean" class="com.cvicse.steve.springframework.inaction.context.StartLoadBean" init-method="init">

<property name="name">

<value>${ProjectName}</value>

</property>

</bean>

 

InitializingBean接口与init-method方法的用法与区别

 

1、InitializingBean接口

    实现InitializingBean接口为Bean提供初始化方法,在Bean初始始化的时候,调用afterPropertiesSet()方法,在xml当中不需要做额外的配置。

 

2、init-method

    通过xml配置Bean的,init-method,同样是为Bean提供初始化方法,在Bean初始化之后,首先执行该方法。与实现InitializingBean接口相比,init-metod是与spring松耦合;同时,底层是使用反射写的,固较实现InitializingBean接口效率低。

 

ApplicationContext的事件

 

     使Bean具有处理容器发布事件的能力,同时应用系统也可以发布自己的事件,是通过Spring的ApplicationListener来实现处理。

 

    1、自定义事件

     实现一个事件类,继承ApplicationEvent 

 

    2、实现监听器

     public interface ApplicationListener<E extends ApplicationEvent>的onApplicationEvent(E event)方法。(注:这里可以学习到一点泛式的用法)

 

    3、发布事件

   当调用 ApplicationContext的publishEvent(ApplicationEvent event)方法时,监听器将会执行onApplicationEvent方法。

 

ContextLoader用法

    手动编写一个监听器,用来启动Spring Web服务

 

    1、ServletContextListener接口

    实现 ServletContextListener接口的contextInitialized方法

 

    2、ContextLoader用法

  this.contextLoader=new ContextLoader();         this.contextLoader.initWebApplicationContext(event.getServletContext());

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值