components.xml 配置seam组件

卢志业
2023-12-01

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">
<property name="persistenceUnitJndiName">java:/accountingEntityManagerFactory</
property>
</component>

 

<component class="com.helloworld.Hello" name="hello">
<property name="name">#{user.name}</property>
</component>

xs:element name="component"

    <xs:element name="component">
        <xs:annotation>
            <xs:documentation> The component tag defines a single Seam component. It may serve as to the root element of
                a fine-grained *.component.xml file. </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" ref="components:property" />
            </xs:sequence>
            <xs:attributeGroup ref="components:attlist.component"/>
            <xs:attributeGroup ref="components:attlist.ejbcomponent"/>

        </xs:complexType>
    </xs:element>

xs:element name="property"

    <xs:element name="property">
        <xs:annotation>
            <xs:documentation>
                A generic property.  The body contains the value or values.
            </xs:documentation>
        </xs:annotation>
        <xs:complexType mixed="true">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="components:key" />
                <xs:element ref="components:value" />
            </xs:choice>
            <xs:attributeGroup ref="components:attlist.property"/>
        </xs:complexType>
    </xs:element>

attlist.property

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

 

    <xs:attributeGroup name="attlist.property">
        <xs:attribute name="name" use="required" type="components:string">
            <xs:annotation>
                <xs:documentation>The property name</xs:documentation>
            </xs:annotation>
        </xs:attribute>
        <xs:attribute name="type" type="components:string" >
            <xs:annotation>
                <xs:documentation>Concrete type to use if the property is multi-valued</xs:documentation>
            </xs:annotation>
        </xs:attribute>
    </xs:attributeGroup>

key

与vlaue配合使用

<property name="issueStatusOptions" type="java.util.LinkedHashMap">
<key>open</key> <value>open issue</value>
<key>resolved</key> <value>issue resolved by developer</value>
<key>closed</key> <value>resolution accepted by user</value>
</property>

 

    <xs:element name="key" type="components:string">
        <xs:annotation>
            <xs:documentation>For map values, the key for the following value</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>
                For list values, the value to be added.
                For map values, the value for the preceding key
            </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));
      }
      return value;
   }

 

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">
        <property name="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>();
    public Map<String,String> getConfigMap() {
        return configMap;
    }

 

特定组件配置

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
package org.jboss.seam.security.management;

import org.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;大写字母换成(-小写)

 

 类似资料: