注解的配置格式:

<?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:annotation-config></context:annotation-config>

如果@Autowired自动装配环境比较复杂,无法通过一个@Autowired完成的时候可以使用@Qualifier(valuer ="dog1") 来配合使用,指定一个对象的注入。

@Autowired和@Resource的区别:

  • 都是通自动装配的,可以放在属性字段上
  • @Autowired通过bytype实现的,并且要求这个对象存在!
  • @Resource默认通过byname实现的,再通过bytype,两个都找不到报错!
  • 执行顺序不同,Autowired通过bytype实现