---
title: "spring ioc之最基础最简单最明了的annotation注入"
author: "wengcang"
date: "2015-04-03T17:28:17Z"
tags: ["Bean", "IOC", "Spring", "annotation", "注解"]
categories: ["开源软件"]
slug: "spring-ioc-annotation"
draft: false
lastmod: "2015-04-03T18:46:02Z"
id: 2199218
url: "https://xiaoshifanyan.iteye.com/blog/2199218"
---

1、配置applicationContext.xml开启注解 \[code="xml"] \[/code] 2、自动注册bean注解 \[code="xml"] \[/code] 3、将该类自动注册为bean并将id命名为myService,如果不指定默认为第一个字母小写 \[code="java"] /\** * @Component 标志该类为Spring组件 * @Controller 标志该类为Spring MVC Controller * @Repository 标志该类为数据仓库 * @Service 标志该类为服务 \*/ // @Service("myService") public class MyServiceImpl implements MyService{ ................ } \[/code] 4、在控制器中注入 \[code="java"] //如果是通过@Component注入普通bean， //那么需要在普通bean中加入默认的构造器，否则将无法实力化bean public class MyControoler{ @Resource private MyService myService; } \[/code]