zoukankan      html  css  js  c++  java
  • Spring boot 官网学习笔记

    1. 推荐使用 Java-based configuration ,也可以使用xml
    2.  we generally recommend that your primary source be a single @Configuration class. Usually the class that defines the main method is a good candidate as the primary @Configuration
    3. Importing Additional Configuration Classes
      1. You need not put all your @Configuration into a single class. 
        The @Import annotation can be used to import additional configuration classes.
        Alternatively, you can use @ComponentScan to automatically pick up
        all Spring components, including @Configuration classes.
      2. 示例
        1. Car.java
          package javabeat.net.basic;
          public interface Car {
              public void print();
          }
          
          
          Toyota.java
          package javabeat.net.basic;
          import org.springframework.stereotype.Component;
          @Component
          public class Toyota implements Car{
              public void print(){
                  System.out.println("I am Toyota");
              }
          }
          
          
          Volkswagen.java
          package javabeat.net.basic;
          import org.springframework.stereotype.Component;
          @Component
          public class Volkswagen implements Car{
              public void print(){
                  System.out.println("I am Volkswagen");
              }
          }
          
          
          JavaConfigA.java
          package javabeat.net.basic;
          
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;
          @Configuration
          public class JavaConfigA {
              @Bean(name="volkswagen")
              public Car getVolkswagen(){
                  return new Volkswagen();
              }
          }
          
          
          JavaConfigB.java
          package javabeat.net.basic;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;
          @Configuration
          public class JavaConfigB {
              @Bean(name="toyota")
              public Car getToyota(){
                  return new Toyota();
              }
          }
          
          
          ParentConfig.java
          package javabeat.net.basic;
          import org.springframework.context.annotation.Configuration;
          import org.springframework.context.annotation.Import;
          @Configuration
          @Import({JavaConfigA.class,JavaConfigB.class})
          public class ParentConfig {
              //Any other bean definitions
          }
          
          
          ContextLoader.java
           package javabeat.net.basic;
          import org.springframework.context.annotation.AnnotationConfigApplicationContext;
          public class ContextLoader {
              public static void main (String args[]){
                  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ParentConfig.class);
                  Car car = (Toyota)context.getBean("toyota");
                  car.print();
                  car = (Volkswagen)context.getBean("volkswagen");
                  car.print();
                  context.close();
              }
          }
        2. If you run the above example,the printed output will be,
          javaconfig spring import
    4. Importing XML Configuration
      1. If you absolutely must use XML based configuration,
        we recommend that you still start with a @Configuration class.
        You can then use an @ImportResource annotation to load XML configuration files.
  • 相关阅读:
    scrapy之download middleware
    远程采集
    未能加载文件或程序集“Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyTok”
    【转】如何解决C盘根目录无法创建或写入文件
    C#报算术运算导致溢出的错误
    【转】C# String 前面不足位数补零的方法
    【转】C# 使用正则表达式去掉字符串中的数字,或者去掉字符串中的非数字
    【转】Asp.Net页面生命周期
    【转】processOnServer
    【转】oracle的分析函数over
  • 原文地址:https://www.cnblogs.com/jiangtao1218/p/10171078.html
Copyright © 2011-2022 走看看