Spring详解(三)
DI依赖注入
spring动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。比如对象A需要操作数据库,以前我们总是要在A中自己编写代码来获得一个Connection对象,有了 spring我们就只需要告诉spring,A中需要一个Connection,至于这个Connection怎么构造,何时构造,A不需要知道。在系统运行时,spring会在适当的时候制造一个Connection,然后像打针一样,注射到A当中,这样就完成了对各个对象之间关系的控制。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">//级联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 容器中取出数据
//3、通过对象调用方法
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person.getPname());//vae
}
利用 构造函数 给属性赋值
在实体类 Person.java 中添加两个构造方法:有参和无参
//默认构造函数
public Person(){}
//带参构造函数
public Person(Long pid,Student students){
this.pid = pid;
this.students = students;
}
在 applicationContext.xml 中进行赋值
<!-- 根据构造函数赋值 -->
<!--
index 代表参数的位置 从0开始计算
type 指的是参数的类型,在有多个构造函数时,可以用type来区分,要是能确定是那个构造函数,可以不用写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>
//利用 构造函数 给对象赋值
@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>该元素,则调用默认的构造函数
2、如果spring的配置文件中的bean中有<constructor-arg>该元素,则该元素确定唯一的构造函数
构造方法通过类型注入
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;根据类型,自动注入
<?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之间的关系
继承
<?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 容器帮我们产生 Person 对象
使用注解
在 applicationContext.xml 中引入命名空间
引入的命名空间,简单来说就是用来约束xml文件格式的。第一个 xmlns:context ,这表示标签格式应该是 <context:标
在 applicationContext.xml 文件中引入注解扫描器
<!-- 组件扫描,扫描含有注解的类 -->
<context:component-scan base-package="com.my.annotation"></context:component-scan>
base-package:表示含有注解类的包名
如果扫描多个包,则上面的代码书写多行,改变 base-package 里面的内容即可!
@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 容器中取出数据
//3、通过对象调用方法
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person.getPname());
}