当前位置: 首页 > article >正文

Spring IOC容器Bean对象管理-Java Config方式

        IOC容器Bean对象管理有纯配置文件方式、注解方式、配置类+注解方式,本文讨论的是配置类+注解方式即Java Config的方式管理IOC容器的Bean对象,此种方式没有xml文件配置,使用配置类+注解实现,常用的注解如下:

  1. @Configuration 作用在配置类上,功能相当于spring的applicationContext.xml文件
  2. @Bean 作用在方法上,方法返回值对象放入IOC容器,bean id=方法名
  3. @ComponentScan(basePackages = "com.text")  功能相当于applicationContext.xml中的扫描包配置

示例代码如下:

配置类SpringConfig:

package com.text.config;
import com.text.controller.StudentController;
import com.text.service.StudentService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration //配置类,相当于spring的applicationContext.xml文件
@ComponentScan(basePackages = "com.text") //相当于applicationContext.xml中的扫描包配置
public class SpringConfig {
    @Bean //方法返回的studentController对象放入IOC容器,bean id=方法名
    public StudentController studentController(StudentService studentService) {
        StudentController controller = new StudentController();
        controller.setStudentService(studentService);
        return controller;
    }
}

组件类:

package com.text.dao.impl;
import com.text.dao.StudentDao;
import org.springframework.stereotype.Repository;

@Repository
public class StudentDaoImpl implements StudentDao {

}
package com.text.service.impl;
import com.text.dao.StudentDao;
import com.text.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;
    public StudentDao getStudentDao() {
        return studentDao;
    }
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
}
package com.text.controller;
import com.text.service.StudentService;

public class StudentController {
    private StudentService studentService;
    public StudentService getStudentService() {
        return studentService;
    }
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
}

测试类:

package com.text;
import com.text.config.SpringConfig;
import com.text.controller.StudentController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        //创建基于注解的Spring应用上下文,以纯Java注解的方式来替代XML配置文件来创建和管理Spring容器上下文。
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for(String beanName:beanDefinitionNames) {
            System.out.println(beanName + ":" + context.getBean(beanName));
        }
        StudentController studentController = (StudentController)context.getBean("studentController");
        System.out.println(studentController);
        System.out.println(studentController.getStudentService());
        /**
         * 输出:
         * springConfig:com.text.config.SpringConfig$$EnhancerBySpringCGLIB$$5adc8a5@23282c25
         * studentDaoImpl:com.text.dao.impl.StudentDaoImpl@7920ba90
         * studentServiceImpl:com.text.service.impl.StudentServiceImpl@6b419da
         * studentController:com.text.controller.StudentController@3b2da18f
         * com.text.controller.StudentController@3b2da18f
         * com.text.service.impl.StudentServiceImpl@6b419da
         */
    }
}

运行结果分析:

  1. @ComponentScan(basePackages = "com.text")  说明com.text包项下的通过注解标识的StudentDaoImpl、StudentServiceImpl 对象已被实例化并放入了IOC容器中
  2. @Configuration标识的SpringConfig类在IOC容器启动时也被对象实例化出来-springConfig
  3. @Bean标识的方法,其返回值对象也会被放入IOC容器中,bean id为方法名;方法参数的注入从IOC容器中寻找id为studentService的对象(系统只有id=studentServiceImpl的对象),找不到则按照StudentService类型寻找,找到StudentService类型的studentServiceImpl,注入到方法参数中,进而赋值到studentController 的属性studentService
  4. 从运行结果可以看出,上述实例的对象都是单例,打印出的对象地址都相同

http://www.kler.cn/news/317801.html

相关文章:

  • 设计模式推荐网站
  • Linux文件I/O
  • 设计模式——对象池模式
  • JavaWeb--小白笔记07-2:超链接以及servlet对表单数据的完整处理
  • QT| QT配置CUDA
  • 9.23工作笔记
  • C++之STL—List 链表
  • 探索未来科技的无限可能:IT领域的深度剖析与趋势展望
  • Java实现零工市场数字化
  • 利士策分享,如何制定合理的工作时长:寻找生活与工作的平衡点
  • 【论文阅读】PolarNet: 3D Point Clouds for Language-Guided Robotic Manipulation
  • ES6 — Promise基础用法(resolve、reject、then、catch,all)
  • Java日期格式化注解@DateTimeFormat和@JsonFormat
  • (14)关于docker如何通过防火墙做策略限制
  • Python获取异常的具体信息
  • js计算倒计时
  • 鸿蒙OpenHarmony【小型系统内核(用户态启动)】子系统开发
  • 大数据Flink(一百二十):Flink SQL自定义函数(UDF)
  • 30. RabbitMQ消息丢失
  • Codigger SIDE:Nvim扩展,重新定义编程体验
  • 【代码随想录训练营第42期 Day59打卡 - 图论Part9 - Bellman-Ford算法
  • 思维链在论文写作中的应用:借助ChatGPT构建完整、清晰的论证
  • 计算机毕业设计 基于Python内蒙古旅游景点数据分析系统 Django+Vue 前后端分离 附源码 讲解 文档
  • Java 怎么调摄像头
  • UEFI EDK2框架学习 (一)
  • AtCoder Beginner Contest 372(C++实现)
  • 笔试题目 :状态检测11011011
  • JavaScript 可视化
  • 【软件文档】项目质量保证计划书(Word原件)
  • 【Kubernetes】常见面试题汇总(三十三)