Spring-----注解实现自动装配(自动匹配属性类型) @Autowired 和 autowire

1.注解:实现Bean的属性装配

实现注解开发自动装配步骤:

   1.导入XML 配置文件头部 约束   增加命名空间  注解支持

 2.注解的支持   <context:annotation-config>   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.xsd
  http://www.springframework.org/schema/context //增加支持注解的约束
   https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> //引入注解机制

 3.使用注解@Autowired  注入    。        不需要set方法,通过反射机制给属性赋值,前提是bean对象在XML中创建。

@Autowired   匹配byName byType  仍无法匹配加注解@Qualifier(value=“”)指定Bean的ID。

@Resource(name="")  另一种装配注解, 先通过byName,在通过byType,最后通过指定属性name值来匹配装zai,前提要定义了bean在XML中)最后崩溃。

@Qualifier(value="") 注解自动装配类太多不能实现,用注解指定其唯一值,类似起别名和@Autowired配合使用.(还可以作为给bean 起别名,)

   

原文地址:https://www.cnblogs.com/chencn/p/12331787.html