广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解Spring bean的注解注入之@Autowired的原理及使用
  • 666
分享到

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

2024-04-02 19:04:59 666人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

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

一、@Autowired

概念:

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

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


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

使用@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<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 容器中。

使用


@Service
public class StudentServiceImpl implements StudentService {
}

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


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

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

--结束END--

本文标题: 详解Spring bean的注解注入之@Autowired的原理及使用

本文链接: https://www.lsjlt.com/news/128457.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作