详解Spring bean的注解注入之@Autowired的原理及使用

之前讲过bean注入是什么,也使用了xml的配置文件进行bean注入,这也是Spring的最原始的注入方式(xml注入).本文主要讲解的注解有以下几个:@Autowired、 @Service、@Repository、@Controller 、@Component、@Bean、@Configuration、@Resource ,需要的朋友可以参考下

一、@Autowired

概念:

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法

在使用@Autowired之前,我们对一个bean配置起属性时,用的是

使用@Autowired之后,我们只需要在需要使用的地方使用一个@Autowired 就可以了。

代码使用:

public interface StudentService { public boolean login(String username,String password); }

@Service public class StudentServiceImpl implements StudentService { @Override public boolean login(String username,String password) { if("crush".equals(username)&&"123456".equals(password)){ System.out.println("登录成功"); return true; } return false; } }

@Controller public class StudentController { @Autowired private StudentService studentService; public void login(){ boolean crush = studentService.login("crush", "123456"); if(crush){ System.out.println("crush"+"登录成功!!!!!"); }else{ System.out.println("登录失败"); } } }

测试:

@Test public void login(){ ApplicationContext applicationContext = new ClasspathXmlApplicationContext("application.xml"); StudentController student = applicationContext.getBean("studentController", StudentController.class); student.login(); }

我们在使用@Autowired 之后不用再去xml文件中继续配置了。

注意细节:

1、使用@Autowired的当前类也必须由spring容器托管(打@coponent、@Controller、@Service 、@repository)

2、不管是public 和 private 修饰的字段都可以自动注入

3、认情况下,使用@Autowired注解的属性一定要被装配,如果在容器中找不到该类型的bean注入,就会报错。如果允许不被装配就可以将@Autowired的required属性为false

4、@Autowired 是基于类型的注入,如果当前类型属性在容器中只有一个Bean, 那么属性名不限制,但一般建议遵循类名首字母小写的规则‘

5、如果当前属性类型在容器中有个多个Bean,那么必须要通过属性名 或者 @Qualifier 指定Bean name

6、@Autowired 可以打在XXX[] 、List上 ,此时会将容器中所有XXX类型的bean 都注入进去、且属性名没有约束,但是注意可以通过@Qualifier指定注入指定beanName的bean,属性名是没有约束作用的

7、@Autowired可以打在Map上,此时所有XXX类型的bean都会被注入 ,beanName 为key ,对象为value,但是注意可以通过@Qualifier指定注入指定beanName的bean,属性名是没有约束作用的

二、@Service、@Repository、@Controller、@Component

这几个注解的含义都是一样的,都是写在类上面或者接口上面,将自动注册spring容器

1、@Service用于标注业务层组件

2、@Controller用于标注控制层组件(如struts中的action)

3、@Repository用于标注数据访问组件,即DAO组件.

4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 注册到Spring 容器中。

使用

@Service public class StudentServiceImpl implements StudentService { }

@Controller public class StudentController { }

其作用就相当于在application.xml文件中 写以下代码

当然如果要使注解生效,必不可少的要加上这样一行扫描包的代码

三、@Bean

@Bean明确地指示了一种方法,什么方法呢――产生一个bean的方法,并且交给spring容器管理;从这我们就明白了为啥@Bean是放在方法的注释上了,因为它很明确地告诉被注释的方法,你给我产生一个Bean,然后交给spring容器,剩下的你就别管了

四、@Configuration

@Configuration用于定义配置类 这里只简单说明。

Spring 目前是有两种配置方式的,一种是xml文件配置加Java 代码,这种是从Spring出生的时候就有了,另一种是完全使用Java代码来进行配置及编写,这是在Spring 后面版本才出的。

从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化spring容器

这种方式更加受java程序员的喜欢。

@Configuration public class MyConfig { }

并且这种方式在后续的学习中,在Spring源码中使用的非常多。

五、@Resource

@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

@Autowired 与@Resource的区别

@Autowired原理

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...