9.基於Java的容器配置

9.基於Java的容器配置

這裏先直接上代碼例子,後面會進行總結java

第一步:編寫實體類spring

 public class User implements Serializable {
 
     @Value("xuan")
     private String name;
     @Value("22")
     private Integer age;
    ....
 }

第二步:編寫本身的配置類數組

  • @Configuration:這個註解至關於標誌了這個類是一個配置類 就像咱們applicationConfig.xml配置文件的beans標籤app

  • @Bean :見名知意 至關於xml中的bean標籤 將對象添加到容器中 測試

  • getUser(方法名):至關於bean標籤中的id屬性atom

  • User(返回值類型): 至關於bean標籤中的餓class屬性spa

 package com.xuan.config;
 
 import com.xuan.pojo.User;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 @Configuration
 public class XuanConfig {
 
     @Bean
     public User getUser(){
         return new User();
    }
 }

第三步編寫測試類code

  • 這裏用的是AnnotationConfigApplicationContext也就是註解版的上下文component

  • AnnotationConfigApplicationContext中的參數是傳一個咱們本身配置類的字節碼文件(能夠是多個可是通常咱們不這樣寫,咱們會將多個配置類經過@Import方法添加在主配置類中)xml

  • 下面的getBean方法傳入的參數能夠是傳入id,沒傳的話會spring會自動掃描配置

 import com.xuan.config.XuanConfig;
 import com.xuan.pojo.User;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
 public class TestUser {
 
     public static void main(String[] args) {
         ApplicationContext context=new AnnotationConfigApplicationContext(XuanConfig.class);
         User user = context.getBean(User.class);
         System.out.println(user);
    }
 
 }

補充

  • @Configuration註解源碼

 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Component
 public @interface Configuration {
 
    /**
     * Explicitly specify the name of the Spring bean definition associated with the
     * {@code @Configuration} class. If left unspecified (the common case), a bean
     * name will be automatically generated.
     * <p>The custom name applies only if the {@code @Configuration} class is picked
     * up via component scanning or supplied directly to an
     * {@link AnnotationConfigApplicationContext}. If the {@code @Configuration} class
     * is registered as a traditional XML bean definition, the name/id of the bean
     * element will take precedence.
     * @return the explicit component name, if any (or empty String otherwise)
     * @see AnnotationBeanNameGenerator
     */
    @AliasFor(annotation = Component.class)
    String value() default "";
 
    /**
     * @since 5.2
     */
    boolean proxyBeanMethods() default true;
 
 }
  • @Import註解源碼 :發現是能夠傳入數組的配置類的

 package org.springframework.context.annotation;
 
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 public @interface Import {
 
    /**
     * {@link Configuration @Configuration}, {@link ImportSelector},
     * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
     */
    Class<?>[] value();
 
 }
相關文章
相關標籤/搜索