Spring3.0的几个新特性

1、利用Spring自带的测试工具测试的时候属性可以自己注入

import javax.annotation.Resource;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.bjsxt.spring30.model.User;

@ContextConfiguration("classpath:beans.xml")
public class UserDAOTest extends AbstractJUnit4SpringContextTests{

@Resource(name="userDAO")
private UserDAO userDAO;

@Test
public void testSave() {
this.userDAO.save(new User());
}

}


2、context.getBean可以使用泛型不用强制类型转换了

public class UserDAOTest2 {
@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文件里面(没什么多大作用,Spring的文档里面说如果你不想脱离java风格的配置文件,就可以这么写)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="bruce.zhao.spring3_0.config"></context:component-scan>
</beans>


@Configuration
public class AppConfig {
@Bean
public UserDAO userDAO() {
return new UserDAOImpl();
}

//xml
//<bean id = "userDAO" class="userDAO()"
}


4、SpEL语言
动态语言的主要特性:能把一个字符串"new HelloWorld().sayHello()"当成语法运行,用现有的代码产生新的代码。
如js里面eval("alert('ok');");
java做不到这一点,所以他是静态语言,除非用java6里面的Compiler动态编译下。

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


eg2 可以用在配置文件里面,更加灵活的给我们一些属性进行初始化的赋值

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--
<context:component-scan base-package="bruce.zhao.spring3_0.config"></context:component-scan>
-->

<bean id="my" class="bruce.zhao.spring3_0.spel.MyBean">
<property name="randomNumber" value="#{T(java.lang.Math).random() * 100}"></property>
<property name="userRegion" value="#{systemProperties['user.country']}"></property>
</bean>

</beans>



public class MyBean {
private double randomNumber;
private String userRegion;
public double getRandomNumber() {
return randomNumber;
}
public void setRandomNumber(double randomNumber) {
this.randomNumber = randomNumber;
}
public String getUserRegion() {
return userRegion;
}
public void setUserRegion(String userRegion) {
this.userRegion = userRegion;
//System.getProperty("user.country");
}

}

@ContextConfiguration("classpath:beans.xml")
public class SpELTest extends AbstractJUnit4SpringContextTests{

@Resource
private MyBean my;

@Test
public void test01() {
System.out.println(my.getRandomNumber());
System.out.println(my.getUserRegion());
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值