1 用注释
   @ContextConfiguration("classpath:beans.xml")
public class UserDAOTest extends AbstractJUnit4SpringContextTests{
 
 @Resource(name="userDAO")
 private UserDAO userDAO;
 
 @Test
 public void testSave() {
  this.userDAO.save(new User());
 }

  注意,extends AbstractJUnit4SpringContextTest的话,就不用再用以前的去根据ApplicationContext去读取了。

2 获得BEAN的时候支持泛型
   @Test
 public void testSave() {
  ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  UserDAO userDAO = context.getBean("userDAO", UserDAO.class);
  
  //UserDAO userDAO = (UserDAO)context.getBean("userDAO");(这是以前的写法)
  userDAO.save(new User());
3 将一个JAVA文件看作配置文件
   在beans.xml中
   <context:component-scan base-package="com.liao.config"></context:component-scan>
然后在类中,设置这个配置文件:
  import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.bjsxt.spring30.dao.UserDAO;
import com.bjsxt.spring30.dao.impl.UserDAOImpl;

@Configuration
public class AppConfig {
 @Bean
 public UserDAO userDAO() {
  return new UserDAOImpl();
 }
 
 //xml
 //<bean id = "userDAO" class="userDAO()"
}

  又如:
@Bean
 public UserDAO userDAO() {
  return new UserDAOImpl();
 }
 
 @Bean
 public UserService userService() {
  UserServiceImpl service = new UserServiceImpl();
  service.setUserDAO(userDAO());
  return service;
 }

4 SPEL:
  @Test
 public void test01() {
  ExpressionParser parser = new SpelExpressionParser();
  Expression exp = parser.parseExpression("'Hello,World'");
  System.out.println((String)exp.getValue());
 }
 
 @Test
 public void test02() {
  
  User u = new User();
  u.setUsername("zhangsan");
  
  EvaluationContext context = new StandardEvaluationContext(u);
  
  ExpressionParser parser = new SpelExpressionParser();
  Expression exp = parser.parseExpression("username"); //u.getUsername()
  System.out.println((String)exp.getValue(context));
  System.out.println((String)exp.getValue(u));
5 SPEL中,可以在配置文件中赋予和计算一些表达式了,比如:
  <property name="randomNumber" value="#{T(java.lang.Math).random() * 100}"></property>