Spring 3.0 新特性_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Spring 3.0 新特性

Spring 3.0 新特性

 2010/12/11 11:31:43  CoderDream  http://coderdream.javaeye.com  我要评论(0)
  • 摘要:Spring3.0新特性1.JUnit4测试Spring关键点:使用AbstractJUnit4SpringContextTests,可以直接注入Bean1、Dao定义时使用注解@Component("userDao")2、测试类使用注解@ContextConfiguration("classpath:beans.xml")3、测试类继承AbstractJUnit4SpringContextTests4、测试类直接注入UserDao@Resource(name="userDao"
  • 标签:新特性 Spring

?

Spring 3.0 新特性

1.????? JUnit4 测试 Spring

关键点:使用AbstractJUnit4SpringContextTests ,可以直接注入Bean

1Dao 定义时使用注解@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" />

2.????? 泛型支持

支持泛型取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());

??? }

}

3.????? JavaConfig 方式

三种配置方式:XMLAnnotationJavaConfig

配置信息 Java 文件 中,即配置过程直接通过编写Java 代码 实现。

需要cglibasm

3.1.???? 注入 Dao

工程名 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

3.2.???? 注入 Service

使用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());

????? }

?

}

4.????? SpEL Spring EL

4.1.?? 简单应用

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));

????? }

}

4.2.?? 复杂应用

表达式直接使用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());

????? }

}

5.????? 源代码

见附件。

?

我的联系方式: 85337464

我的博客: http://coderdream.javaeye.com

?

发表评论
用户名: 匿名