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

一、@Autowired

概念:

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

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

<property name="属性名" value=" 属性值"/>    

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

代码使用:

public interface StudentService {    public boolean login(String username,String password);}
@Servicepublic 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;    }}
@Controllerpublic 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<String,XXX>上,此时所有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 容器中。

使用

@Servicepublic class StudentServiceImpl implements StudentService {}
@Controllerpublic class StudentController {}

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

<bean id="studentServiceImpl" class="com.crush.service.impl.StudentServiceImpl"/><bean id="studentController" class="com.crush.controller.StudentController"/>

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

<!--让com.crush包下类中使用 spring的注解生效--><context:component-scan base-package="com.crush"/>

三、@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程序员的喜欢。

@Configurationpublic 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原理

到此这篇关于详解Spring bean的注解注入之@Autowired的原理及使用的文章就介绍到这了,更多相关Autowired原理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

走马观花之外,这才是深入体验,探索自我的最佳时间,

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

相关文章:

你感兴趣的文章:

标签云: