Spring Core
Spring Professional Certification
Spring Professional Certification
-
- 1 / 227
-
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)
- access (hasRole, hasAnyRole, isAnonymous, isAuthenticated)
- url (e.g. /admin/deleteAll)
- ifAnyGranted
- 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)
- handles server-side of RMI interaction
- declarative exposes an existing service to RMI registry
- 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)
- handles client-side of RMI interaction
- communicates with endpoint (Service Exporter)
- converts checked RemoteException into Spring's runtime hierarchy of RemoteAccessException
- 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
- RMI vs ...
- RmiServiceExporter vs ...
- RmiProxyFactoryBean vs ...
- Port 1099 vs ...
- HTTP POST
- HttpInvokerServiceExtporter
NOTE: bean definition needs name attribute! - HttpInvokerProxyFactoryBean
- 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)
- Message
- Destination
- Connection
- Session
- MessageProducer
- MessageConsumer
JMS Message Types (5)
- TextMessage
- ObjectMessage
- MapMessage
- BytesMessage
- StreamMessage
JMS Destination Types (2)
- Queue
= Point-to-Point messaging / 1 receiver - Topic
= Publish-Subscribe messaging / several receiver
JmsTemplate
purpose of use (5)
- simplifies usage of JMS API
- handles exception properly
- converts checked exception to runtime equivalents
- provides conveniance methods and callbacks
- 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
- acts as receiver for JMS messages
- 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.
- SimpleMessageListenerContainer
= no transactionall support
= creates a fixed number of sessions - 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
- top-level element
- child element
- <jms:listener-container>
= no required attributes
= container-type="default" (default)
= connection-factory="connectionFactory" (default) - <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)
- gather information from application in runtime
- dynamically reconfigure application
- trigger operations in application
JMX Architecture
3 components
- MBeanServer
= maintains a keyed reference to all MBeans
= acts as broker for communication between
- multiple local MBeans
- remote clients and MBeans - MBean
= object with additional management metadata
- Attributes = properties
- Operations = mehtods - 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>