1.基于XML的bean注入
1.构造方法注入
2.Set方法注入
2.自动注入Bean[Spring的自动装配策略]
自动注入【自动装配】–Spring容器会根据配置文件中配置的元素,自动将依赖对象注入到调用者类中的成员变量中。
要使用自动装配,就需要配置 元素的 autowire 属性。
autowire 属性有五个值。
例如:测试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();
}
}
package com.wangxing.spring.byname;
import com.wangxing.spring.bytype.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
PersonService personService=(PersonService)ac.getBean("personService");
personService.testPersonService();
}
}
例如:测试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 testStudentServiece(){
studentDao.selectStudent();
}
}
package com.wangxing.spring.bytype;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
StudentService studentService=(StudentService)ac.getBean("Student");
studentService.testStudentServiece();
}
}
例如:测试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();
}
}
package com.wangxing.spring.constructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
UserService userService=(UserService)ac.getBean("userService");
userService.testUserService();
}
}
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 com.sun.org.apache.xml.internal.res.XMLErrorResources_tr;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.stereotype.Component;
@Component("u")
public class UserBean {
public void testUserBean(){
System.out.println("UserBean类的实例方法");
}
}
package com.wangxing.spring.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
UserBean userBean=(UserBean)ac.getBean("u");
userBean.testUserBean();
}
}
<?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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
//定义依赖对象
@Autowired
private UserBean userBean;
public void testUserDao(){
System.out.println("这是你实现的类");
userBean.testUserBean();
}
}
package com.wangxing.spring.annotation;
import com.sun.org.apache.xml.internal.res.XMLErrorResources_tr;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.stereotype.Component;
@Component("u")
public class UserBean {
public void testUserBean(){
System.out.println("UserBean类的实例方法");
}
}
@Resource 完成 Bean 的自动注入工作。默认按照byname。
package com.wangxing.spring.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
// UserBean userBean=(UserBean)ac.getBean("u");
// userBean.testUserBean();
//UserDao userDao=(UserDao)ac.getBean("userDao");
// userDao.testUserDao();
UserService userService=(UserService)ac.getBean("userService");
userService.testUserService();
}
}
package com.wangxing.spring.annotation;
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注解的区别?
(1)@Autowired与@Resource都可以用来装配bean,都可以写在字段或setter方法上
(2)@Autowired默认按类型装配,默认情况下必须要求依赖对象存在,如果要允许null值,可以设置它的required属性为false。如果想使用名称装配可以结合@Qualifier注解进行使用。
(3)@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;
}