资源描述
Spring DI
Made by Michael.Shi
281481047@
注入方法 2
1.Setter注入 2
2.Constructor注入 2
3.接口注入(了解) 2
简单属性注入 3
Bean的声明周期 3
1.Sigleton 3
2.Prototype 3
集合注入 3
AutoWire 4
byName 4
byType 5
LifeCycle 5
lazy-init 5
init-method和destory-method 5
Annotation 6
Autowire & Qualifier 6
1.普通xml文件 6
2.DTD BEAN 的xml文件 7
Resource 8
Component 8
全文都是以《学生课程管理》这个例子来进行说明,简单明了:
里面有studentDao,studentDaoImpl,Student,
StudentService
注入方法
1.Setter注入
在StudentService类中要有Student对象作为局部变量,以及它的setter方法
<bean id="studentService" class="com.primeton.spl.service.StudentService" lazy-init="true">
<property name="student">
<ref bean="student"/>
</property>
</bean>
<bean id="student" class="com.primeton.spl.bean.Student" lazy-init="true"/>
2.Constructor注入
<bean id="studentService" class="com.primeton.spl.service.StudentService" lazy-init="true">
<constructor-arg type="com.primeton.spl.bean.Student">
<ref bean="student"/>
</constructor-arg>
<constructor-arg index="1" ref="studentDao" />
</bean>
3.接口注入(了解)
简单属性注入
在Student类中要有id,name局部变量,以及他们的setter方法
<bean id="student" class="com.primeton.spl.bean.Student" lazy-init="true">
<property name="id">
<value>9257</value>
</property>
<property name="name">
<value>Michael</value>
</property>
</bean>
或者
<bean id="student" class="com.primeton.spl.bean.Student" lazy-init="true">
<constructor-arg value="Michael" index="1"/>
<constructor-arg type="java.lang.String">
<value>5555</value>
</constructor-arg>
</bean>
Bean的声明周期
1.Sigleton
默认(只new一个)
2.Prototype
每次new的都是新的
集合注入
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
AutoWire
byName
<beanid="studentService"
class="com.primeton.spl.service.StudentService"
autowire="byName">
<bean id="student"
class="com.primeton.spl.bean.Student"/>
public class StudentService
{
private Student student;
public void setStudent(Student student)
{
this.student = student;
}
}
类StudentService会根据自己类中局部变量的名字(student)在beans.xml中查找名字一样的bean(student),然后进行装配。
byType
<bean id="studentService"
class="com.primeton.spl.service.StudentService"
autowire="byType">
根据类型来进行装配
注意:如果在beans.xml中有两个是同一个类型的(例如都是Student)类型的bean,就会报错。
注意:byName和byType可以写在<beans default-autowire>标签上,就对所有的bean进行auto-wire。在<bean> 中的autowire=”default” 就是依据<beans>中的default-autowire设置。
LifeCycle
lazy-init
<bean id="studentDao" class="com.primeton.spl.dao.impl.StudentDaoImpl" lazy-init="true"/>
若不加,一旦ApplicationContext new 出来,所有的bean就都new出来;加上了,当getBean()的时候才会初始化。
注意:也可以在beans中设置<beans default-lazy-init="false" >
init-method和destory-method不要和prototype一起用
<bean id="studentService"
class="com.primeton.spl.service.StudentService"
init-method="init" destroy-method="destory">
初始化的时候调用init,容器关闭的时候调用destory
注意:一般非web程序,需要ClassPathXmlApplicationContext.destory( )才能调用destory.
顺序还是先new 这个bean,然后再调用init方法,最后执行destory方法。
如果scope是sigleton那么就算是两个bean对象也只会调用一次init;但是如果scope是prototype, 那么就会调用两次init。
Annotation
Autowire好像只能用ApplicationContext,用BeanFactory会抛出异常(不清楚是什么原因,有知道的情联系我)。
& Qualifier
1.普通xml文件
(1).需要在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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
(2).在StudentServcie类中加入@Autowired
①.可以在局部变量上面加(接口注入,不推荐)
@Autowired
private StudentDao studentDao;
②.可以在setter方法上面加
@Autowired
public void setStudentDao (StudentDao studentDao)
{
this. studentDao = studentDao;
}
③.可以在构造方法上面加
@Autowired
public StudentService(StudentDao studentDao)
{
this. studentDao = studentDao;
}
④.可以在普通方法上面加
@Autowired
public void addStudent(Student student)
{
System.out.println(student.getName());
}
2.DTD BEAN 的xml文件
Xml的文件头不变,只需要添加
<bean
Class
="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
即可。而不需要添加<context:annotation-config/>。因为<context:annotation-config/>的用途就是配置了AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor,
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
注意:如果autowire默认是byType,如果xml文件中有两个是同样的类型需要被注入(用非Annotation法会抛出异常),这里需要在参数上面添加@Qualifier
@Autowired
public void setStudentDao (@Qualifier("student2") StudentDao studentDao)
注意:@Qualifier("student2")中的"student2"是可以在xml的bean中配置的,
<bean id="student2222" class="com.primeton.spl.test.Student">
<qualifier value="student2" />
</bean>
或者
不用在bean中配置qualifire,默认与name,id相同。
注意:可以设置@Autowired(required=false),即表示不是必须要进行织入,如果没有响应的<bean>不会抛出异常
Resource
和autowire结果差不多,推荐使用Resource,可以在局部变量和setter方法上面进行@Resource
@Resource(name="student2")
public void addUser(Student student)
{
}
默认是byName,找不到的时候再byType,可以用(name=”student2”)来指定用哪一个同类型的bean。
Component
使用@Component能够在xml文件中不配置任何bean
1.在xml文件中添加
<context:component-scan base-package="com.primeton.spl.test"/>
其中base-package 指的是从哪个包中找你要的bean
这样就可以将xml文件中的bean都删除掉
2.在bean所代表的类上面加上@Component
@Component
public class Student
{
…
}
3.在需要这个bean的地方可以用@Resource进行注入
@Resource
public void addUser(Student student)
{
…
}
完成!
注意:一般推荐在@Component上面加上name:@Component("student"),然后再@Resource(name="student"),这样能够更加清楚、准确地进行定位。
提示:也可以在xml文件中删除studentService 的bean,在StudentService的类上加上@Component("studentService") ,最后在main方法中依然使用context.getBean(“studentService”);
注意:
1.可以在@Component上面添加@Scope("prototype")来指定bean的范围(prototype / sigleton)
2.在方法上面加@PostConstruct 可以指定bean的init方法;
在方法上面加@PreDestroy可以指定bean的destory方法。
展开阅读全文