spring framework core 2017

31
Spring Framework Core Diego Armando Gómez Mosquera. [email protected] 2017

Upload: vortexbird

Post on 22-Jan-2018

112 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Spring framework core 2017

SpringFrameworkCoreDiegoArmandoGómezMosquera.

[email protected]

Page 2: Spring framework core 2017

Agenda• QueesSpring• ArquitecturadeSpring• PrincipalesmódulosdeSpring

– Inyeccióndedependencias– Programaciónorientadaaspectos.

• ConfigurandoaplicaciónusandoSpring– InstalarelcontenedordeInversióndeControldeSpring(IoC)– CrearyconfigurarunBeanenelcontenedordeInversióndeControl.– UsandoelAuto-WritingBeanconXML– UsandoelAuto-WritingBeanconAnotaciones.

Page 3: Spring framework core 2017

QueesSpring?

• Unframework contenedorlivianobasadoenlatécnicaInversióndeControl(IoC) yunaimplementacióndedesarrollosegúnelparadigmaOrientadoaAspectos(AOP)

Page 4: Spring framework core 2017

QueesSpring?• Framework: porque define la forma de desarrollaraplicaciones JavaEE, dando soporte y simplificandocomplejidad propia del software empresarial.

• Inversión de Control (IoC): promueve el bajoacoplamiento a partir de la inyección de dependencias(DI) entre los objetos (relaciones).

• Orientación a Aspectos (AOP): presenta una estructurasimplificada para el desarrollo y utilización de aspectos(módulos multiple object crosscutting).

Page 5: Spring framework core 2017

ArquitecturadeSpring

Page 6: Spring framework core 2017

SpringFrameworkArtifactsGroupId ArtifactId Description

org.springframework spring-aop Proxy-basedAOPsupportorg.springframework spring-aspects AspectJbasedaspectsorg.springframework spring-beans Beanssupport,includingGroovyorg.springframework spring-context Applicationcontextruntime,includingschedulingandremotingabstractions

org.springframework spring-context-support Supportclassesforintegratingcommonthird-partylibrariesintoaSpringapplicationcontextorg.springframework spring-core Coreutilities,usedbymanyotherSpringmodulesorg.springframework spring-expression SpringExpressionLanguage(SpEL)org.springframework spring-instrument InstrumentationagentforJVMbootstrappingorg.springframework spring-instrument-tomcat InstrumentationagentforTomcatorg.springframework spring-jdbc JDBCsupportpackage,includingDataSource setupandJDBCaccesssupportorg.springframework spring-jms JMSsupportpackage,includinghelperclassestosend/receiveJMSmessagesorg.springframework spring-messaging Supportformessagingarchitecturesandprotocolsorg.springframework spring-orm Object/RelationalMapping,includingJPAandHibernatesupportorg.springframework spring-oxm Object/XMLMappingorg.springframework spring-test SupportforunittestingandintegrationtestingSpringcomponents

org.springframework spring-tx Transactioninfrastructure,includingDAOsupportandJCAintegration

org.springframework spring-web Foundationalwebsupport,includingwebclientandweb-basedremoting

org.springframework spring-webmvc HTTP-basedModel-View-ControllerandRESTendpointsforServletstacks

org.springframework spring-webmvc-portlet MVCimplementationtobeusedinaPortletenvironment

org.springframework spring-websocket WebSocket andSockJS infrastructure,includingSTOMPmessagingsupport

Page 7: Spring framework core 2017

Inyeccióndedependencias

• DependencyInjection(DI)• Patróndediseñoorientadoaobjetos,enelquesesuministranobjetosaunaclaseenlugardeserlapropiaclasequiencreeelobjeto.

• EltérminofueacuñadoporMartinFowler.

Page 8: Spring framework core 2017

SinInyeccióndedependencias

publicclassVehiculo{

privateMotormotor=newMotor();

/**@retornalavelocidaddelvehículo*/publicDoubleenAceleracionDePedal(intpresionDePedal){motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();Doublevelocidad=...//realizaelcálculoreturnvelocidad;

}

}

Page 9: Spring framework core 2017

ConInyeccióndedependenciassincontenedoromotor

publicclassVehiculo{

privateMotormotor=null;

publicsetMotor(Motormotor){this.motor=motor;

}

/**@retornalavelocidaddelvehículo*/publicDoubleenAceleracionDePedal(intpresionDePedal){Doublevelocidad=null;if(null!=motor){motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();velocidad=...//realizaelcálculo

}

returnvelocidad;}

}

Page 10: Spring framework core 2017

ConInyeccióndedependenciassincontenedoromotor

//seomitelaclaseMotoryaquenoesrelevanteparaesteejemplo

publicclassVehiculoFactory{

publicVehiculoconstruyeVehiculo(){Vehiculovehiculo=newVehiculo();Motormotor=newMotor();vehiculo.setMotor(motor);returnvehiculo;

}

}

Page 11: Spring framework core 2017

ConInyeccióndedependenciasusandoSpring

publicclassVehiculo{

@AutowiredprivateMotormotor;

publicDoubleenAceleracionDePedal(intpresionDePedal){Doublevelocidad=null;motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();velocidad=...//realizaelcálculoreturnvelocidad;

}}

Page 12: Spring framework core 2017

ConInyeccióndedependenciasconEJB

publicclassVehiculo{

@EJBprivateMotormotor;

publicDoubleenAceleracionDePedal(intpresionDePedal){Doublevelocidad=null;motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();velocidad=...//realizaelcálculoreturnvelocidad;

}}

Page 13: Spring framework core 2017

ConInyeccióndedependenciasconEJB

publicclassVehiculo{

@InjectprivateMotormotor;

publicDoubleenAceleracionDePedal(intpresionDePedal){Doublevelocidad=null;motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();velocidad=...//realizaelcálculoreturnvelocidad;

}}

Page 14: Spring framework core 2017

Inversióndecontrol• InversionofControl(IoC)• Esunmétododeprogramaciónenelqueelflujodeejecucióndeun

programaseinvierterespectoalosmétodosdeprogramacióntradicionales,enlosquelainteracciónseexpresadeformaimperativahaciendollamadasaprocedimientos(procedurecalls)ofunciones.Tradicionalmenteelprogramadorespecificalasecuenciadedecisionesyprocedimientosquepuedendarseduranteelciclodevidadeunprogramamediantellamadasafunciones.Ensulugar,enlainversióndecontrolseespecificanrespuestasdeseadasasucesososolicitudesdedatosconcretas,dejandoquealgúntipodeentidadoarquitecturaexternalleveacabolasaccionesdecontrolqueserequieranenelordennecesarioyparaelconjuntodesucesosquetenganqueocurrir.

Page 15: Spring framework core 2017

Inversióndecontrol

• Principio de Hollywood– “no nos llames; nosotros te llamaremos”

• Lasimplementacionesmascomunes:– Callback– Listener

Page 16: Spring framework core 2017

Laboratorio inyeccióndedependencias

Page 17: Spring framework core 2017

ConfigurandoaplicaciónusandoSpring

• AgregardepenciasdeSpring• InstalarelcontenedordeInversióndeControldeSpring(IoC)

• CrearyconfigurarunBeanenelcontenedordeInversióndeControl.

• UsandoelAuto-WritingBeanconXML• UsandoelAuto-WritingBeanconAnotaciones.

Page 18: Spring framework core 2017

SpringconMaven<dependencies>

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.8.RELEASE</version>

</dependency></dependencies>

Page 19: Spring framework core 2017

applicationContext.xml• EstearchivopermitehacerlasconfiguracionesdelascapacidadessoportadasporSpring

<?xmlversion="1.0"encoding="UTF-8"?><beans

xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<beanname="persona"class="com.vortexbird.demo.modelo.Persona">

</bean>

</beans>

Page 20: Spring framework core 2017

ConfigurarunBean

<?xmlversion="1.0"encoding="UTF-8"?><beans

xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<beanname="persona"class="com.vortexbird.demo.modelo.Persona"/></beans>

Page 21: Spring framework core 2017

ConfigurandoelBeanconatributos<?xmlversion="1.0"encoding="UTF-8"?><beans

xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<beanname="persona"class="com.vortexbird.demo.modelo.Persona"scope="prototype"><propertyname="id"value="142020"/><propertyname="nombre"value="JuanPerez"/><propertyname="mail"value="[email protected]"/>

</bean>

</beans>

Page 22: Spring framework core 2017

ConfigurandoelBean conconstructor<?xmlversion="1.0"encoding="UTF-8"?><beans

xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<beanname="persona"class="com.vortexbird.demo.modelo.Persona"scope="prototype">

<constructor-argname="id"value="90893"/><constructor-argname="nombre"value="DiegoGomez"/><constructor-argname="mail"value="[email protected]"/>

</bean>

</beans>

Page 23: Spring framework core 2017

UsandoelAuto-Writing Bean conXML

<?xmlversion="1.0"encoding="UTF-8"?><beans

xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<beanname="vehiculo"class="com.vortexbird.demo.modelo.Vehiculo"autowire="byType"/>

<beanname="motor"class="com.vortexbird.demo.modelo.Motor"/>

</beans>

Page 24: Spring framework core 2017

UsandoelAuto-WritingBeanconAnotaciones

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<context:component-scanbase-package="com.vortexbird.demo"/></beans>

Page 25: Spring framework core 2017

EscenariosdeUso

Page 26: Spring framework core 2017

Typical full-fledged Springwebapplication

Page 27: Spring framework core 2017

Springmiddle-tierusingathird-Web

Page 28: Spring framework core 2017

Remotingusagescenario

Page 29: Spring framework core 2017

EJBs- WrappingexistingPOJOs

Page 30: Spring framework core 2017

Informacióndecontacto

• SitioWeb:www.vortexbird.com• Blog:http://blog.vortexbird.com• Contactovíamail:[email protected]• Direccion:Calle18#118-241Oficina21• Teléfonos:+57- (3164824629)

Page 31: Spring framework core 2017