Spring-DI(依赖注入)

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="person" class="cn.itcast.spring0909.di.xml.set.Person">
    	<!-- 
    		property就是代表属性
    		  在spring中基本类型(包装类型和String)都可以用value来赋值
    		                         引用类型用ref赋值
    	 -->
    	 
    	<property name="pid" value="5"></property>
    	<property name="pname" value="王二麻子"></property>
    	<property name="student" ref="student">
    		
    	</property>
    	<property name="lists">
    		<list>
    			<value>list1</value>
    			<value>list2</value>
    			<ref bean="student"/>
    		</list>
    	</property>
    	<property name="sets">
    		<set>
    			<value>set1</value>
    			<value>set2</value>
    			<ref bean="student"/>
    		</set>
    	</property>
    	<property name="map">
    		<map>
    			<entry key="map1">
    				<value>map1</value>
    			</entry>
    			<entry key="map2">
    				<value>map2</value>
    			</entry>
    			<entry key="map3">
    				<ref bean="student"/>
    			</entry>
    		</map>
    	</property>
    	<property name="properties">
    		<props>
    			<prop key="prop1">
    				prop1
    			</prop>
    		</props>
    	</property>
    </bean>
	<bean id="student" class="cn.itcast.spring0909.di.xml.set.Student"></bean>
</beans>



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="person" class="cn.itcast.spring0909.di.xml.constructor.Person">
    	<!-- 
    		构造函数的参数
    		  index  第几个参数,下标从0开始
    		  type   参数的类型
    		  ref    如果类型是引用类型,赋值
    		  value  如果类型是基本类型,赋值
    		 说明:
    		    只能指定一个构造函数
    	 -->
    	<constructor-arg index="0"  type="java.lang.String" value="干露露"></constructor-arg>
	<constructor-arg index="1" ref="student"></constructor-arg>
    </bean>
    
    <bean id="student" class="cn.itcast.spring0909.di.xml.constructor.Student"></bean>
</beans>



面向接口的操作

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- 把documentManager,worddocument,exceldocument,pdfdocument -->
	<bean id="documentManager" class="cn.itcast.spring0909.document.spring.DocumentManager">
		<!-- 
			document是一个接口
		 -->
		<property name="document">
		    <!-- 
		    	通过ref给接口赋值
		     -->
			<ref bean="wordDocument"/>
		</property>
	</bean>

	<bean id="wordDocument" class="cn.itcast.spring0909.document.spring.WordDocument"></bean>

	<bean id="excelDocument" class="cn.itcast.spring0909.document.spring.ExcelDocument"></bean>

	<bean id="pdfDocument" class="cn.itcast.spring0909.document.spring.PDFDocument"></bean>
</beans>

mvc

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<bean id="personAction" class="cn.itcast.spring0909.mvc.spring.PersonAction">
		<property name="personService">
			<ref bean="personSerivce" />
		</property>
	</bean>

	<bean id="personSerivce" class="cn.itcast.spring0909.mvc.spring.PersonServiceImpl">
		<property name="personDao">
			<ref bean="personDao" />
		</property>
	</bean>

	<bean id="personDao" class="cn.itcast.spring0909.mvc.spring.PersonDaoImpl"></bean>
</beans>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值