?
Spring 3.0 新特性
关键点:使用AbstractJUnit4SpringContextTests ,可以直接注入Bean
1 、Dao 定义时使用注解@Component("userDao")
2 、测试类使用注解 @ContextConfiguration("classpath:beans.xml")
3 、测试类继承AbstractJUnit4SpringContextTests
4 、测试类直接注入UserDao
?????? @Resource(name = "userDao")
?????? private UserDao userDao;
5 、测试方法直接调用注入的userDao 的方法:userDao.save(new User());
工程名 :SXT_Spring3_01
测试类 :UserDaoTest
需要commons-logging 包
代码清单 1 : UserDao.java
@Component ( "userDao" )
public class UserDaoImpl implements UserDao {
??? @Override
??? public void save(User user) {
?????? System. out .println( "a user saved!" );
??? }
}
代码清单 2 : UserDaoTest.java
@ContextConfiguration( "classpath:beans.xml" )
public class UserDaoTest extends AbstractJUnit4SpringContextTests {
????? @Resource(name = "userDao" )
????? private UserDao userDao ;
?
????? @Test
????? public void testSave() throws Exception {
?????????? this . userDao .save( new User());
????? }
?
}
代码清单 3 : beans.xml
< context:component-scan base-package= "com.coderdream" />
支持泛型取Bean ,不用强制转换了。
工程名 :SXT_Spring3_02
测试类 :UserDaoTest2
代码清单 4 : UserDaoTest2.java
public class UserDaoTest2 {
??? @Test
??? public void testSave() throws Exception {
?????? ApplicationContext applicationContext =
new ClassPathXmlApplicationContext( "beans.xml" );
?????? UserDao userDao =
applicationContext.getBean( "userDao" , UserDao. class );
?????? userDao.save( new User());
??? }
}
三种配置方式:XML 、Annotation 和JavaConfig 。
配置信息 在Java 文件 中,即配置过程直接通过编写Java 代码 实现。
需要cglib 和asm 包
工程名 :SXT_Spring3_03_Configuration_Dao
测试类 :UserDaoTest
代码清单 5 : beans.xml
< context:component-scan base-package = "com.coderdream.config" />
代码清单 6 : AppConfig.java
@Configuration
public class AppConfig {
????? // xml<bean id = "userDao" class="userDao()"
????? @Bean
????? public UserDao userDao() {
?????????? return new UserDaoImpl();
????? }
}
测试代码见代码清单4 。
使用JavaConfig ,引入UserService ,注入UserDao 。
工程名 :SXT_Spring3_04_Configuration_Dao_Service
测试类 :UserServiceTest
代码清单 7 : AppConfig.java
@Configuration
public class AppConfig {
????? // xml <bean id = "userDao" class="userDao()"
????? @Bean
????? public UserDao userDao() {
?????????? return new UserDaoImpl();
????? }
?
????? @Bean
????? public UserService userService() {
?????????? UserServiceImpl userServiceImpl = new UserServiceImpl();
?????????? userServiceImpl.setUserDao(userDao());
?????????? return userServiceImpl;
????? }
}
代码清单 7 : UserServiceTest .java
@ ContextConfiguration("classpath:beans.xml" )
public class UserServiceTest extends AbstractJUnit4SpringContextTests {
?
????? @Resource (name = "userService" )
????? private UserService userService ;
?
????? @Test
????? public void testSave() throws Exception {
?????????? this .userService .add(new User());
????? }
?
}
1、 表达式直接取对象的值
2、 表达式自动处理对象(取对象的属性等等)
工程名 :SXT_Spring3_05_SpEL_01
测试类 :SpelTest_Sample
代码清单 7 : SpelTest_Sample .java
public class SpelTest_Sample {
?
????? @Test
????? public void test01() throws Exception {
?????????? ExpressionParser expressionParser = new SpelExpressionParser();
?????????? Expression expression =
expressionParser.parseExpression("'Hello,World'" );
?????????? System.out .println(expression.getValue());
????? }
?
????? @Test
????? public void test02() throws Exception {
?????????? User user = new User();
?????????? user.setUsername("zhangsan" );
?
?????????? EvaluationContext evaluationContext =
new StandardEvaluationContext(user);
?????????? ExpressionParser expressionParser =
new SpelExpressionParser();
?????????? Expression expression =
expressionParser.parseExpression("username" );
?????????? System.out .println(expression.getValue(evaluationContext));
?????????? System.out .println(expression.getValue(user));
????? }
}
表达式直接使用Java 工具类取值、获取系统属性值
工程名 :SXT_Spring3_06_SpEL_02
测试类 :SpelTest_Sample2
代码清单 8 : MyBean .java
public class MyBean {
????? private double randomNumber ;
????? private String userRegion ;
????? // getter & setter
}
代码清单 9 : beans.xml
< bean id = "myBean" class = "com.coderdream.spel.MyBean">
????? < property name = "randomNumber"
value = "#{T(java.lang.Math).random()*100}" />
????? < property name = "userRegion"
value = "#{systemProperties['user.country']}" />
</ bean >
代码清单 10 : SpelTest_Sample2.xml
@ ContextConfiguration("classpath:beans.xml" )
public class SpelTest_Sample2 extends AbstractJUnit4SpringContextTests {
????? @Resource
????? private MyBean myBean ;
?
????? @Test
????? public void test01() throws Exception {
?????????? System.out .println(myBean .getRandomNumber());
?????????? System.out .println(myBean .getUserRegion());
????? }
}
见附件。
?
我的联系方式: 85337464
我的博客: http://coderdream.javaeye.com
?