---
title: "2.1Spring在xml中配置autowire注入对象"
author: "garrincha"
date: "2014-08-26T21:14:18Z"
tags: ["Bean", "Spring", "XML", "autowire", "注入"]
categories: ["编程语言"]
slug: "2-1spring-xml-autowire"
draft: false
lastmod: "2014-08-26T21:14:18Z"
id: 2109324
url: "https://garrincha.iteye.com/blog/2109324"
---

书接上回 \[url=/admin/blogs/2109297]上一篇在此\[/url] 上一篇的方式是通过bean中的id制定，然后把属性的对象注入进去的。这里我们采用xml中写autowire的方式注入 上一篇代码不变 修改xml文件如下 \[code="xml"] \[/code] 我们把property标签注释掉，不使用制定名字的方式注入。在userService的bean上写autowire=“byName”，使用名字进行自动注入，这里的名字是指，id=“dao”的bean和userService中的成员变量dao的名字相同。所以可以注入。 修改了xml之后自行测试一下吧。