spring的依赖注入

    1、spring的普通属性注入 
 参见:spring文档3.3章节
 
什么是属性编辑器,作用? 
 * 自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入
 spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器
 
 * 如何定义属性编辑器?
  * 继承PropertyEditorSupport类,覆写setAsText()方法,参见:UtilDatePropertyEditor.java
  * 将属性编辑器注册到spring中,参见:applicationContext-editor.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: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.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     <!-- 定义属性编辑器 -->      
	<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<bean class="com.bjsxt.spring.UtilDatePropertyEditor">
						<property name="format" value="yyyy-MM-dd"/>
					</bean>
				</entry>
			</map>
		</property>
	</bean>	
	
	<!-- 
	<bean id="utilDatePropertyEditor" class="com.bjsxt.spring.UtilDatePropertyEditor"></bean>
	 -->
</beans>
 
  
依赖对象的注入方式,可以采用:
 * ref属性

 
  <property name="bean3" ref="bean3"/>
  <property name="bean5" ref="bean5"/>
 
 * <ref>标签

<property name="bean4">
   <ref bean="bean4"/>
  </property>

 * 内部<bean>来定义

<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<bean class="com.bjsxt.spring.UtilDatePropertyEditor">
						<property name="format" value="yyyy-MM-dd"/>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
 
 
如何将公共的注入定义描述出来?
 * 通过<bean>标签定义公共的属性,指定abstract=true
 * 具有相同属性的类在<bean>标签中指定其parent属性
 
 参见:applicationContext-other.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: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.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
   
   <bean id="beanAbstract" abstract="true">
   		<property name="id" value="1000"/>
   		<property name="name" value="Jack"/>  
   </bean>         
   
   <bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
   		<property name="name" value="Tom"/>   //将会覆盖
   		<property name="password" value="123"/>
   </bean>        
   
   <bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>

你可能感兴趣的:(spring,AOP,bean,Class,文档,encoding)