Spring詳解(三)
DI依賴注入
spring動態(tài)的向某個對象提供它所需要的其他對象搔啊。這一點是通過DI(Dependency Injection洒试,依賴注入)來實現(xiàn)的规肴。比如對象A需要操作數(shù)據(jù)庫鲁冯,以前我們總是要在A中自己編寫代碼來獲得一個Connection對象垃帅,有了 spring我們就只需要告訴spring缨叫,A中需要一個Connection椭符,至于這個Connection怎么構(gòu)造,何時構(gòu)造耻姥,A不需要知道销钝。在系統(tǒng)運行時,spring會在適當?shù)臅r候制造一個Connection琐簇,然后像打針一樣蒸健,注射到A當中,這樣就完成了對各個對象之間關(guān)系的控制婉商。A需要依賴 Connection才能正常運行似忧,而這個Connection是由spring注入到A中的,依賴注入的名字就這么來的丈秩。
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Person {
private Long pid;
private String pname;
private Student students;
private List lists;
private Set sets;
private Map maps;
private Properties properties;
}
在 applicationContext.xml 中進行賦值
<!--
property是用來描述一個類的屬性
基本類型的封裝類盯捌、String等需要值的類型用value賦值
引用類型用ref賦值
-->
<bean id="person" class="com.my.di.Person">
<property name="pid" value="1"></property>
<property name="pname" value="vae"></property>
<property name="students">
<ref bean="student"/>
</property>
<property name="students" ref="student">//注入bean
<property name="students.name" value="jack">//級聯(lián)private Student students=new Student()
</property>
<property name="lists">
<list>
<value>1</value>
<ref bean="student"/>
<value>vae</value>
</list>
</property>
<property name="sets">
<set>
<value>1</value>
<ref bean="student"/>
<value>vae</value>
</set>
</property>
<property name="maps">
<map>
<entry key="m1" value="1"></entry>
<entry key="m2" >
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="p1">p1</prop>
<prop key="p2">p2</prop>
</props>
</property>
</bean>
<bean id="student" class="com.my.di.Student"></bean>
//利用 set 方法給對象賦值
@Test
public void testSet(){
//1、啟動 spring 容器
//2蘑秽、從 spring 容器中取出數(shù)據(jù)
//3饺著、通過對象調(diào)用方法
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person.getPname());//vae
}
利用 構(gòu)造函數(shù) 給屬性賦值
在實體類 Person.java 中添加兩個構(gòu)造方法:有參和無參
//默認構(gòu)造函數(shù)
public Person(){}
//帶參構(gòu)造函數(shù)
public Person(Long pid,Student students){
this.pid = pid;
this.students = students;
}
在 applicationContext.xml 中進行賦值
<!-- 根據(jù)構(gòu)造函數(shù)賦值 -->
<!--
index 代表參數(shù)的位置 從0開始計算
type 指的是參數(shù)的類型,在有多個構(gòu)造函數(shù)時,可以用type來區(qū)分肠牲,要是能確定是那個構(gòu)造函數(shù)瓶籽,可以不用寫type
value 給基本類型賦值
ref 給引用類型賦值
-->
<bean id="person_con" class="com.my.di.Person">
<constructor-arg index="0" type="java.lang.Long" value="1">
</constructor-arg>
<constructor-arg index="1" type="com.my.di.Student" ref="student_con"></constructor-arg>
</bean>
<bean id="student_con" class="com.my.di.Student"></bean>
//利用 構(gòu)造函數(shù) 給對象賦值
@Test
public void testConstrutor(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person_con");
System.out.println(person.getPid());//1
}
1、如果spring的配置文件中的bean中沒有<constructor-arg>該元素埂材,則調(diào)用默認的構(gòu)造函數(shù)
2、如果spring的配置文件中的bean中有<constructor-arg>該元素汤求,則該元素確定唯一的構(gòu)造函數(shù)
構(gòu)造方法通過類型注入
public class People {
private int id;
private String name;
private int age;
public People() {
super();
// TODO Auto-generated constructor stub
}
public People(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
}
<bean id="people3" class="com.java.entity.People">
<constructor-arg type="int" value="2"></constructor-arg>
<constructor-arg type="String" value="李四"></constructor-arg>
<constructor-arg type="int" value="22"></constructor-arg>
</bean>
自動裝配
byName通過名稱自動匹配
<?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.xsd"
default-autowire="byName">
<bean id="dog" class="com.java.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
<bean id="dog2" class="com.java.entity.Dog">
<property name="name" value="Tom"></property>
</bean>
<bean id="people1" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="張三"></property>
<property name="age" value="11"></property>
</bean>
</beans>
public class People {
private int id;
private String name;
private int age;
private Dog dog;
}
byType:通過類型進行自動匹配
constructor;根據(jù)類型俏险,自動注入
<?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.xsd"
default-autowire="constructor">
<bean id="dog" class="com.java.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
<bean id="people1" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="張三"></property>
<property name="age" value="11"></property>
</bean>
</beans>
public class People {
private int id;
private String name;
private int age;
private Dog dog;
public People() {
super();
// TODO Auto-generated constructor stub
}
public People(Dog dog) {
super();
System.out.println("constructor");
this.dog = dog;
}
}
bean之間的關(guān)系
繼承
<?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.xsd">
<bean id="dog" class="com.java.entity.Dog">
<property name="name" value="jack"></property>
</bean>
<bean id="abstractPeople" class="com.java.entity.People" abstract="true">
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean>
<bean id="zhangsan" parent="abstractPeople" depends-on="autority">
<property name="id" value="1"></property>
<property name="name" value="張三"></property>
</bean>
<bean id="lisi" parent="abstractPeople">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
<property name="dog" ref="dog"></property>
</bean>
<bean id="autority" class="com.java.service.Authority"></bean>
</beans>
public class T {
private ApplicationContext ac;
@Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void test1() {
People zhangsan=(People)ac.getBean("zhangsan");
System.out.println(zhangsan);
People lisi=(People)ac.getBean("lisi");
System.out.println(lisi);
}
}
依賴
<?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.xsd">
<bean id="dog" class="com.java.entity.Dog">
<property name="name" value="jack"></property>
</bean>
<bean id="zhangsan" parent="abstractPeople" depends-on="autority">
<property name="id" value="1"></property>
<property name="name" value="張三"></property>
</bean>
<bean id="lisi" parent="abstractPeople">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
<property name="dog" ref="dog"></property>
</bean>
<bean id="autority" class="com.java.service.Authority"></bean>
</beans>
使用注解,讓 Spring 容器幫我們產(chǎn)生 Person 對象
使用注解
在 applicationContext.xml 中引入命名空間
引入的命名空間扬绪,簡單來說就是用來約束xml文件格式的竖独。第一個 xmlns:context ,這表示標簽格式應(yīng)該是 <context:標
在 applicationContext.xml 文件中引入注解掃描器
<!-- 組件掃描挤牛,掃描含有注解的類 -->
<context:component-scan base-package="com.my.annotation"></context:component-scan>
base-package:表示含有注解類的包名
如果掃描多個包莹痢,則上面的代碼書寫多行,改變 base-package 里面的內(nèi)容即可!
@Component
如果一個類上加了@Component注解竞膳,就會進行如下的法則
如果其value屬性的值為""
@Component
public class Person {}
等價于
<bean id="person" class="..Person">
如果其value屬性的值不為""
@Component("p")
public class Person {}
等價于
<bean id="p" class="..Person">
在 Person 類中添加注解@Component
public void testAnnotation(){
//1航瞭、啟動 spring 容器
//2、從 spring 容器中取出數(shù)據(jù)
//3坦辟、通過對象調(diào)用方法
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person.getPname());
}