009---@Value註解

實體類,如下@Value註解的使用,支持spel表達式

package top.huashengshu.bean;

import org.springframework.beans.factory.annotation.Value;

public class HelloWorld {

    @Value("張三")
    private String name;
    @Value("#{20-2}")
    private Integer age;

    public String getName() {
        return name;
    }

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

    public HelloWorld(){
        System.out.println("構造HelloWorld");
    }

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

配置類

package top.huashengshu.config;

import org.springframework.context.annotation.Bean;
import top.huashengshu.bean.HelloWorld;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationContext01 {
    @Bean
    public HelloWorld helloWorld(){
        return new HelloWorld();
    }
}

測試類

package top.huashengshu;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import top.huashengshu.bean.HelloWorld;
import top.huashengshu.config.ApplicationContext01;

public class DemoTest01 {
    @Test
    public void test01(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContext01.class);
        HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("helloWorld");
        System.out.println(helloWorld);
    }
}

 

 

發佈了259 篇原創文章 · 獲贊 15 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章