您的位置:首页 > 编程语言 > Java开发

小白文章之---Spring表达式(SpEl)获取对象值

2020-01-15 01:22 453 查看
创建实体类 创建一个类用于返回实体类的值
@Component
public class AccountSpEl {
@Bean("test")//使用时通过“spel”调用
public Account spELAccount(){
//创建并返回对象
return new Account("admin","admin");
}
}
创建一个类用于获取实体类对象的值
@Component("spElView")
public class SpElView {
//获取容器中名字为test的值,并将值赋值给定义的变量
@Value("#{test.name}")
private String name;
@Value("#{test.password}")
private String password;
public void input(){
System.out.println(name + ":" + password);
}
}
测试
@Configuration//配置spring容器
@ComponentScan("com.xuetang9.t8.spring")//扫描组件
public class App {
public static void main( String[] args ) {
//创建容器对象,扫描容器中的组件,即Component和Bean
ApplicationContext ctx = new AnnotationConfigApplicationContext(App.class);
SpElView spElView = ctx.getBean("spElView", SpElView.class);
spElView.input();
}
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
华小灼 发布了28 篇原创文章 · 获赞 2 · 访问量 452 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: