• Bean管理注解


    目录

    一、用于创建对象注解

    二、注入数据的注解


    SpringBean管理注解: 为了语义化。

    一、用于创建对象注解

    @Component:除三层以外的其它层:实体层;
    @Controller:控制层/action/servlet;
    @Service:业务逻辑层service 的实现类;
    @Repository:数据访问层/持久层 dao/mapper。

    案例:

    1.加入注解@Component

    Student.java

    1. package com.zking.entity;
    2. import lombok.Data;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.objenesis.instantiator.perc.PercInstantiator;
    5. import org.springframework.stereotype.Component;
    6. @Data
    7. @Component
    8. public class Student {
    9. private String name;
    10. private String sex;
    11. private Integer sage;
    12. }

    2.加入扫描包

    spring.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. 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">
    6. <context:component-scan base-package="com.zking"/>
    7. beans>

    3.测试

    Test.java

    1. package com.zking.test;
    2. import com.sun.xml.internal.fastinfoset.util.DuplicateAttributeVerifier;
    3. import com.zking.entity.Student;
    4. import com.zking.entity.Teacher;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. import javax.rmi.CORBA.Stub;
    8. public class Test {
    9. public static void main(String[] args) {
    10. // 1.启动spring容器
    11. ApplicationContext applicationcontext = new ClassPathXmlApplicationContext("spring_03.xml");
    12. // 2.得到bean对象
    13. Student student = (Student)applicationcontext.getBean("student");
    14. System.out.println("student = " + student);
    15. }
    16. }

    控制台打印:

    注:可以手动指定注解对象名bean里面的名字;

    @Component(value = "s")

    注:给注解属性赋值,如果只有一个属性,且属性名是value,则可以省略value不写。

    例:

    @Component("s")
    

    1.1.@Bean注解
    @Bean:加在方法上面,告诉该方法把返回的对象交给spring容器管理。


    bean对象名默认是当前方法名;例:createTeacher。

    也可以指定bean里面的名字,name="" ;

    案例:

    Student.java

    1. package com.zking.entity;
    2. import lombok.Data;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.objenesis.instantiator.perc.PercInstantiator;
    5. import org.springframework.stereotype.Component;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.stereotype.Repository;
    8. import org.springframework.stereotype.Service;
    9. @Data
    10. @Component
    11. public class Student {
    12. private String name;
    13. private String sex;
    14. private Integer sage;
    15. @Bean// 加在方法上面,告诉该方法把返回的对象交给spring容器管理
    16. public Dog createDog(){
    17. return new Dog();
    18. }
    19. }

    Dog.java

    1. package com.zking.entity;
    2. import lombok.Data;
    3. @Data
    4. public class Dog {
    5. private String name;
    6. public Dog(){
    7. System.out.println("我是狗类的无参构造方法!");
    8. }
    9. }

    spring.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. 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">
    6. <context:component-scan base-package="com.zking"/>
    7. beans>

    测试

    1. package com.zking.test;
    2. import com.sun.xml.internal.fastinfoset.util.DuplicateAttributeVerifier;
    3. import com.zking.entity.Student;
    4. import com.zking.entity.Teacher;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. import javax.rmi.CORBA.Stub;
    8. public class Test {
    9. public static void main(String[] args) {
    10. // 1.启动spring容器
    11. ApplicationContext applicationcontext = new ClassPathXmlApplicationContext("spring_03.xml");
    12. System.out.println(applicationcontext.getBean("createDog"));
    13. }
    14. }

    控制台打印:

    二、注入数据的注解

    2.1.@Value:给基本数据类型注入数据(八大基本数据类型+String);

    给Bean类型注入数据:
    2.2.@Autowired:通过类型从IOC容器里面找bean对象(同类型的只能有一个Bean对象);


    相当于new了对象。

    2.3.@Qualifier:当Spring容器同类型的Bean对象存在多个使用;

    @Autowired和@Qualifier结合起来使用:

    例:如果IOC容器里面有两个Dog类型的Bean,名字分别为dog1、dog2,此时IOC容器不知道实例化哪个对象,就要通过@Qualifier指定对象名。

    @Autowired

    @Qualifier("studentDao")

    private StudentDog studentDao;

    private StudentDog studentDao2;

    2.4.@Resource:先通过name注入,找不到再通过类型。

    2.4.1.通过name注入;

    @Resource(name = "studentDao")

    private StudentDao studentDao;

    2.4.2.再通过类型注入;

    @Reource

    private StudentDog studentDao;

    @Autowired和@Resource的不同:

    1.查找Bean的方式不同;

    2.如果使用的是spring框架用:@Autowired;

    如果使用的是javax:@Resource。


    2.5.用于改变作用的范围@Scope

    2.6.用于定义生命周期@PostConstruct(bean被创建的时候执行)&@PreDestroy(bean被销毁的时候执行);


    2.7.懒加载注解@Lazy

    2.8.注解@ComponentScan

    @ComponentScan 相当于扫描包

    单个值:

     多个值:

     案例:

    完。

  • 相关阅读:
    DPDK的VFIO
    doT.js模板学习笔记
    区块链论文最新录用A会-ISSTA 2024 共8篇
    低代码维格云甘特视图入门教程
    SpringBoot+Vue项目宠物猫店管理系统的设计与实现
    远程端点管理和安全性
    异步编程 - 05 基于JDK中的Future实现异步编程(中)_CompletableFuture
    Android 13.0 SystemUI下拉状态栏背景增加高斯模糊背景
    Java多线程使用 CountDownLatch等待其他线程执行完成
    【HOG】HOG 之FPPW
  • 原文地址:https://blog.csdn.net/weixin_62332711/article/details/125837373