依星源码资源网,依星资源网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

【好消息,好消息,好消息】VIP会员可以发表文章赚积分啦 !
查看: 122|回复: 0

Spring Bean的声明方式

[复制链接] 主动推送

1万

主题

1万

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
15435
发表于 2025-4-15 12:07:42 | 显示全部楼层 |阅读模式
Spring Bean的声明方式
一、环境说明
  • 项目结构

    Spring Bean的声明方式

    Spring Bean的声明方式
  • StudentService
    1. package com.cookie.service;

    2. /**
    3. * @author cxq
    4. * @version 1.0
    5. * @date 2020/7/14 9:18
    6. * @desc
    7. */
    8. public interface StudentService {

    9.     void add();
    10. }

    复制代码

  • StudentServiceImpl
    1. package com.cookie.service.impl;

    2. import com.cookie.service.StudentService;
    3. import org.springframework.stereotype.Component;

    4. /**
    5. * @author cxq
    6. * @version 1.0
    7. * @date 2020/7/14 9:20
    8. * @desc
    9. */
    10. public class StudentServiceImpl implements StudentService {

    11.     public void add() {
    12.         System.out.println(" add student ... ");
    13.     }
    14. }

    复制代码

  • applicationContext.xml核心配置文件
    1. <?xml version="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
    6.         https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


    7. </beans>
    复制代码


二、XML
  • 以bean的方式在核心配置文件中声明
    1. <!--
    2.       xml声明
    3.         id : bean的唯一标识
    4.         class:bean所在的Java类的全类名
    5.     -->
    6. <bean id="studentService" class="com.cookie.service.impl.StudentServiceImpl" />
    复制代码

  • 通过ClassPathXmlApplicationContext读取配置文件
    1. /**
    2.      * 基于xml声明bean
    3.      */
    4.     @Test
    5.     public void method1(){
    6.         // 1.获取容器:读取配置文件
    7.         ApplicationContext applicationContext
    8.                 = new ClassPathXmlApplicationContext("applicationContext.xml");

    9.         // 2.获取bean
    10.         StudentService studentService = (StudentService) applicationContext.getBean("studentService");

    11.         // 3.调用对应的方法
    12.         studentService.add();
    13.     }
    复制代码


三、注解扫描
  • 在核心配置文件中加入要扫描的类
    1. <!--
    2.     2.注解扫描
    3.         base-package :类所在的包
    4.     -->
    5. <context:component-scan base-package="com.cookie.service" />
    复制代码

  • 在对应类上加上@Component将该类放入IOC容器中,并起一个别名
    1. @Component("studentService")       
    2. public class StudentServiceImpl implements StudentService {

    3.     public void add() {
    4.         System.out.println(" add student ... ");
    5.     }
    6. }
    复制代码

  • 通过ClassPathXmlApplicationContext读取配置文件
    1. /**
    2.      * 2.注解扫描
    3.      *
    4.      */
    5.     @Test
    6.     public void method2(){
    7.         // 1.获取容器:读取配置文件
    8.         ApplicationContext applicationContext
    9.                 = new ClassPathXmlApplicationContext("applicationContext.xml");

    10.         // 2.获取bean
    11.         StudentService studentService = (StudentService) applicationContext.getBean("studentService");

    12.         // 3.调用对应的方法
    13.         studentService.add();
    14.     }

    复制代码


四、Java类
  • 创建一个java类CommonConfig
    1. package com.cookie;

    2. import com.cookie.service.StudentService;
    3. import com.cookie.service.impl.StudentServiceImpl;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;

    6. /**
    7. * @author cxq
    8. * @version 1.0
    9. * @date 2020/7/14 10:15
    10. * @desc
    11. */
    12. @Configuration // 声明这是一个配置类
    13. public class CommonConfig {

    14.     @Bean        // 声明bean
    15.     public StudentService studentService(){
    16.         return  new StudentServiceImpl();
    17.     }
    18. }

    复制代码

  • 通过AnnotationConfigApplicationContext读取该java配置类
    1. /**
    2.      * 3.基于java类
    3.      *
    4.      */
    5.     @Test
    6.     public void method3(){

    7.         ApplicationContext applicationContext
    8.                 = new AnnotationConfigApplicationContext(CommonConfig.class);

    9.         StudentService studentService = (StudentService) applicationContext.getBean("studentService");

    10.         studentService.add();
    11.     }
    复制代码






相关帖子

扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员6折;永久VIP4折
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
 
在线客服
点击这里给我发消息 点击这里给我发消息 点击这里给我发消息
售前咨询热线
312337667

微信扫一扫,私享最新原创实用干货

QQ|免责声明|小黑屋|依星资源网 ( 鲁ICP备2021043233号-3 )|网站地图

GMT+8, 2025-4-30 10:42

Powered by Net188.com X3.4

邮箱:312337667@qq.com 客服QQ:312337667(工作时间:9:00~21:00)

快速回复 返回顶部 返回列表