Spring续

我是一个从汽车行业转行IT的项目经理,我是Edward,如想了解更多,请关注我的公众号【转行项目经理的逆袭之路】。关于Spring容器,还有这些补充

1. 自动装配

Spring可以将容器中存在的任何对象自动赋值到某个属性或参数中去!这样的机制就称之为“自动装配”!简单的说,当某个属性或参数需要值时,只要这个值是在Spring容器中,Spring就可以为它自动赋值!

在需要被自动装配的属性之前添加@Autowired注解,即表示“希望Spring为该属性自动的装配值”,例如:

1
2
3
4
5
6
7
8
9
10
11
12
@Controller
public class UserLoginServlet {

    @Autowired
    private UserDao userJdbcDao;

    public void doPost() {
        System.out.println("UserLoginServlet.doPost()");
        userJdbcDao.login();
    }

}

当然,自动装配的前提是Spring容器中有合适的值!以上需要被装配的是UserDao类型的属性,那就要求某个类是实现了UserDao接口的,并且这个类是被Spring管理的(组件扫描+注解),例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.tedu.spring;

import org.springframework.stereotype.Repository;

@Repository
public class UserJdbcDao implements UserDao {
   
    public void reg() {
        System.out.println("UserJdbcDao.reg()");
    }
   
    public void login() {
        System.out.println("UserJdbcDao.login()");
    }

}

Spring框架在实现自动装配时,有2种装配模式:

  • byName模式:根据名称实现自动装配,在这种模式下,要求被装配的属性的名称,与Bean的名称是完全一致的!
  • byType模式:根据类型实现自动装配,在这种模式下,要求被装配的属性,在Spring容器中存在类型匹配的对象,如果被装配的属性是声明为父级类型,则Spring容器存在子级类型的对象即可装配成功,如果被装配的属性是声明为接口类型,则Spring容器存在实现类的对象即可装配成功!注意:使用这种模式时,如果在Spring容器中匹配类型的对象超过1个,就会装配失败!

当使用@Autowired注解时,其装配机制是:首先,会以byType模式在Spring容器中查找匹配类型的对象的数量,如果为0个,会报告错误,例如:

1
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.tedu.spring.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

如果有1个,则直接装配,无视名称是否匹配;

如果超过1个,会尝试通过byName模式来装配,如果名称匹配成功,则实现装配,如果名称均不匹配,则报告错误,例如:

1
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.tedu.spring.UserDao' available: expected single matching bean but found 2: userJdbcDao,userMybatisDao

当然,不可以存在2个对象使用相同的名称,否则,会因为名称冲突而出现错误:

1
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'a' for bean class [cn.tedu.spring.UserMybatisDao] conflicts with existing, non-compatible bean definition of same name and class [cn.tedu.spring.UserJdbcDao]

另外,还可以使用@Resource注解来修饰被装配的属性,例如:

1
2
3
4
5
6
7
8
9
10
11
12
@Controller
public class UserLoginServlet {

    @Resource // 这里既可以使用@Autowired,也可以使用@Resource
    private UserDao userMybatisDao;

    public void doPost() {
        System.out.println("UserLoginServlet.doPost()");
        userMybatisDao.login();
    }

}

使用@Resource时,必须将环境改为JDK 1.8,或添加Tomcat环境。

使用@Resource时,其执行效果与@Autowired是完全一致的!但是,其装配机制并不相同!@Resource的装配机制是:先尝试byName模式来装配,如果存在名称匹配的对象,则直接装配,如果不存在,则尝试byType模式来装配。

2. Spring阶段小结

  • 【理解】Spring框架的主要作用是:创建对象,管理对象;
  • 【理解】使用Spring的好处在于:降低类与类之间的耦合度,改为依赖于接口,实现解耦;
  • 【理解】Spring框架通过DI(Dependency Injection:依赖注入,通过Spring框架使得某个属性有值了)实现了IoC(Inversion of Control:控制反转,通过注解或某些做法将对象的控制权交给了Spring框架);
  • 【掌握】使用@Bean注解结合某个返回对象的方法,使得Spring框架管理相关的对象;
  • 【掌握】通过组件扫描(配置@ComponentScan注解的参数)和@Component/@Controller/@Service/@Repository注解,使得Spring框架管理类的对象;
  • 【了解】关于Spring管理的对象的作用域及生命周期;
  • 【掌握】通过Spring框架读取**.properties**配置文件中的信息;
  • 【掌握】通过@Autowired注解实现自动装配;
  • 【理解】Spring的自动装配的机制,及@Autowired@Resource这2个自动装配的注解的装配机制的区别。