Spring注解驱动开发 第十一节默认赋值注解

Spring注解驱动开发 第十一节默认赋值注解

怎样在bean注入到spring容器时属性传入默认值

@Component
public class Dog {
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

首先创建一个Dog类,有set get方法和一个toString方法

@Configuration
//@Import(MyImportSelector.class)
@ComponentScan("com.meng")
public class MainConfig {

}

然后在配置类中扫描包,将Dog注入到spring容器。

	@Test
    public void test001(){
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] names = context.getBeanDefinitionNames();
        for(String name : names){
            System.out.println(name);
        }
        Dog dog = (Dog) context.getBean("dog");
        System.out.println(dog);
    }

最后启动spring容器,并获取dog对象,查看打印结果:

四月 23, 2019 9:32:55 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4141d797: startup date [Tue Apr 23 21:32:55 CST 2019]; root of context hierarchy
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
dog
Dog{name='null', age='null'}

Process finished with exit code 0

可以看出Dog类的name与age属性没有被赋值,那么怎样赋值呢

@Component
public class Dog {
    @Value("旺财")
    private String name;
    @Value("#{20-17}")
    private String age;

在Dog类的属性上加入@Value注解就可以对属性进行赋值,而且还可以进行SpEl操作,而且还能读取配置文件中的内容,这个稍后在做演示,在这里,第一个是直接赋值,第二个是SpEl对整数做加减乘除操作。查看打印结果:

org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
dog
Dog{name='旺财', age='3'}

Process finished with exit code 0

可以看到Dog类中的属性已经不是null了。

接下来就是演示用${}获取配置文件中的值
@Configuration
//@Import(MyImportSelector.class)
@ComponentScan("com.meng")
@PropertySource(value={"classpath:/dog.properties"})
public class MainConfig {

}

首先在配置类中加入@PropertySource注解,添加配置文件的类路径,加载这个配置文件。

@Value("旺财")
    private String name;
    @Value("#{20-17}")
    private String age;
    @Value("${dog.sax}")
    private String sax;
    public String getName() {
        return name;
    }
dog.sax=

然后在sax属性上加入注解${}符号表示从配置文件中获取key为dog.sax的值,并赋给sax属性。查看打印结果:

org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
dog
Dog{name='旺财', age='3', sax='男'}

可以看出,sax属性的值已经从配置文件中读取出来并且赋值成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值