1.基于XML的bean注入
1.构造方法注入
2.Set方法注入
2.自动注入Bean[Spring的自动装配策略]
自动注入【自动装配】–Spring容器会根据配置文件中配置的元素,自动将依赖对象注入到调用者类中的成员变量中。
要使用自动装配,就需要配置 元素的 autowire 属性。
autowire 属性有五个值。
byName 根据 成员变量的 name 自动装配,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。
byType 根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。
constructor 根据构造方法的参数的数据类型,进行 byType 模式的自动装配。
autodetect 如果发现默认的构造方法,则用 constructor 模式,否则用 byType 模式。
被标记为过时方法,在Spring3.0之后已经不再支持。
no 默认情况下,不使用自动装配,Bean 依赖必须通过 ref 元素定义。
例如:测试byName【Spring配置文件总bean元素的id属性值,与调用者类中依赖对象的成员变量名称相同】
注意:需要为依赖对象提供setXX()
package com.wangxing.spring.byname;
//被调用者类
public class PersonDao {
public void select(){
System.out.println("查询所有用户信息");
}
}
package com.wangxing.spring.byname;
//调用者类
public class PersonService {
//定义依赖对象
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void testPersonService(){
personDao.select();
}
}
<?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">
<!-- 测试byname自动注入方式-->
<bean id="personDao" class="com.wangxing.spring.byname.PersonDao"></bean>
<bean id="personService" class="com.wangxing.spring.byname.PersonService" autowire="byName"></bean>
</beans>
例如:测试byType[Spring配置文件中bean元素的class属性值,与调用者类中依赖对象的成员变量的类型相同]
注意:需要为依赖对象提供setXX()
package com.wangxing.spring.bytype;
//被调用者
public class StudentDao {
public void selectStudent(){
System.out.println("查询所有学生信息");
}
}
package com.wangxing.spring.bytype;
//调用者
public class StudentService {
//定义依赖对象
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void testStudentService(){
studentDao.selectStudent();
}
}
<?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">
<!--测试bytype自动注入方式-->
<bean id="stuDao" class="com.wangxing.spring.bytype.StudentDao"></bean>
<bean id="studentService" class="com.wangxing.spring.bytype.StudentService" autowire="byType"></bean>
</beans>
例如:测试constructor【Spring配置文件中bean元素的class属性值,与调用者类中构造方法的参数类型相同{byType}】
package com.wangxing.spring.constructor;
//被调用者
public class UserDao {
public void selectUser(){
System.out.println("查询所有用户信息");
}
}
package com.wangxing.spring.constructor;
//调用者类
public class UserService {
private UserDao userDao;
public UserService(UserDao userDao){
this.userDao=userDao;
}
public void testUserService(){
userDao.selectUser();
}
}
<?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">
<!--测试constructor自动注入方式-->
<bean id="uDao" class="com.wangxing.spring.constructor.UserDao"></bean>
<bean id="userService" class="com.wangxing.spring.constructor.UserService" autowire="constructor"></bean>
</beans>
3.基于注解注入Bean
在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。
Java 从 JDK 5.0 以后,提供了 Annotation(注解)功能,Spring 也提供了对 Annotation 技术的全面支持。Spring3 中定义了一系列的 Annotation(注解)
1)@Component 创建对象的注解,使用时只需将该注解标注在相应类上即可。
没有@Component之前
public class Student{
}
Spring配置文件
<bean id=”student” class=”com.wangxing.spring.bean.Student”></bean>
有@Component之后
@Component----使用默认的对象名称【类名,首字母小写】
public class Student{
}
@Component(“stu”)—使用指定的对象名称
public class Student{
}
例如:
package com.wangxing.spring.annotation;
import org.springframework.stereotype.Component;
@Component("user")
public class UserBean {
public void testUserBean(){
System.out.println("UseerBean类的是实例方法");
}
}
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用context命名空间,通知spring扫描指定目录,进行注解的解析-->
<context:component-scan base-package="com.wangxing.spring.annotation"></context:component-scan>
</beans>
2)@Repository 标注数据访问接口实现类,其功能与 @Component 相同。
3)@Service标注业务访问接口实现类,其功能与 @Component 相同。
4)@Controller标注控制层实现类,其功能与 @Component 相同。
5)@Autowired 完成 Bean 的自动注入工作。默认按照bytype。
package com.wangxing.spring.annotation;
import org.springframework.stereotype.Component;
@Component("user")
public class UserBean {
public void testUserBean(){
System.out.println("UseerBean类的是实例方法");
}
}
package com.wangxing.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDao {
//定义依赖对象UserBean
@Autowired
private UserBean userBean;
public void testUserDao(){
System.out.println("数据访问层接口实现类");
userBean.testUserBean();
}
}
6)@Resource 完成 Bean 的自动注入工作。默认按照byname。
package com.wangxing.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("uDao")
public class UserDao {
//定义依赖对象UserBean
@Autowired
private UserBean user;
public void testUserDao(){
System.out.println("数据访问层接口实现类");
user.testUserBean();
}
}
package com.wangxing.spring.annotation.service;
import com.wangxing.spring.annotation.UserDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserService {
//定义依赖对象UserDao
@Resource
private UserDao userDao;
public void testUserService(){
System.out.println("业务层接口实现类");
userDao.testUserDao();
}
}
@autowired和@resource注解的区别?
①@Autowired与@Resource都可以用来装配bean,都可以写在字段或setter方法上
②@Autowired默认按类型装配,默认情况下必须要求依赖对象存在,如果要允许null值,可以设置它的required属性为false。如果想使用名称装配可以结合@Qualifier注解进行使用。
③@Resource,默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行名称查找。如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
推荐使用@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与Spring的耦合。
7)@Qualifier 与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。
public interfase UserDao{
}
@Repository(“studentUsetDao”)
public class StudentUserDao implements UserDao{
}
@Repository(“personUsetDao”)
public class PersonUserDao implements UserDao{
}
//业务类
public class UserServiceImple{
@Autowired
@Qualifier(“personUsetDao”)
private UserDao userDao;
}