components.xml 配置seam组件

components.xml 的配置采用反射机制来实现,组件类的私有属性照样可以设置(private String f1;)

如下的私有属性resolverChains没有提供get、set方法照样可以配置赋值 。之前我一直以为私有的属性是不可以设置的,原来大错特错了。

public class PermissionMapper implements Serializable

{

   private Map<Class,Map<String,String>> resolverChains = new HashMap<Class,Map<String,String>>();

<security:permission-mapper default-resolver-chain="defaultChain" resolver-chains="#{resolverChains}">    </security:permission-mapper>

基本component配置

所有seam组件都可以使用基本component方式来配置,示例如下

<component class="org.jboss.seam.bpm.Jbpm"/>

<component name="customerDatabase"class="org.jboss.seam.persistence.ManagedPersistenceContext">

<component name="accountingDatabase"

class="org.jboss.seam.persistence.ManagedPersistenceContext">

<propertyname="persistenceUnitJndiName">java:/accountingEntityManagerFactory</

property>

</component>

<component class="com.helloworld.Hello" name="hello">

<propertyname="name">#{user.name}</property>

</component>

xs:element name="component"

    <xs:element name="component">

<xs:annotation>

<xs:documentation>ThecomponenttagdefinesasingleSeamcomponent.Itmayserveastotherootelementof

afine-grained*.component.xmlfile.</xs:documentation>

</xs:annotation>

<xs:complexType>

<xs:sequence>

<xs:elementminOccurs="0"maxOccurs="unbounded"ref="components:property"/>

</xs:sequence>

<xs:attributeGroupref="components:attlist.component"/>

<xs:attributeGroupref="components:attlist.ejbcomponent"/>

</xs:complexType>

    </xs:element>

xs:element name="property"

    <xs:element name="property">

<xs:annotation>

<xs:documentation>

Agenericproperty.Thebodycontainsthevalueorvalues.

</xs:documentation>

</xs:annotation>

<xs:complexTypemixed="true">

<xs:choiceminOccurs="0"maxOccurs="unbounded">

<xs:elementref="components:key"/>

<xs:elementref="components:value"/>

</xs:choice>

<xs:attributeGroupref="components:attlist.property"/>

</xs:complexType>

    </xs:element>

attlist.property

<property name=“name” type="org.manaty.mvno.portal.Party">

    <xs:attributeGroup name="attlist.property">

<xs:attributename="name"use="required"type="components:string">

<xs:annotation>

<xs:documentation>Thepropertyname</xs:documentation>

</xs:annotation>

</xs:attribute>

<xs:attributename="type"type="components:string">

<xs:annotation>

<xs:documentation>Concretetypetouseifthepropertyismulti-valued</xs:documentation>

</xs:annotation>

</xs:attribute>

    </xs:attributeGroup>

key

与vlaue配合使用

<property name="issueStatusOptions" type="java.util.LinkedHashMap">

<key>open</key><value>openissue</value>

<key>resolved</key><value>issueresolvedbydeveloper</value>

<key>closed</key><value>resolutionacceptedbyuser</value>

</property>

    <xs:element name="key" type="components:string">

<xs:annotation>

<xs:documentation>Formapvalues,thekeyforthefollowingvalue</xs:documentation>

</xs:annotation>

    </xs:element>

最低级元素了,不能再有子元素

value

设置list,与key配合可以设置map

            <value>processTestStock/processdefinition.xml</value>            <value>processTestStockRoaming/processdefinition.xml</value>

    <xs:element name="value" type="components:string">

<xs:annotation>

<xs:documentation>

Forlistvalues,thevaluetobeadded.

Formapvalues,thevaluefortheprecedingkey

</xs:documentation>

</xs:annotation>

    </xs:element>

最低级元素了,不能再有子元素

property属性取值

有三种方式设置property的值

1)直接字符串,只能设置String类型的属性

<property name="persistenceUnitJndiName">java:/accountingEntityManagerFactory</

property>

</component>

2)使用表达式,可以设置任意Object类型的属性<property name="user">#{user}</property>

3)从components.properties(组件属性文件)中获取,只能是String

<property name="filename">@properties.filename@</property>

components.properties文件内容如下

jndiPattern=portal/#{ejbName}/local

debug=true

properties.filename=portal.properties

properties.reload=true

不是以@开头的直接返回即可  ,任何以@开头的都会从components.properties中获取,不单单是在 property中

<core:init debug="true" jndi-pattern="@jndiPattern@"/>

private String replace(String value, Properties replacements)

{

if(value.startsWith("@"))

{

value=replacements.getProperty(value.substring(1,value.length()-1));

}

returnvalue;

   }

component只能有property子节点,property的属性级子节点的属性(key,value)都可以使用字符串和表达式两种形式,表达式可以设置object类型的值

map

可以使用key,value元素来实现map的配置,需要注意的是bean属性(map属性)的写法,配置会获取bean的属性,不管该属性是否有对应的set方法,将配置的值put进map中,所以属性值必须首先赋值( = new HashMap)

    <security:permission-mapper default-resolver-chain="defaultChain">

<security:resolver-chains>

<key>org.manaty.model.party.Party</key><value>#{partyResolverMap.configMap}</value>

<key>org.manaty.model.order.Order</key><value>#{partyResolverMap.configMap}</value>

</security:resolver-chains>

    </security:permission-mapper>

public class PermissionMapper implements Serializable

{

   private Map<Class,Map<String,String>> resolverChains = new HashMap<Class,Map<String,String>>();

定义map组件

由于key、value是最底层的元素,所以不能定义嵌套的map,比如Map<Class,Map<String,String>>是不能直接定义的,需要将Map<String,String>定义成一个组件。

定义一个包含map的组件

<component name="partyResolverMap" class="org.manaty.util.SeamUtil" auto-create="true" scope="application">

<propertyname="configMap">

<key>restrictPartyResource</key><value>partyChain</value>

</property>

    </component>

使用上面定义的map

<security:permission-mapper default-resolver-chain="defaultChain">

<security:resolver-chains>

<key>org.manaty.model.party.Party</key><value>#{partyResolverMap.configMap}</value>

<key>org.manaty.model.order.Order</key><value>#{partyResolverMap.configMap}</value>

</security:resolver-chains>

    </security:permission-mapper>

public class SeamUtil {

    private Map<String,String> configMap=new HashMap<String,String>();

publicMap<String,String>getConfigMap(){

returnconfigMap;

    }

特定组件配置

seam对一些常用的内置组件定义定义了些schema,这样就可以利用编辑器的提示功能还编写组件配置了,这样可以提高效率。

    <core:manager concurrent-request-timeout="500"

conversation-timeout="120000"conversation-id-parameter="cid"

        parent-conversation-id-parameter="pid" />

所有特定组件都按照规定的模式来解析配置,所以即使系统没有提供对应的schema,我们也可以按照规则来使用。

1)在包中增加 package-info.java,用来定义namespace和prefix

@Namespace(value="http://jboss.com/products/seam/security", prefix="org.jboss.seam.security")

@AutoCreate

packageorg.jboss.seam.security.management;

importorg.jboss.seam.annotations.AutoCreate;

import org.jboss.seam.annotations.Namespace;

不同的包可以有相同的 package-info.java定义

如下两个包中的package-info.java的定义是相同的

package org.jboss.seam.security.management

package org.jboss.seam.security;

2)tag name和属性名的拼写规则

<security:permission-mapper default-resolver-chain="defaultChain" resolver-chains="#{resolverChains}">    </security:permission-mapper>

tag name:permission-mapper对应类名PermissionMapper,将类名的首字母小写,中间的大写字母换成(-小写)

属性:default-resolver-chain对应private String defaultResolverChain;大写字母换成(-小写)

相关推荐