spring j2ee

21
Spring framework Motto: Musíte rozbít vejce když chcete udělat omeletu Spring framework training materials by Roman Pichlík is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License . 1 Sunday 13 May 2012

Upload: roman-pichlik

Post on 28-Nov-2014

1.289 views

Category:

Technology


0 download

DESCRIPTION

* Podporované technologie* Integrace s EJB

TRANSCRIPT

Page 1: Spring J2EE

Spring frameworkMotto: Musíte rozbít vejce když chcete udělat omeletu

Spring framework training materials by Roman Pichlík is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

1Sunday 13 May 2012

Page 2: Spring J2EE

Java EEKurz jak používat Java EE a nezbláznit se z toho

2Sunday 13 May 2012

Page 3: Spring J2EE

Co Spring nabízí

• EJB

• JMS

• JNDI

• JCA

• Remoting

• Napojení na služby

3Sunday 13 May 2012

Page 4: Spring J2EE

EJB

• Zjednoduššené vytváření EJB

• SLSB, SLFB, MDB

• AbstractStatelessSessionBean

• AbstractStatefulSessionBean

• AbstractJmsMessageDrivenBean

4Sunday 13 May 2012

Page 5: Spring J2EE

Jak na vlastní beanu

5Sunday 13 May 2012

Page 6: Spring J2EE

Vlastní business rozhraní

public interface ReservationService {

public boolean reserveBook(Long bookId, Date from, Date to, User user);}

6Sunday 13 May 2012

Page 7: Spring J2EE

Implementace

@Service(value="reservationService")public class ReservationServiceImpl implements ReservationService { @Autowired private BookService bookService;

public boolean reserveBook(Long bookId, Date from, Date to, User user) { Book book = bookService.getBook(bookId); if(book == null) { throw new RuntimeException("Sorry, the book doesn't exist"); } return true; }}

7Sunday 13 May 2012

Spring bean, mozno testovat

Page 8: Spring J2EE

EJB facade

8Sunday 13 May 2012

Page 9: Spring J2EE

Vlastní fasáda

public class ReservationServiceEJBFacade extends AbstractStatelessSessionBean implements ReservationService { private static final long serialVersionUID = 1L;

private ReservationService reservationService;

@Override protected void onEjbCreate() throws CreateException { reservationService = getBeanFactory().getBean("reservationService"); }

public boolean reserveBook(Long bookId, Date from, Date to, User user) { return reservationService.reserveBook(bookId, from, to, user); }}

Získání spring beany

EJB má stejné rozhraní jako Spring beana

Předek, zaručující Spring podport

9Sunday 13 May 2012

-stejny business interface nam umuznuje delegovat volani

Page 10: Spring J2EE

Home a Local Interfaceimport javax.ejb.CreateException;import javax.ejb.EJBLocalHome;

public interface ReservationServiceHome extends EJBLocalHome {

public ReservationServiceLocal create() throws CreateException;}

import javax.ejb.EJBLocalObject;

import cz.sweb.pichlik.ReservationService;

public interface ReservationServiceLocal extends EJBLocalObject, ReservationService {}

10Sunday 13 May 2012

Page 11: Spring J2EE

Jak se Spring startuje

<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" version="2.1" id="ejb-jar_ID"> <description>Book service EJB facade</description> <display-name>ejbs</display-name> <enterprise-beans> <session> <description>Book service EJB facade</description> <ejb-name>ReservationServiceEJBFacade</ejb-name> <local-home>cz.sweb.pichlik.ejb.ReservationServiceHome</local-home> <local>cz.sweb.pichlik.ejb.ReservationServiceLocal</local> <ejb-class>cz.sweb.pichlik.ejb.ReservationServiceEJBFacade</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> <env-entry> <env-entry-name>ejb/BeanFactoryPath</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>classpath*:META-INF/businessContext.xml</env-entry-value> </env-entry> </session>

</enterprise-beans>

</ejb-jar>

Určuje kontext, který se bude bootovat

11Sunday 13 May 2012

- kazda instance beany si vytvari vlastni kontext.

Page 12: Spring J2EE

JNDI

12Sunday 13 May 2012

Page 13: Spring J2EE

Lookup generic, local&remote<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource" cache="true" resource-ref="true" lookup-on-startup="false" expected-type="" proxy-interface="com.myapp.Foo"/>

<jee:local-slsb id="complexLocalEjb" jndi-name="ejb/RentalServiceBean" business-interface="com.foo.service.RentalService" cache-home="true" lookup-home-on-startup="true" resource-ref="true">

<jee:remote-slsb id="complexRemoteEjb" jndi-name="ejb/MyRemoteBean" business-interface="com.foo.service.RentalService" cache-home="true" lookup-home-on-startup="true" resource-ref="true" home-interface="com.foo.service.RentalService" refresh-home-on-connect-failure="true">

13Sunday 13 May 2012

Page 14: Spring J2EE

Remoting

14Sunday 13 May 2012

Page 15: Spring J2EE

Podporované protokoly

• Client/Server

• RMI

• Hessian/Burlap

• HttpInvoker

• JAX-RPC, JAX-WS

15Sunday 13 May 2012

Page 16: Spring J2EE

HttpInvoker service <servlet> <servlet-name>remoting</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet>

<servlet-mapping> <servlet-name>remoting</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping>

<bean name="/ReservationService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="web.reservationService"/> <property name="serviceInterface" value="cz.sweb.pichlik.ReservationService"/></bean>

web.xml

remoting-servlet.xml

16Sunday 13 May 2012

Page 17: Spring J2EE

HttpInvoker client

<bean id="client.reservationService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/servlet/remoting/ReservationService"/> <property name="serviceInterface" value="cz.sweb.pichlik.ReservationService"/></bean>

17Sunday 13 May 2012

Page 18: Spring J2EE

JMS

18Sunday 13 May 2012

Page 19: Spring J2EE

Podporované typy

• MDB

• AbstractJmsMessageDrivenBean

• Spring MDB

• Client

19Sunday 13 May 2012

Page 20: Spring J2EE

Mail

20Sunday 13 May 2012

Page 21: Spring J2EE

import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.MailSender;

@Servicepublic class EmailExample { @Autowired private MailSender mailSender; @Autowired private SimpleMailMessage templateMessage;

public void sayHello() { SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage); msg.setTo(order.getCustomer().getEmailAddress()); msg.setText(“HelloWorld”); this.mailSender.send(msg); }}

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="mail.mycompany.com"/></bean>

<!-- this is a template message that we can pre-load with default state --><bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="[email protected]"/> <property name="subject" value="Your order"/></bean>

21Sunday 13 May 2012org.springframework.mail.javamail.MimeMessageHelper