当前位置:文档之家› 北大青鸟软件园校区:使用p命名空间注入

北大青鸟软件园校区:使用p命名空间注入

网址 http://www.mybdqn.cn/
地址:北京市海淀区上地信息路甲28号科实大厦3层12区
使用 元素为Bean 的属性装配值和引用并不太复杂。尽管如此,Spring 的命名空间p
提供了另一种Bean 属性的装配方式,该方式不需要配置如此多的尖括号。
命名空间p 的schema URI 为http://www.springframework.org/schema/p。如果你想使用命名
空间p,只需要在Spring 的XML 配置中增加如下一段声明:

xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

通过此声明,我们现在可以使用p: 作为 元素所有属性的前缀来装配Bean 的属性。为了示
范,我们重新声明了kenny Bean 的配置:
p:song = "Jingle Bells"
p:instrument-ref = "saxophone" />

p:song 属性的值被设置为“Jingle Bells”,将使用该值装配song 属性。同样,p:instrument-ref

属性的值被设置为“saxophone”,将使用一个ID 为saxophone 的Bean 引用来装配instrument 属
性。-ref 后缀作为一个标识来告知Spring 应该装配一个引用而不是字面值。
选择 还是命名空间p 取决于你,它们是等价的。命名空间p 的最主要优点是更简洁。
在固定宽度的纸张上编写样例时,选择命名空间相对更合适。因此,在本书中你可能看到我不时的使用命
名空间p,特别是水平页面空间比较紧凑时。
3、注入不同数据类型
The value attribute of the element specifies a property or constructor argument as a
human-readable string representation. As mentioned previously, JavaBeansPropertyEditors are used to
convert these string values from a String to the actual type of the property or argument.

destroy-method="close">




4、Spring AOP
Aop面向切面编程,除了在申明式事物中外,我们自己还可以用于日志,安全信息等方面,以下案例
为操作日志案例:
public interface UserBiz {
public void save();
public void delete();
}

public class UserBizImpl implements UserBiz {
@Override
public void save() {
System.out.println("save user");
}
public void delete(){
System.out.println("delete user");
}
}

public class Logging{
public void writeLogging(){
System.out.println("write logging");
}
}
网址 http://www.mybdqn.cn/
地址:北京市海淀区上地信息路甲28号科实大厦3层12区
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
UserBiz ub= (UserBiz) ac.getBean("userBiz");
ub.save();
ub.delete();
}
}
applicationContext.xml







http://www.mybdqn.cn/

相关主题