zoukankan      html  css  js  c++  java
  • 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();
          }
      
  • 相关阅读:
    bzoj4358: permu
    bzoj4636: 蒟蒻的数列
    bzoj4229: 选择
    bzoj4561: [JLoi2016]圆的异或并
    bzoj2618: [Cqoi2006]凸多边形
    bzoj1803: Spoj1487 Query on a tree III
    bzoj2467: [中山市选2010]生成树
    PostgreSQL服务器存储参数的内部查看方法和实际表述之间的关系
    PostgreSQL 系统表
    PostgreSQL环境中查看SQL执行计划示例
  • 原文地址:https://www.cnblogs.com/pretttyboy/p/13298240.html
Copyright © 2011-2022 走看看