在Spring1.x系列中,bean的配置文件使用DTD,没有namespace的分隔。
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<!
DOCTYPE beans PUBLIC
"
-//SPRING//DTD BEAN//EN
"
"
http://www.springframework.org/dtd/spring-beans.dtd
"
>
<
beans
>

</
beans
>
2.0的一个非常大的改进是引入了XML Schema的namespace,因而可以将bean的配置文件做大幅度的简化。
<?
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:aop
=
"
http://www.springframework.org/schema/aop
"
xmlns:tx
=
"
http://www.springframework.org/schema/tx
"
xsi:schemaLocation
=
"
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http:
//
www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http:
//
www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
</
beans
>
现在我们对这一个基于xml Schema的配置文件进行常用功能的演示:
上面这个配置文件中加入了AOP配置的简化,事务配置的简化的一些元素
在spring框架的解压缩包的dist\resources路径下包含大量的schema文件:如:
spring-aop-2.5.xsd
spring-beans-2.5.xsd
spring-context-2.5.xsd
spring-jee-2.5.xsd
spring-jms-2.5.xsd
spring-lang-2.5.xsd
spring-tool-2.5.xsd
spring-tx-2.5.xsd
spring-util-2.5.xsd等文件
其中只有spring-beans-2.5.xsd是spring的内核,其他schema文件大都是为了简化某些方面的配置
一.使用p名字空间配置属性
假设有如下持久类:
import com.spring.being.Axe;
import com.spring.being.Person;
public class Janpanese implements Person{
private Axe axe;//斧头类,自己书写
private String name;//name属性
public Janpanese(){
System.out.println("Janpanese的无参构造器被调用.......");
}
public void setAxe(Axe axe) {
System.out.println("Spring执行依赖关系注入.......");
this.axe = axe;
}
public Axe getAxe() {
return axe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void useAxe() {
System.out.println("日本人使用:"+axe.chop());
System.out.println("参数name的值:"+name);
}
}
上面的持久类中的axe和name属性需要注入,如果直接采用DTD的配置,则需要采用<property.../>元素来配置这两个属性。但是如果使用Schema的p名称空间,则可以直接使用属性来配置他们
配置文件如下:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="axe" class="com.spring.being.impl.SteelAxe"/>
<bean id="Janpanese" class="com.spring.xmlschema.Janpanese"
p:axe-ref="axe" p:name="郑君华">
</bean>
</beans>
在一般的schema文件中加入第一行带红色的代码,可以简化属性de依赖注入。
二。使用AOP Schema
其中我们需要修改配置文件和切面类,修改后如下所示:
- public class CheckSecurity {
- public void checkSecurity(ProceedingJoinPoint pjp) throws Throwable{
- System.out.println("checkSecurity...");
- for(Object p : pjp.getArgs()){
- System.out.println(p);
- }
- pjp.proceed();
- }
- }
public class CheckSecurity { public void checkSecurity(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("checkSecurity..."); for(Object p : pjp.getArgs()){ System.out.println(p); } pjp.proceed(); } }
我们可以在通知中添加一个参数,JoinPoint类型的参数,这个参数的值
spring会自动传入,从JoinPoint参数中,我们可以获取目标对象的相关信息,被调用
对象有那些参数即哪个方法被调用等等。更多请参考通知参数的说明.
applicationContext-aop.xml 写道
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="userManagerAop" class="com.ufinity.spring.aop.aspectj.UserManagerImpl"></bean>
<bean id="checkSecurity" class="com.ufinity.spring.aop.schema.CheckSecurity"></bean>
<aop:config >
<!-- 通过<aop:aspect> 标签定义切面 -->
<aop:aspect id="userAspect" ref="checkSecurity">
<!-- 通过<aop:piontcut> 标签定义切入点 -->
<aop:pointcut id="allMethods" expression_r_r="execution(* add*(..)) || execution(* del*(..)) ||execution(* update*(..)) ||execution(* find*(..))"/>
<aop:around pointcut-ref="allMethods" method="checkSecurity"/>
</aop:aspect>
</aop:config>
</beans>
三.使用 util Schema
测试类
import com.spring.being.Axe;
import com.spring.being.Person;
public class Janpanese implements Person{
private Axe axe;
private String name;
private List schools;
private Map scores;
public Janpanese(){
System.out.println("Janpanese的无参构造器被调用.......");
}
public void setAxe(Axe axe) {
System.out.println("Spring执行依赖关系注入.......");
this.axe = axe;
}
public Axe getAxe() {
return axe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getSchools() {
return schools;
}
public void setSchools(List schools) {
this.schools = schools;
}
public Map getScores() {
return scores;
}
public void setScores(Map scores) {
this.scores = scores;
}
public void useAxe() {
System.out.println("日本人使用:"+axe.chop());
System.out.println("参数name的值:"+name);
System.out.println(schools);
System.out.println(scores);
}
}
配置文件:
<?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:p=" http://www.springframework.org/schema/p"
xmlns:util="
http://www.springframework.org/schema/util
"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/beans/spring-util-2.5.xsd
">
<bean id="axe" class="com.spring.being.impl.SteelAxe"/>
<bean id="Janpanese" class="com.spring.xmlschema.Janpanese"
p:axe-ref="axe" p:name="郑君华"
p:schools-ref="util.schools"
p:scores-ref="util.scores"
/>
<util:list id="util.schools" list-class="java.util.ArrayList">
<values>小学</values>
<values>中学</values>
</util:list>
<util:map id="util.scores" map-class="java.util.HashMap">
<entry key="英语" value="87" />
<entry key="数学" value="90" />
</util:map>
</beans>
四.使用 context Schema
<?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:p=" http://www.springframework.org/schema/p"
xmlns:util="
http://www.springframework.org/schema/context
"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/beans/spring-context-2.5.xsd
">
<!--自动扫描com.spring.bean包下的所有bean类-->
<context:component-scan base-package="com.spring.bean"
</beans>
该配置文件一般用于sping的“零配置”支持