Spring IOC相关注解运用(上篇)

 更新时间:2023年05月05日 11:39:08   作者:会洗碗的CV工程师  
这篇文章主要介绍了Spring IOC相关注解的运用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

前言

注解配置和xml配置对于Spring的IOC要实现的功能都是一样的,只是配置的形式不一样。
准备工作:

  • 创建一个新的Spring项目。
  • 编写pojo,dao,service类。
  • 编写空的配置文件,如果想让该文件支持注解,需要在bean.xml添加新的约束:
<?xml version="1.0" encoding="UTF-8" ?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="com.example"/>
    <context:property-placeholder location="db.properties"/>
 
</beans>

一、@Component

@Component可以代替bean标签

  • 作用:用于创建对象,放入Spring容器,相当于 <bean id="" class="">
  • 位置:类上方
  • 注意:@Component 注解配置bean的默认id是首字母小写的类名。也可以手动设置bean的id值。
// 此时bean的id为studentDaoImpl
@Component
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模拟根据id查询学生
    return new Student(1,"程序员","北京");
 }
// 此时bean的id为studentDao
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模拟根据id查询学生
    return new Student(1,"程序员","北京");
 }
}

二、@Repository、@Service、@Controller

作用:这三个注解和@Component的作用一样,使用它们是为了区分该类属于什么层。
位置:

  • @Repository用于Dao层
  • @Service用于Service层
  • @Controller用于Controller层
@Repository
public class StudentDaoImpl implements StudentDao{}
@Service
public class StudentService {}

三、@Scope

作用:指定bean的创建策略
位置:类上方
取值:singleton prototype request session globalsession 

@Service
@Scope("singleton")
public class StudentService {}

测试一下: 

    @Test
    public void t1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
        System.out.println(studentDao);
        StudentService service1 = (StudentService) ac.getBean("studentService");
        System.out.println(service1.hashCode());
        StudentService service2 = ac.getBean("studentService",StudentService.class);
        System.out.println(service2.hashCode());
    }

OK,确实可以

四、@Autowired

作用:从容器中查找符合属性类型的对象自动注入属性中。用于代替 <bean> 中的依赖注入配置。
位置:属性上方、setter方法上方、构造方法上方。
注意:@Autowired 写在属性上方进行依赖注入时,可以省略setter方法。

@Component
public class StudentService {
  @Autowired
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}
 
// 测试方法
@Test
public void t2(){
  ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  StudentService studentService = (StudentService) ac.getBean("studentService");
  System.out.println(studentService.findStudentById(1));
}

测试结果:

OK,也是可以的 

五、@Qualifier

作用:在按照类型注入对象的基础上,再按照bean的id注入。
位置:属性上方
注意:@Qualifier必须和@Autowired一起使用。

如下

@Component
public class StudentService {
  @Autowired
  @Qualifier("studentDaoImpl2")
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}

六、@Value

作用:注入String类型和基本数据类型的属性值。
位置:属性上方

以下说明一下用法:

1. 直接设置固定的属性值

    @Value("1")
    private int count;
 
    @Value("hello")
    private String str;

2. 获取配置文件中的属性值

编写配置文件db.properties

jdbc.username=root
jdbc.password=123456

spring核心配置文件(bean.xml)扫描配置文件

<context:property-placeholder location="db.properties"/>

注入配置文件中的属性值

    @Value("${jdbc.username}")
    private String username;
 
    @Value("${jdbc.password}")
    private String password;

3. 测试结果

测试方法

    // 测试注解Value
    @Test
    public void t3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = ac.getBean("studentService",StudentService.class);
        System.out.println(service);
    }

运行结果

OK,应该和上面设置的值一样,说明可以使用,本篇就介绍到这几个注解了,下篇会介绍完接下来的注解。 

以上就是Spring IOC相关注解运用(上篇)的详细内容,更多关于Spring IOC注解的资料请关注脚本之家其它相关文章!

相关文章

  • Java局部打印效果不同问题解决方案

    Java局部打印效果不同问题解决方案

    这篇文章主要介绍了Java局部打印效果不同问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 使用SpringBoot 工厂模式自动注入到Map

    使用SpringBoot 工厂模式自动注入到Map

    这篇文章主要介绍了使用SpringBoot 工厂模式自动注入到Map,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • SpringBoot任务之详解邮件任务

    SpringBoot任务之详解邮件任务

    今天给大家整理的文章是SpringBoot邮件任务的相关知识点,文中有非常详细的介绍及代码示例,对正在学习SpringBoot任务的小伙伴们很有帮助,需要的朋友可以参考下
    2021-06-06
  • Spring @Bean注解配置及使用方法解析

    Spring @Bean注解配置及使用方法解析

    这篇文章主要介绍了Spring @Bean注解配置及使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 深入理解Java8新特性之接口中的默认方法和静态方法

    深入理解Java8新特性之接口中的默认方法和静态方法

    从Java8开始,程序允许在接口中包含带有具体实现的方法,使用default修饰,这类方法就是默认方法。默认方法在接口中可以添加多个,并且Java8提供了很多对应的接口默认方法,接下来让我们一起来看看吧
    2021-11-11
  • tk.Mybatis 插入数据获取Id问题

    tk.Mybatis 插入数据获取Id问题

    本文主要介绍了tk.Mybatis 插入数据获取Id问题,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • Java文件管理操作的知识点整理

    Java文件管理操作的知识点整理

    这篇文章主要为大家详细介绍了Java中文件管理操作的一些知识点和实现方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-09-09
  • MyBatis中使用$和#所遇到的问题及解决办法

    MyBatis中使用$和#所遇到的问题及解决办法

    这篇文章主要介绍了MyBatis中使用$和#所遇到的问题及解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-08-08
  • 详解在SpringBoot中使用MongoDb做单元测试的代码

    详解在SpringBoot中使用MongoDb做单元测试的代码

    这篇文章主要介绍了详解在SpringBoot中使用MongoDb做单元测试的代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Spring Boot中如何使用Swagger详解

    Spring Boot中如何使用Swagger详解

    Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful风格的Web服务,这篇文章主要给大家介绍了关于Spring Boot中如何使用Swagger的相关资料,需要的朋友可以参考下
    2021-08-08

最新评论