實體類,如下@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);
}
}