bean的基本设置

bean的表述

类别描述
名称bean
类型标签
功能将对象交给spring容器管理
格式
<beans>
<bean/>
<bean><bean/>
<beans/>
属性列表

id:可以通过id获取对应的bean,在容器中id必须是唯一

class:bean的类型,即配置的bean的全路径类名

示例
<bean id="studentDao" class="com.ioc.dao.impl.StudentDaoImpl"/>
<bean id="studentService" class="com.ioc.service.impl.StudentServiceImpl">
    <property name="studentDao" ref="studentDao"/>
</bean>

编写流程

  1. 添加依赖
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.2.RELEASE</version>
            </dependency>
  2. 创建applicationContext.xml (spring核心配置文件)
  3. 在配置文件中添加bean
     <bean id="studentDao" class="com.ioc.dao.impl.StudentDaoImpl"/>
  4. 通过applicationContext接口注入(DI注入对象)
     // 初始化Spring的ApplicationContext,加载applicationContext.xml文件
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            // 从IOC容器中获取StudentDao Bean,并强制转换为StudentDao类型
            StudentDao studentDao = (StudentDao) context.getBean("studentDao");
  5. 处理业务逻辑层service
  6. 将service接口中的new出的dao对象去除,只保留接口
    private StudentDao studentDao=new StudentDaoImpl();
    修改为
     private StudentDao studentDao;
  7. 添加get,set方法
     /**
         * 获取学生数据访问对象(DAO)
         */
        public StudentDao getStudentDao() {
            return studentDao;
        }
    
        /**
         * 设置学生数据访问对象(DAO)
         */
        public void setStudentDao(StudentDao studentDao) {
            this.studentDao = studentDao;
        }
  8. 在配置文件中添加bean并通过添加关系
      <!-- 配置Student服务的bean -->
        <bean id="studentService" class="com.ioc.service.impl.StudentServiceImpl">
            <!--        配置service和dao的关系,配置都是通过get和set拿到对象,添加前需要添加get,set-->
            <property name="studentDao" ref="studentDao"/>
        </bean>
  9. 编写测试类
     public static void main(String[] args) {
            // 初始化Spring的ApplicationContext,加载applicationContext.xml文件
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            // 从IOC容器中获取StudentDao Bean,并强制转换为StudentDao类型
            StudentDao studentDao = (StudentDao) context.getBean("studentDao");
    
            // 遍历并打印所有Bean的定义名称,用于验证Bean是否正确加载
            for (String beanDefinitionName : context.getBeanDefinitionNames()) {
                System.out.println(beanDefinitionName);
            }
            System.out.println("--------------------------------------------------");
    
            // 调用StudentDao的save方法
            studentDao.save();
            System.out.println("--------------------------------------------------");
            // 从Spring上下文中获取StudentService Bean
            StudentService studentService = (StudentService) context.getBean("studentService");
            // 调用StudentService的save方法
            studentService.save();
        }

    bean的实例化--构造方法

在service实现中添加构造方法

 public StudentServiceImpl() {
        System.out.println(1);
        System.out.println(2);
    }

添加测试方法

   public static void main(String[] args) {
        // 初始化Spring的ApplicationContext,加载applicationContext.xml文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从Spring上下文中获取StudentService Bean
        StudentService studentService = (StudentService) context.getBean("studentService");
        // 调用StudentService的save方法
        studentService.save();
    }

控制台打印,输出了1和2

  • 29
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BeanDefinition是Spring中的一个核心概念,它表示了一个Bean的定义。在Spring中,根据BeanDefinition来创建Bean对象。BeanDefinition具有很多属性用来描述Bean的信息和配置。 AnnotatedBeanDefinition是AnnotatedBeanDefinition接口的实现类,表示注解的BeanDefinition。它包含了注解元数据和基本类元数据的信息。AnnotatedBeanDefinition有两个主要的实现类:AnnotatedGenericBeanDefinition和ScannedGenericBeanDefinition。 RootBeanDefinition代表普通的BeanDefinition实现,通过直接实例化BeanDefinition对象来创建。ChildBeanDefinition代表可以设置BeanDefinition的子BeanDefinition。GenericBeanDefinition代表一般的BeanDefinition,AnnotatedGenericBeanDefinition和ScannedGenericBeanDefinition都是它的两个主要实现类。 综上所述,BeanDefinition是Spring中用来描述Bean的定义的概念,它包含了Bean的各种属性和配置信息。AnnotatedBeanDefinition是注解的BeanDefinition的实现类,它包含了注解元数据和类元数据的信息。RootBeanDefinition和ChildBeanDefinition是BeanDefinition的不同实现方式,GenericBeanDefinition是一般的BeanDefinition,它的两个主要实现类是AnnotatedGenericBeanDefinition和ScannedGenericBeanDefinition。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [BeanDefinition](https://blog.csdn.net/xxssyyyyssxx/article/details/124819785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值