Spring Core

Spring Professional Certification

Spring Professional Certification


Ursula Fernes
Diese Lernkarten bieten einen umfassenden Einblick in die Spring Core-Funktionen, insbesondere im Bereich der Konfiguration, JMS, RMI und JMX. Sie behandeln Themen wie die Konfiguration von MBeans, die Nutzung von JMS für asynchrone Nachrichtenverarbeitung und die Implementierung von Remote Services mit RMI. Entwickler, die Spring Core für die Erstellung robuster und skalierbarer Anwendungen nutzen möchten, profitieren von diesen Lernkarten, da sie praktische Beispiele und Best Practices für die Integration und Verwaltung von Services bereitstellen.
Cartes-fiches
227
Utilisateurs
2
Langue
Allemand
Catégorie
Informatique
Niveau
Autres
Créé / Mis à jour
21.10.2012 / 30.07.2016

Cartes-fiches

Spring Security's Tag Libraries

<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

1. display properties of authentication object on view

<security:authentication property="principal.username"/>

2. hide sections of output on view based on role

<security:authorize access="hasRole('ADMIN')">
    Click <a:href="/admin/deleteAll">HERE</a>
    to delete all records.
</security:authorize>

<security:authorize /> possible properties (4)

  1. access (hasRole, hasAnyRole, isAnonymous, isAuthenticated)
  2. url (e.g. /admin/deleteAll)
  3. ifAnyGranted
  4. ifAllGranted

Method security:

=> characteristics
=> 2 ways of configuration

characteristics:
=> Spring Security uses AOP

2 ways of configuration:
=> xml configuration with spring security namespace
=> java configuration with annotations
     (Spring or JSR-250)

Method security with XML configuration

<security:global-method-security>
    <security:potect-pointcut
        expression="execution(* ch.hg..*Services.*(..))"
        access="hasRole('ROLE_USER')" />
</security:global-method-security>

Method security with Spring annotations

<security:global-method-security secured-annotations="enabled">

public class ItemManager {
    @Secured("ROLE_MEMBER")
    public Item findItem(long itemNumber) { ... }
}

NOTE: import org.springframework.security.annotation.Secured

Method security with JSR-250 annotations

 

<security:global-method-security jsr250-annotations="enabled">

public class ItemManager {
    @RolesAllowed("ROLE_MEMBER")
    public Item findItem(long itemNumber) { ... }
}

NOTE: import javax.annotation.security.RolesAllowed

Spring Remote: advantages

  • hide complex installation code
    Spring provides ...
    => exporters (server-side)
    => proxies (client-side)
  • support multiple protocols
  • configure and expose services declaratively

RMI Service Exporters:

goals, characteristics (3)

  1. handles server-side of RMI interaction
  2. declarative exposes an existing service to RMI registry
  3. avoid traditional RMI requirements
    => Service interface doe NOT extend Remote

NOTE: Transferred object however still need to implement the interface java.io.Serializable

RMI Proxy Generator:

goals, characteristics (3)

  1. handles client-side of RMI interaction
  2. communicates with endpoint (Service Exporter)
  3. converts checked RemoteException into Spring's runtime hierarchy of RemoteAccessException
  4. can treat remote EJB's as POJOs

RMI Service Exporter:

configuration example

Configuration starts with a existing POJO service with id "transferService"

<bean class="org.springframework.
    remoting.rmi.RmiServiceExporter">
    <property name="serviceName" 
                        value="transferService" />
    <property name="serviceInterface"
                        value="app.TransferService" />
    <property name="service" ref="transferService" />
    <property name="registryPort" value="1096" />
</bean>

NOTE:
serviceName      = name in RMI registry
service                 = reference to POJO service
serviceInterface = interface classname
registryPort         = optional / defaults to 1099

RMI Proxy Generator:

configuration example

 

<bean id="transferService" class="org.springframework.
    remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceInterface" 
                        value="app.TransferService" />
    <property name="serviceUrl"
                        value="rmi://foo:1096/transferService" />
</bean>

NOTE:
serviceInterface = interface classname
serviceUrl           = endpoint in registry
                                 rmi://server:port/serviceName

 

RMI Service Exporter vs HttpInvoker

  1. RMI vs ...
  2. RmiServiceExporter vs ...
  3. RmiProxyFactoryBean vs ...
  4. Port 1099 vs ...

  1. HTTP POST
  2. HttpInvokerServiceExtporter
    NOTE: bean definition needs name attribute!
  3. HttpInvokerProxyFactoryBean
  4. Port 8080

NOTE: HttpInvoker needs webserver on server side, but NOT on client side!

NOTE: HttpInvoker is a Java-to-Java technology only!

NOTE: Both sides need Spring!

JMS core concepts (6)

  1. Message
  2. Destination
  3. Connection
  4. Session
  5. MessageProducer
  6. MessageConsumer

JMS Message Types (5)

  1. TextMessage
  2. ObjectMessage
  3. MapMessage
  4. BytesMessage
  5. StreamMessage

JMS Destination Types (2)

  1. Queue
    = Point-to-Point messaging / 1 receiver
  2. Topic
    = Publish-Subscribe messaging / several receiver

JmsTemplate

purpose of use (5)

  1. simplifies usage of JMS API
  2. handles exception properly
  3. converts checked exception to runtime equivalents
  4. provides conveniance methods and callbacks
  5. delegates to collaborators to handle some of the work
    => MessageConverter
    => DestinationResolver

Define JmsTemplate Bean

<bean id="jmsTemplate"
    class="org.springframework.jms.core.JmsTemplate"
    p:connectionFactory-ref="connectionFactory"
    p:defaultDestination-ref="destination" />

Property overview:
- connectionFactory = mandatory
- defaultDestination = optinal

 

Receiving messages

JmsTemplate vs MessageListener

JmsTemplate:

  • synchronous message reception
    (= methods are blocking)
  • jmsTemplate.receive()

MessageListener:

  • asynchronous message reception
  • MessageListenerContainers are needed

Message-Driven POJO (MDP)

1. function
2. restriction

  1. acts as receiver for JMS messages
  2. must implement javax.jms.MessageListener

Note: Must be thread-safe when receiving messages from multiple threads!

public class MyListener implements MessageListener
{
    public void onMessage(Message msg) { ... }
}

MessageListener containers

  • 2 types of containers
  • example definition

Spring provides lightweight alternatives to traditional EJB containers.

  1. SimpleMessageListenerContainer
    = no transactionall support
    = creates a fixed number of sessions
  2. DefaultMessageListenerContainer
    = adds transactional capability

<!-- Message-Driven POJO (MDP) -->
<bean id="msgListener" class="example.MyListener"  />

<bean id="jmsContainer class="org.springframework.
    jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="..." />
    <property name="destination" ref="..." />
    <property name="messageListener" ref="msgListener" />
</bean>

JMS Namespace support

  1. top-level element
  2. child element

  1. <jms:listener-container>
    = no required attributes
    = container-type="default" (default)
    = connection-factory="connectionFactory" (default)
  2. <jms:listener destination="..." ref="..." />
    = required attributes are 'destination' and 'ref'
        (destination name / bean name of listener)

JMS Listener as POJO

  • POJO example
  • listener definition

POJO example:
public class OrderService {
    public OrderConfirmation order(Order o) { ... }
}

Listener configuration:
<jms:listener ref="orderService" method="order" destination="..." response-destination="..." />

JMX: Java Management Extension

general goals of JMX (3)

  1. gather information from application in runtime
  2. dynamically reconfigure application
  3. trigger operations in application

JMX Architecture

3 components

  1. MBeanServer
    = maintains a keyed reference to all MBeans
    = acts as broker for communication between
        - multiple local MBeans
        - remote clients and MBeans
  2. MBean
    = object with additional management metadata
        - Attributes = properties
        - Operations = mehtods
  3. MBeanExporter
    = takes existing POJO beans and registers
       them with MBeanServer

Export bean as JMX MBean

=> 3 annotations
=> activate exporter

@ManagedResource(description="...")
public class JmxCounterImpl implements JmxCounter
{
    @ManagedAttribute(description="Counter Value")
    public int getCount() { ... }

    @ManagedOperation(description="Increment value")
    public void increment() { .. }
}

<context:mbean-export />

MBeanExporter:

exporting an existing bean as an MBean

Existing POJO bean:
<bean id="msgService" class="example.MsgService" />

MBeanExporter configuration:
<bean class="org.springframework.
    jmx.export.MBeanExporter">
    <property name="beans">
        <util:map>
            <entry key="bean:name=msgService"
                          value-ref="msgService" />
        </util:map>
    </property>
</bean>

Create an MBeanServer

2 ways

With context namespace:
<context:mbean-server />
=> automatic registration of existing MBeans
     <bean id="..." class="ExistingMBean" />

Declare it explicitly:
<bean id="mBeanServer" class="org.springframework.
    jmx.support.MBeanServerFactoryBean">
    <property name="locateExistingServerIfPossible"
        value="true" />
</bean>
 

Study