码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Bean的声明方式

时间:2020-07-14 12:59:33      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:public   ann   context   loading   ace   声明   classpath   ram   nconf   

一、环境说明
  1. 项目结构

    技术图片

  2. StudentService

    package com.cookie.service;
    
    /**
     * @author cxq
     * @version 1.0
     * @date 2020/7/14 9:18
     * @desc
     */
    public interface StudentService {
    
        void add();
    }
    
    
  3. StudentServiceImpl

    package com.cookie.service.impl;
    
    import com.cookie.service.StudentService;
    import org.springframework.stereotype.Component;
    
    /**
     * @author cxq
     * @version 1.0
     * @date 2020/7/14 9:20
     * @desc
     */
    public class StudentServiceImpl implements StudentService {
    
        public void add() {
            System.out.println(" add student ... ");
        }
    }
    
    
  4. applicationContext.xml核心配置文件

    <?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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    
    </beans>
    
二、XML
  1. 以bean的方式在核心配置文件中声明

    <!--
          xml声明
            id : bean的唯一标识
            class:bean所在的Java类的全类名
        -->
    <bean id="studentService" class="com.cookie.service.impl.StudentServiceImpl" />
    
  2. 通过ClassPathXmlApplicationContext读取配置文件

    /**
         * 基于xml声明bean
         */
        @Test
        public void method1(){
            // 1.获取容器:读取配置文件
            ApplicationContext applicationContext
                    = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            // 2.获取bean
            StudentService studentService = (StudentService) applicationContext.getBean("studentService");
    
            // 3.调用对应的方法
            studentService.add();
        }
    
三、注解扫描
  1. 在核心配置文件中加入要扫描的类

    <!--
        2.注解扫描
            base-package :类所在的包
        -->
    <context:component-scan base-package="com.cookie.service" />
    
  2. 在对应类上加上@Component将该类放入IOC容器中,并起一个别名

    @Component("studentService")	
    public class StudentServiceImpl implements StudentService {
    
        public void add() {
            System.out.println(" add student ... ");
        }
    }
    
  3. 通过ClassPathXmlApplicationContext读取配置文件

    /**
         * 2.注解扫描
         *
         */
        @Test
        public void method2(){
            // 1.获取容器:读取配置文件
            ApplicationContext applicationContext
                    = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            // 2.获取bean
            StudentService studentService = (StudentService) applicationContext.getBean("studentService");
    
            // 3.调用对应的方法
            studentService.add();
        }
    
    
四、Java类
  1. 创建一个java类CommonConfig

    package com.cookie;
    
    import com.cookie.service.StudentService;
    import com.cookie.service.impl.StudentServiceImpl;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author cxq
     * @version 1.0
     * @date 2020/7/14 10:15
     * @desc
     */
    @Configuration // 声明这是一个配置类
    public class CommonConfig {
    
        @Bean	// 声明bean
        public StudentService studentService(){
            return  new StudentServiceImpl();
        }
    }
    
    
  2. 通过AnnotationConfigApplicationContext读取该java配置类

     /**
         * 3.基于java类
         *
         */
        @Test
        public void method3(){
    
            ApplicationContext applicationContext
                    = new AnnotationConfigApplicationContext(CommonConfig.class);
    
            StudentService studentService = (StudentService) applicationContext.getBean("studentService");
    
            studentService.add();
        }
    

Spring Bean的声明方式

标签:public   ann   context   loading   ace   声明   classpath   ram   nconf   

原文地址:https://www.cnblogs.com/pretttyboy/p/13298240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!