spring之IOC

来源:互联网 发布:路由器访客网络连不上 编辑:程序博客网 时间:2024/05/17 01:26

注入:

设值注入:属性必须配有setter()方法

构造注入:利用构造函数注入,<constructor-arg ref="refBean"/>

对于依赖关系无须变化的注入,尽量采用构造注入;而其他则采用设值注入.


BeanFactory负责配置,创建,管理Bean.还有另外一个子接口ApplicationContext

BeanFactory接口基本方法:

boolean contaionsBean(String name);

Object getBean(String name);

Object getBean(String name, Class requiredType);

Class getType(String name);

ClassPathResource cpr = new ClassPathResource("beans.xml");XmlBeanFactory factory = new XmlBeanFactory(cpr);


由于ApplicationContext内置有Resource可以直接加载配置文件

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplication(new String[]{"beans.xml"});//可以一次加载多个配置文件

ApplicationContext继承了MessageSource接口,支持国际化:

String getMessage(String code,Object[] args,Locate loc);

-- 国际化 <bean id="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basename"><list><value>message</value></list></property></bean>

message_en_US.properties

message.properties

转码工具:native2ascii message.properties message_zh_CN.properties


ApplicationContext时间机制

ApplicationEvent:容器事件

ApplicationListener:监听器

<!-- 事件监听 -->    <bean id="emailListListener" class="fox.event.en.EmailNotifier"></bean>
内置事件:

extends ContextRefreshedEvent..ContextStartedEvent..ContextClosedEvent..ContextStoppedEvent..RequestHandledEvent



Bean的定义

bean的作用域:scope

singleton:单例模式

prototype:原型模式,每次getBean都会产生一个新的Bean

request:顾名思义

session:顾名思义

<listener>      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener>

Bean的创建和配置

bean工厂有静态工厂与实例工厂

容器工厂

/** * 静态工厂方法创建bean * @author Administrator * */public class BeingStaticFactory {public static Being getBeing(String s){if(s.equalsIgnoreCase("dog")){return new Dog();}else{return new Cat();}}}/** * 实例工厂创建bean */public class BeingFactory {public Being getBeing(String s){if(s.equalsIgnoreCase("dog")){return new Dog();}else{return new Cat();}}}

<!-- 工厂 --><!-- 静态工厂 --><bean id="sdog" class="fox.factory.BeingStaticFactory" factory-method="getBeing"><constructor-arg index="0" value="dog"></constructor-arg><property name="msg" value="I'm a dog "></property>--普通依赖注入属性</bean><bean id="scat" class="fox.factory.BeingStaticFactory" factory-method="getBeing"><constructor-arg value="cat"></constructor-arg><property name="msg" value="I'm a cat "></property></bean><!-- 实例工厂 --><bean id="beingFactory" class="fox.factory.BeingFactory"></bean><bean id="dog" factory-bean="beingFactory" factory-method="getBeing"><property name="msg" value="I'm a dog "></property><constructor-arg value="dog"></constructor-arg></bean><bean id="cat" factory-bean="beingFactory" factory-method="getBeing"><property name="msg" value="I'm a cat "></property><constructor-arg value="cat"></constructor-arg></bean><!-- 容器工厂 --><bean id="appFactory_cat" class="fox.factory.BeingAppFactory"><property name="type" value="cat"></property></bean>


bean实例的属性值

<?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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!--<context:annotation-config />--><context:component-scan base-package="fox.aop"><context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/></context:component-scan><bean id="chinese" class="fox.bean.impl.Chinese"><property name="axe" ref="steelAxe"></property></bean><bean id="stoneAxe" class="fox.bean.impl.StoneAxe"></bean><bean id="steelAxe" class="fox.bean.impl.SteelAxe" scope="prototype"></bean><!-- 国际化 <bean id="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basename"><list><value>message</value></list></property></bean>--><!-- 时间监听 --><bean id="emailListListener" class="fox.event.en.EmailNotifier"></bean><!-- 作用域 --><bean id="s1" class="fox.bean.ScopeTest" scope="singleton"></bean><bean id="s2" class="fox.bean.ScopeTest" scope="prototype"></bean><bean id="cl" class="fox.bean.impl.ChineseLife" init-method="init" destroy-method="close"><property name="axe" ref="steelAxe"></property></bean><!-- 协调作用域不同步bean --><bean id="cds" class="fox.bean.impl.ChineseLife_dif_scope"><lookup-method bean="steelAxe" name="createAxe" /><property name="axe" ref="steelAxe"></property></bean><!-- 配置个数据源玩玩 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><property name="driverClass" value="${jdbc.driverClassName}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property><property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property><property name="minPoolSize" value="${jdbc.minPoolSize}"></property><property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property><property name="maxIdleTime" value="${jdbc.maxIdleTime}"></property></bean><!-- 工厂 --><!-- 静态工厂 --><bean id="sdog" class="fox.factory.BeingStaticFactory" factory-method="getBeing"><constructor-arg index="0" value="dog"></constructor-arg><property name="msg" value="I'm a dog "></property></bean><bean id="scat" class="fox.factory.BeingStaticFactory" factory-method="getBeing"><constructor-arg value="cat"></constructor-arg><property name="msg" value="I'm a cat "></property></bean><!-- 实例工厂 --><bean id="beingFactory" class="fox.factory.BeingFactory"></bean><bean id="dog" factory-bean="beingFactory" factory-method="getBeing"><property name="msg" value="I'm a dog "></property><constructor-arg value="dog"></constructor-arg></bean><bean id="cat" factory-bean="beingFactory" factory-method="getBeing"><property name="msg" value="I'm a cat "></property><constructor-arg value="cat"></constructor-arg></bean><!-- 容器工厂 --><bean id="appFactory_cat" class="fox.factory.BeingAppFactory"><property name="type" value="cat"></property></bean><!-- 继承 --><bean id="chineseTemplate" class="fox.bean.impl.Chinese" abstract="true"><property name="axe" ref="steelAxe"></property></bean><bean id="realChinese" parent="chineseTemplate"></bean><!-- 注入集合 --><bean id="arrayChinese" class="fox.bean.value.Chinese"><property name="schools"><list><value>小学</value><value>中学</value><value>大学</value></list></property><property name="scores" ><map><entry><!-- 每个entry一个键值对 --><key><value>数学</value></key><value>87</value></entry><entry><!-- 每个entry一个键值对 --><key><value>语文</value></key><value>88</value></entry></map></property><property name="health"><props><prop key="血压">正常</prop><prop key="身高">175</prop></props></property><property name="axes"><set><value>字符串裤子</value><bean class="fox.bean.impl.SteelAxe"></bean><ref local="stoneAxe"/></set></property><property name="books"><list><value>数据库</value><value>数据结构</value></list></property></bean><!-- 后处理器 --><!-- bean --><bean class="fox.bean.postprocessor.MyBeanPostProcessor"></bean><bean id="postChinese" class="fox.bean.postprocessor.Chinese" init-method="init"><property name="name" value="heihei"></property><property name="axe" ref="steelAxe"></property></bean><!-- 容器 --><bean class="fox.bean.postprocessor.MyBeanFactoryPostProcessor"></bean><!-- 属性占位符配置器 --><bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="locations"><list><value>dbconn.properties</value><!-- 可以配置多个 --></list></property></bean><!-- 资源访问 --><bean id="resourceBean" class="fox.resource.ResourceBean"><property name="resource" value="classpath:book.xml"></property></bean><!-- AOP --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>




原创粉丝点击