天天看點

Spring 學習筆記(二)

Spring 屬性配置細節

 1.屬性配置中 有特殊字元的處理 使用<![CDATA[]]>

<!-- 通過構造器方式 第二種方式 type 指定順序 -->
	<bean id="car2" class="xyz.yangjian.spring.demo.Car">
		<constructor-arg value="大車" type="java.lang.String"/>
		<!-- <constructor-arg value="普拉多" type ="java.lang.String"/> -->
		<constructor-arg>
			<value><![CDATA[<普拉多~>]]></value>
		</constructor-arg>
		<constructor-arg value="2000" type ="double"/>
	</bean>
           

2.屬性配置中 給值null 使用 <null/>

<!-- bean引用  構造器注入 -->
	<bean id="person2" class="xyz.yangjian.spring.demo.Person">
		<constructor-arg value="Jerry"></constructor-arg>
		<constructor-arg value="25"></constructor-arg>
		<!-- null值 -->
		<!-- <constructor-arg><null/></constructor-arg> -->
	</bean>
           

3.級聯屬性指派 car.price  注意的是 car 必須先初始化 并且Car類中 必須對屬性設定set方法 否則報錯

<!-- bean引用  構造器注入 -->
	<bean id="person2" class="xyz.yangjian.spring.demo.Person">
		<constructor-arg value="Jerry"></constructor-arg>
		<constructor-arg value="25"></constructor-arg>
		<constructor-arg ref="car1"></constructor-arg>
		<!-- 為級聯屬性指派  注意必須先引入 car對象 才能進行指派 -->
		<property name="car.price" value="5000000"></property>
	</bean>
           

4.List 集合屬性指派 建立Person類 建立cars 集合屬性 private List<Car> cars;

<!-- bean List 集合 -->
	<bean id="person3" class="xyz.yangjian.spring.demo.collections.Person">
		<property name="name" value="Mike"></property>
		<property name="age" value="30"></property>
		<property name="cars">
			<list>
				<ref bean="car1"/>
				<ref bean="car2"/>
                <!--内部引用 外部無法通路 -->
				<bean class="xyz.yangjian.spring.demo.Car">
					<constructor-arg value="福特"></constructor-arg>
					<constructor-arg value="長安"></constructor-arg>
					<constructor-arg value="100000" type="double"></constructor-arg>
				</bean>
			</list>
		</property>
	</bean>
           

5.map 屬性指派 建立NewPerson類 建立cars Map 屬性 private Map<String,Car> cars;

<!-- 使用 map 屬性值 -->
	<bean id="person4" class="xyz.yangjian.spring.demo.collections.NewPerson">
		<property name="name" value="ChunLing"></property>
		<property name="age" value="25"></property>
		<property name="cars">
			<map>
				<entry key="AA" value-ref="car1"></entry>
				<entry key="BB" value-ref="car2"></entry>
                <!--内部引用 外部無法通路 -->
				<entry key="CC">
					<bean class="xyz.yangjian.spring.demo.Car">
						<constructor-arg value="福特"></constructor-arg>
						<constructor-arg value="長安"></constructor-arg>
						<constructor-arg value="100000" type="double"></constructor-arg>
					</bean>
				</entry>
			</map>
		</property>
	</bean>
           

6.配置propertise 屬性值 建立 DataSource類 模仿資料庫屬性指派 建立 private Properties properties;

<!-- 配置propertise 屬性值 -->
	<bean id ="dataSource" class="xyz.yangjian.spring.demo.collections.DataSource">
		<property name="properties">
			<props>
				<prop key="driverClass">com.mysql.jdbc.driver</prop>
				<prop key="url">jdbc:mysql///test</prop>
				<prop key="user">root</prop>
				<prop key="password">123456</prop>
			</props>
		</property>
	</bean>
           

7.配置單例的集合bean 以供多個bean 進行引用 需要引入util 命名空間

<!-- 配置單例的集合bean 以供多個bean 進行引用 需要引入util 命名空間-->
	<util:list id="cars">
		<ref bean="car1"/>
		<ref bean="car2"/>
	</util:list>
	
	<bean id="person5" class="xyz.yangjian.spring.demo.collections.Person">
		<property name="name" value="Jack"></property>
		<property name="age" value="29"></property>
		<property name="cars" ref="cars"></property>
	</bean>
           

8.配置p 命名空間屬性 直接通過p: 給屬性指派

<!-- 配置p 命名空間屬性 -->
	<bean id="person6" class="xyz.yangjian.spring.demo.collections.Person" p:age="35"
		p:name="queen" p:cars-ref="cars">
	</bean>
           

9.測試結果

//建立IOC容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");

		Person person3 = (Person) ac.getBean("person3");
		System.err.println(person3.toString());
		
		NewPerson person4 = (NewPerson) ac.getBean("person4");
		System.err.println(person4.toString());
		
		Person person5 = (Person) ac.getBean("person5");
		System.err.println(person5.toString());
		
		Person person6 = (Person) ac.getBean("person6");
		System.err.println(person6.toString());
		
		DataSource dataSource = ac.getBean(DataSource.class);
		System.out.println(dataSource);
           
Person [name=Mike, age=30, cars=[Car [name=小車, brand=奔馳, size=5, price=5000000.0], Car [name=大車, brand=<普拉多~>, size=0, price=2000.0], Car [name=福特, brand=長安, size=0, price=100000.0]]]
Person [name=ChunLing, age=25, cars={AA=Car [name=小車, brand=奔馳, size=5, price=5000000.0], BB=Car [name=大車, brand=<普拉多~>, size=0, price=2000.0], CC=Car [name=福特, brand=長安, size=0, price=100000.0]}]
Person [name=Jack, age=29, cars=[Car [name=小車, brand=奔馳, size=5, price=5000000.0], Car [name=大車, brand=<普拉多~>, size=0, price=2000.0]]]
Person [name=queen, age=35, cars=[Car [name=小車, brand=奔馳, size=5, price=5000000.0], Car [name=大車, brand=<普拉多~>, size=0, price=2000.0]]]
DataSource [properties={driverClass=com.mysql.jdbc.driver, user=root, url=jdbc:mysql///test, password=123456}]
           

10 下一節學習自動裝配屬性 

未完待更~~