9.基于Java的容器配置

9.基于Java的容器配置

这里先直接上代码例子,后面会进行总结

​ 第一步:编写实体类

public class User implements Serializable {

    @Value("xuan")
    private String name;
    @Value("22")
    private Integer age;
    ....
}

​ 第二步:编写自己的配置类

  • @Configuration:这个注解相当于标志了这个类是一个配置类 就像我们applicationConfig.xml配置文件的beans标签
  • @Bean :见名知意 相当于xml中的bean标签 将对象添加到容器中
  • getUser(方法名):相当于bean标签中的id属性
  • User(返回值类型): 相当于bean标签中的饿class属性
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();
    }
}

​ 第三步编写测试类

  • 这里用的是AnnotationConfigApplicationContext也就是注解版的上下文
  • AnnotationConfigApplicationContext中的参数是传一个我们自己配置类的字节码文件(可以是多个但是一般我们不这样写,我们会将多个配置类通过@Import方法添加在主配置类中)
  • 下面的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();

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值