Java网课基础笔记(12)19-07-25

 

 

1.属性依赖注入的三种方式

  1.  构造器参数注入
  2. setter方法属性注入(setter方法的规范需要符合JavaBean规范)
  3. 接口注入

Spring框架规范中通过配置文件配置的方式,只支持构造器参数注入和setter方法属性注入,不支持接口注入!

构造器参数注入

//演示构造器传参,通过构造方法来赋值
package com.igeek.xmlpropertydi;
public class Car {
	private Integer id;
	private String name;
	private Double price;
	public Car(Integer id, String name, Double price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}
	public Integer getId() {
		return id;
	}
	@Override
	public String toString() {
		return "Car [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
}
//测试类
package com.igeek.xmlpropertydi;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringText1 {
	@Test
	public void testArgs() {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car=(Car) ac.getBean("car");
		System.out.println(car);
	}
}
//applicationContext.xml
<!-- 构造器传参 -->
		<bean id="car" class="com.igeek.xmlpropertydi.Car">
		<!-- index 参数的索引位置定位属性 =0是传第一个参数 -->
		<!-- name 根据属性参数名称定位属性 -->
		<!-- type 根据属性参数类型定位属性 -->
		<constructor-arg index="0" value="1"></constructor-arg>
		<constructor-arg name="name">
			<value>宝马2代</value>
		</constructor-arg>
		<constructor-arg type="java.lang.Double" value="99999"></constructor-arg>
		</bean>

 setter方法属性注入

package com.igeek.xmlpropertydi;
public class Person {
	private Integer id;
	private String name;
	private Car car;
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", car=" + car + "]";
	}
		
}

//测试类
package com.igeek.xmlpropertydi;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringText1 {
	@Test
	public void testArgs() {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car=(Car) ac.getBean("car");
		System.out.println(car);
	}
	@Test
	public void testArgs1() {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person=(Person) ac.getBean("person");
		System.out.println(person);
	}
}
//aplicationContex.xml
<!-- setter方法传参 -->
		<bean id="person" class="com.igeek.xmlpropertydi.Person">
		<!-- property 专门进行setter属性注入的标签
			name:setter方法的属性名字,如setXXX方法,跟setter方法对应,并不是跟属性对应,属性名称可以改不影响
			value:赋简单类型的值
			ref:bean的名字,对象的引用	
		 -->
			<property name="id" value="001"></property>
			<property name="name" value="tom"></property>
			<property name="car" >
				<ref bean="car"></ref>
			</property>
		</bean>

2.p名称空间:

作用:Schema区分同名元素。(有点类似于Java包)

<beans xmlns="http://www.springframework.org/schema/beans"

Xmlns没有前缀是默认的名称空间。

为简化xml文件的配置,Spring2.5版本开始引入了人一个新的p名称空间。简单的来说,它的作用是为了简化setter方法属性依赖注入配置的,它不是真正的名称空间

package com.igeek.xmlpropertydi;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringText1 {
	@Test
	public void testArgs() {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car=(Car) ac.getBean("car");
		System.out.println(car);
	}
	@Test
	public void testArgs1() {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person=(Person) ac.getBean("person2");
		System.out.println(person);
	}
	
}

aplicationContex.xml 配置文件增加这句

 xmlns:p="http://www.springframework.org/schema/p" 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	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.xsd">
        <!-- 增加以下代码 -->
        <!-- 名称空间p的用法 -->
		<bean id="person2" class="com.igeek.xmlpropertydi.Person" 
		p:id="1002" p:name="chen" p:car-ref="car"
		></bean>
</beans>

3.spEL表达式的使用

spEL是一种表达式语言,它是spring3.x版本的新特性。

作用:支持在运行时操作和查询对象,其语法类似统一的EL语言,但是spEL提供了额外的功能,功能更加强大。

【面试题】什么是EL、OGNL、spEL

EL:操作servlet相关的一些对象和相关的值,$(request.nam)

OGNL:主要操作struts2值栈,<s:propetry value="#request.name">

spEL:操作bean相关的

语法:#{...} ,引用另一个Bean、属性、方法

spEL表达式的使用功能比较多,Bean操作相关的通常有:

  • #{beanid} 引用bean(具体对象)
  • #{beanid.属性} 引用bean属性
  • #{bean.方法(参数)}  调用bean方法
    //演示构造器传参,通过构造方法来赋值
    package com.igeek.xmlpropertydi;
    public class Car {
    	private Integer id;
    	private String name;
    	private Double price;
    	public Car(Integer id, String name, Double price) {
    		this.id = id;
    		this.name = name;
    		this.price = price;
    	}
    	public Integer getId() {
    		return id;
    	}
    	//增加name的get()方法,不然bean获取不到name
    	public String getName() {
    		return name;
    	}
    	@Override
    	public String toString() {
    		return "Car [id=" + id + ", name=" + name + ", price=" + price + "]";
    	}
    }
    //测试类
    package com.igeek.xmlpropertydi;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class SpringText1 {
    	@Test
    	public void testArgs() {
    		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
    		Person person=(Person) ac.getBean("person4");
    		System.out.println(person);
    	}
    	@Test
    	public void testArgs1() {
    		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
    		Person person=(Person) ac.getBean("person3");
    		System.out.println(person);
    	}
    	
    }
    
    //aplicationContex.xml增加
    <!-- spEL表达式的用法 -->
    		<bean id="person3" class="com.igeek.xmlpropertydi.Person" 
    		p:id="#{car.id}" p:name="#{car.name}" p:car="#{car}"
    		></bean>
    		<bean id="person4" class="com.igeek.xmlpropertydi.Person" 
    		p:id="#{1+1}" p:name="#{'chen'.toUpperCase()}" p:car="#{car}"
    		></bean>
    		

4.集合方法注入

Spring 提供了四种类型的集合的配置元素,如下所示: 

元素描述
<list>它有助于连线,如注入一列值,允许重复。
<set>它有助于连线一组值,但不能重复。
<map>它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。
<props>它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。

你可以使用<list><set>来连接任何 java.util.Collection 的实现或数组。

package com.igeek.xmlpropertydi;
import java.util.*;
public class CollectionBean {
	private List<String> list;
	private Set<Integer> set;
	private Map<String, Object>map;
	private Properties properties;//特殊类型的map,key和value都是String
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setSet(Set<Integer> set) {
		this.set = set;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	@Override
	public String toString() {
		return "CollectionBean [list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + "]";
	}
}
//测试类
package com.igeek.xmlpropertydi;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringText1 {
	@Test
	public void testArgs2() {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionBean collectionBean=(CollectionBean) ac.getBean("collectionBean");
		System.out.println(collectionBean);
	}
}
//aplicationContex.xml增加
<!-- 对集合赋值 -->
		<bean id="collectionBean" class="com.igeek.xmlpropertydi.CollectionBean">
			<!-- setter方式注入 -->
			<property name="list">
				<list>
					<value>tom</value>
					<value>jack</value>
				</list>
			</property>
			<property name="set">
				<set>
					<value>12</value>
					<value>46</value>
				</set>
			</property>
			<property name="map">
				<map>
					<entry key="name" value="张三"></entry>
					<entry key="age" value="23"></entry>
					<entry key="car" value-ref="car"></entry>
				</map>
			</property>
			<property name="properties">
				<props>
					<prop key="name">李四</prop>
					<prop key="age">33</prop>
				</props>
				
			</property>
		</bean>

//结果

CollectionBean [list=[tom, jack], set=[12, 46], map={name=张三, age=23, car=Car [id=1, name=宝马2代, price=99999.0]}, properties={name=李四, age=33}]

5.分模块开发 

如果有多个配置文件

第一种

//用逗号分隔开
@Test
	public void testArgs2() {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
		CollectionBean collectionBean=(CollectionBean) ac.getBean("collectionBean");
		System.out.println(collectionBean);
	}

第二种

//applicationContext.xml引入其他的配置文件
<import resource="applicationContext2.xml"/>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值