创建实体类 创建一个类用于返回实体类的值 @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(); } }