• Spring Bean的声明方式


    一、环境说明
    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();
          }
      
  • 相关阅读:
    django之验证码
    无法显示SQL Server Management Studio Express解决办法
    程序员常用不常见很难得的地址大全转
    调用ip138的页面获取IP地址
    VS 2010无法创建项目提示写入项目文件时出错 没有注册类别
    webapi token、参数签名是如何生成的(转载)
    尝试asp.net mvc 基于controller action 方式权限控制方案可行性(转载)
    委托学习
    Webapi上传数据(XML)敏感字符解决方案
    redis 学习
  • 原文地址:https://www.cnblogs.com/pretttyboy/p/13298240.html
Copyright © 2020-2023  润新知