Spring种存取Bean的5种注解

这篇具有很好参考价值的文章主要介绍了Spring种存取Bean的5种注解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


先给大家看看我的命名规范
Spring种存取Bean的5种注解

存储Bean对象两种方式

1.添加一行bean

在spring-config.xml中添加一个bean 把对象注册给spring。
这种方法在有多个对象的时候 也得一行一行的存进去。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="user" class="User"></bean>
</beans>

2.使用注解的方式(5大注解)

2.第二种方法是使用注解 在有很多个对象需要存储的时候就不用一行一行注册了,使用前需要先在xml中配置一下扫描路径,这样注解才能识别出来类并存储到spring。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <content:component-scan base-package="com.demo.component"></content:component-scan>
</beans>

@Controller(控制器存储)

表示的是业务逻辑层,会判断前端发来的请求是否符合规范。

@Service(服务存储)

表示的是服务层,用来分配用户需要使用的功能。

@Repository(仓库存储)

表示的是持久层,直接和数据库交互,一张表一个@Repository。

@Component(组件存储)

表示的是公共工具层,用来添加一些公共的方法。

@Configuration(配置存储)

表示的是配置层,用来配置一些项目信息。

方法注解 @Bean

演示:
(配置了扫描路径可以使用@Bean)

package com.demo.component;

import com.demo.model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;

@Controller
public class BController {//只是我的包装类 目的是
    //获取bean 注释一下 使用Bean注解 需要配合5大注解一起用。
    //可以加入name参数 重新命名 因为如果有多个包装类都要使用这个Bean对象就有问题 名字都一样 到底拿哪一个Bean

    @Bean
    public Student student() {
        Student student = new Student();
        student.setId(1);
        student.setAge(18);
        student.setName("zyz");
        return student;
    }
}

使用@Bean注解来把Bean中的对象。 但是使用Bean注解的时候配合这五大注解来用。

在@Bean中 还有命名规范的问题。
Student类
Spring种存取Bean的5种注解

package com.demo.model;

public class Student {
    private  int id;
    private  String name;
    private  int age;

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

BController类
Spring种存取Bean的5种注解

package com.demo.component;

import com.demo.model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;

@Controller
public class BController {
    //获取bean 注释一下 使用Bean注解 需要配合5大注解一起用。
    //可以加入name参数 重新命名 因为如果有多个包装类都要使用这个Bean对象就有问题 名字都一样 到底拿哪一个Bean

    @Bean
    public Student student() {
        //构造对象的 伪代码
        Student student = new Student();
        student.setId(1);
        student.setAge(18);
        student.setName("zyz");
        return student;
    }
}

App类
Spring种存取Bean的5种注解
我们需要在APP类中启动Spring然后在BController类中拿到Bean对象(Student)。

ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Student student = context.getBean("student",Student.class);//拿的是BController中的对象
System.out.println(student);
  • 需要注意的是Bean的命名规则是默认为类名的小写
  • 如果类首字母大写/小写,那么命名就需要是小写的,比如类是Student 那么命名就要是student。
  • 如果类 首字母和第二个字母都是大写的话,命名就不变。
  • 如果@Bean方法注解 加入了name 那么就需要按照name来命名,不能使用原来默认的名字。代码如下👇
package com.demo.component;
import com.demo.model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;

@Controller
public class StudentBeans {//只是一个包装类 我目的是拿到Student对象
//获取bean 注释一下 使用Bean注解 需要配合5大注解一起用。
    //可以加入name参数 重新命名 因为如果有多个包装类都要使用这个Bean对象就有问题 名字都一样 到底拿哪一个Bean
    @Bean(name = {"s1","s2"})//s1s2命名 使用哪个都可以
    public Student st() {
        //构造对象的 伪代码
        Student student = new Student();
        student.setId(1);
        student.setAge(18);
        student.setName("zyz");
        return student;
    }
}
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

        //默认的命名是将类的首字母小写
        //1.如果类名首字母是大写或者小写 则都是小写 2.如果类名前两个字母是小写 则命名不变 这是JVM来规定的 不是Spring进行规定的 源码下在rt.jar包里 可以说明
        ArtController artController = context.getBean("artController",ArtController.class);
        System.out.println(artController.Say());

        Student student = context.getBean("s1",Student.class);//拿的是StudentBeans中的对象
        Student student1 = context.getBean("student",Student.class);//拿的是BController中的对象
        //如果对Bean注解 加入了name 那就只能使用那个name 不能使用原来的名字
        System.out.println(student);

获取Bean对象(三种)

获取Bean对象也叫对象装配,把对象拿出来放到类中,所以也叫对象注入,ApplicationContext 中getBean是在main把对象从Spring上下文中拿出来需要运行使用,

如果我们需要把一个类注入到另一个类中 就需要使用对象注入。
下面的例子是将StudentService类注入到StudentController类中

1.属性注入

使用@Autowired

package com.demo.controller;

import com.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class StudentController {
    //1.使用属性注入的方法来获取Bean 从Spring中 获取Bean
    //另一种获取Bean对象的方法(对象装配)
    @Autowired
    private StudentService studentService;


    public void Say() {
        //调用 service的方法
        studentService.Say();
    }
}

2.setter注入

package com.demo.controller;

import com.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class StudentController {
    //1.使用属性注入的方法来获取Bean 从Spring中 获取Bean
    //另一种获取Bean对象的方法(对象装配)
//    @Autowired
//    private StudentService studentService;

    //2.构造方法注入
    private StudentService studentService;
    
    @Autowired
    public  StudentController (StudentService studentService) {
        this.studentService = studentService;
    }


    public void Say() {
        //调用 service的方法
        studentService.Say();
    }
}

3.构造方法注入

是Spring推荐的使用方法 其中只有一个构造方法的时候@Autowired可以省略

package com.demo.controller;

import com.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class StudentController {
    //1.使用属性注入的方法来获取Bean 从Spring中 获取Bean
    //另一种获取Bean对象的方法(对象装配)
//    @Autowired
//    private StudentService studentService;

    //2.构造方法注入
    private StudentService studentService;
    
    @Autowired
    public  StudentController (StudentService studentService) {
        this.studentService = studentService;
    }


    public void Say() {
        //调用 service的方法
        studentService.Say();
    }
}

三种注入的优缺点(面试)

  • 属性注入:
  • 优点:使用方便,简单。
  • 缺点:1.不能注入final修饰的不可变对象。2.只用用于Ioc容器。3.更加不符合单一设计原则(类)
  • setter注入:
  • 优点:更加符合单一设计原则(方法)
  • 缺点:不能注入final修饰的不可变对象,注入的对象可以修改,因为set是一个普通方法,调用的时候就可以修改。
  • 构造方法注入(Spring4开始 官方的推荐):
  • 优点:可以注入不final修饰的不可变对象,意味着对象是不可被修改的, 通用性好,对象完全被初始化。
  • 缺点:不是那么方便。

在开发中使用属性注入比较多。

@Resource 和 @Autowired区别

相同点:都是依赖注入
不同点:1.@Resource是JDK提供的,@Autowired是Spring框架中的。2.@Resource源码中 有更多的参数和方法,@Autowired中只有一个required参数。2.@Resource不支持构造方法注入。4.同一类型多个Bean要返回的时候 可以使用@Resource中可以加入name @Resource(name=" “) 或者 使用@Qualifier(value=” ")
代码如下👇
Spring种存取Bean的5种注解
StudentBeans类注入到UserController中

package com.demo.component;
import com.demo.model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;

@Controller
public class StudentBeans {//只是一个包装类 我目的是拿到Student对象
        @Bean
    public Student st1() {

        Student student = new Student();
        student.setId(1);
        student.setAge(18);
        student.setName("zyz");
        return student;
    }

        @Bean
    public Student st2() {

        Student student = new Student();
        student.setId(1);
        student.setAge(18);
        student.setName("李四");
        return student;
    }

}

UserController类

package com.demo.controller;

import com.demo.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller
public class UserController {
    //将StudentBeans中的对象注入到这个类中 因为查找的时候先通过类型查找
    //StudentBeans中的Bean对象有两个 是相同类型的 这时候就分不清应该取哪个
    //就要使用@Resource 或者 @Qualifier来写入名称

    //1.
//    @Resource(name = "st1")
//    private Student student;
//
//    public Student getStudent() {
//        return student;
//    }

    //2.
    @Autowired
    @Qualifier(value = "st1")
    private Student student;

    public Student getStudent() {
        return student;
    }
}

在2个Bean对象类型相同的时候,对象注入的时候获取对象 就不知道应该拿哪一个对象,这时候就使用@Resource 或者 @Qualifier 可以添加想要获取的Bean对象的名称,这里拿的是st1。文章来源地址https://www.toymoban.com/news/detail-427751.html

到了这里,关于Spring种存取Bean的5种注解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring】-Spring中Bean对象的存取

    作者 :学Java的冬瓜 博客主页 :☀冬瓜的主页🌙 专栏 :【Framework】 主要内容 :往spring中存储Bean对象的三大方式:XML方式(Bean标签);五大类注解;方法注解。从spring中取对象的两种方式:基本方法、注解方法(属性注入、set注入、构造方法注入)。 在Spring中,Bean的装配方式

    2024年02月12日
    浏览(21)
  • Spring 用注解更简单存取对象

    ​ 上一篇文章是最原始的创建使用,这篇主要是讲 Spring 更简单的存储和读取对象的核心是使用 注解 ,也是日常生活企业用的最多的方法 “注解” 所以这篇的内容是很重要的 !!! 1.1 前置工作 ​ 需要再 Spring 的配置文件中设置组件 Component 的根路径 这是很重要的一步,在

    2024年02月13日
    浏览(26)
  • Spring 更简单的读取和存储对象、使用注解存取对象

    我们知道物件都会随着时代的发展,越变越简单的。Spring 框架也不意外;我们在之前存储Bean对象是在配置文件中添加 bean/bean 来存入Bean对象到Spring当中的,使用那个类对象就要存入那个,一个两个类对象还好,如果需要的对象多了,就会非常麻烦。现在,随着 Spring 发展不用

    2024年02月11日
    浏览(36)
  • 【Spring】基于注解方式存取JavaBean:Spring有几种注入方式?有什么区别?

     Hello,我是小黄。众所周知,Spring是一个开源的Java应用程序框架,其中包括许多通过注解实现依赖注入的功能。Spring提供了多种注入方式,可以满足不同的需求和场景。常见的注入方式包括构造函数注入、Setter方法注入和属性注入。不同的注入方式有不同的适用场景和优缺

    2024年02月11日
    浏览(33)
  • spring-注解开发bean

    使用@Component定义bean 在配置文件中通过组建扫描加载bean 3.也可以通过不要配置文件,定义类,使用@Configuration,来代替配置文件 基于注解定义bean 1.@component,大部分的bean都可以通过这个来定义 1.1@Controller,控制层 1.2@Service,服务层 1.3@Repository,数据层 2.对于纯注解的开发,还

    2024年02月13日
    浏览(31)
  • 11Spring IoC注解式开发(上)(元注解/声明Bean的注解/注解的使用/负责实例化Bean的注解)

    注解的存在主要是为了简化XML的配置。Spring6倡导全注解开发。 注解开发的优点 :提高开发效率 注解开发的缺点 :在一定程度上违背了OCP原则,使用注解的开发的前提是需求比较固定,变动较小。 自定义一个注解: 该注解上面修饰的注解包括:Target注解和Retention注解,这两个注

    2024年01月21日
    浏览(34)
  • Spring基于注解管理bean及全注解开发

    Spring是一款主流的Java EE 轻量级开源框架,目的是用于简化Java企业级引用的开发难度和开发周期。从简单性、可测试性和松耦合度的角度而言,任何Java应用都可以从Spring中受益。Spring框架提供自己提供功能外,还提供整合其他技术和框架的能力。 Spring自诞生以来备受青睐,

    2024年02月14日
    浏览(30)
  • Spring使用注解管理Bean

    引入lib包 Spring对Bean管理的常用注解 @Component组件(作用在类上) Spring中提供了@Component的三个衍生注解:(功能在目前为止是一致的) @Controller WEB层 @Service 业务层 @Repository 持久层 属性注入的注解:(使用注解注入的方式,可以不用提供set方法) @Value  用于注入普通类型 @Autowired  自动装

    2024年01月17日
    浏览(31)
  • Spring使用注解存储Bean对象

    在前一篇博客中( Spring项目创建与Bean的存储与读取(DL))介绍的是通过配置文件注册对象从而存储到 Spring 中,这种方式其实还是挺繁琐的。 实际上,在使用学习使用 Spring过程中,当我们要实现一个功能的时候,先应该考虑的是有没有相应的注解是实现对应功能的,Spring 中

    2024年02月16日
    浏览(32)
  • Spring 非自定义Bean注解

    1.概述 在xml中配置的Bean都是自己定义的, 例如:UserDaolmpl,UserServicelmpl。但是,在实际开发中有些功能类并不是我们自己定义的, 而是使用的第三方jar包中的,那么,这些Bean要想让Spring进行管理,也需要对其进行配置配置非自定义的Bean需要考虑如下两个问题: 被配置的B

    2024年02月13日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包