spring framework core 2017
TRANSCRIPT
SpringFrameworkCoreDiegoArmandoGómezMosquera.
Agenda• QueesSpring• ArquitecturadeSpring• PrincipalesmódulosdeSpring
– Inyeccióndedependencias– Programaciónorientadaaspectos.
• ConfigurandoaplicaciónusandoSpring– InstalarelcontenedordeInversióndeControldeSpring(IoC)– CrearyconfigurarunBeanenelcontenedordeInversióndeControl.– UsandoelAuto-WritingBeanconXML– UsandoelAuto-WritingBeanconAnotaciones.
QueesSpring?
• Unframework contenedorlivianobasadoenlatécnicaInversióndeControl(IoC) yunaimplementacióndedesarrollosegúnelparadigmaOrientadoaAspectos(AOP)
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).
ArquitecturadeSpring
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
Inyeccióndedependencias
• DependencyInjection(DI)• Patróndediseñoorientadoaobjetos,enelquesesuministranobjetosaunaclaseenlugardeserlapropiaclasequiencreeelobjeto.
• EltérminofueacuñadoporMartinFowler.
SinInyeccióndedependencias
publicclassVehiculo{
privateMotormotor=newMotor();
/**@retornalavelocidaddelvehículo*/publicDoubleenAceleracionDePedal(intpresionDePedal){motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();Doublevelocidad=...//realizaelcálculoreturnvelocidad;
}
}
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;}
}
ConInyeccióndedependenciassincontenedoromotor
//seomitelaclaseMotoryaquenoesrelevanteparaesteejemplo
publicclassVehiculoFactory{
publicVehiculoconstruyeVehiculo(){Vehiculovehiculo=newVehiculo();Motormotor=newMotor();vehiculo.setMotor(motor);returnvehiculo;
}
}
ConInyeccióndedependenciasusandoSpring
publicclassVehiculo{
@AutowiredprivateMotormotor;
publicDoubleenAceleracionDePedal(intpresionDePedal){Doublevelocidad=null;motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();velocidad=...//realizaelcálculoreturnvelocidad;
}}
ConInyeccióndedependenciasconEJB
publicclassVehiculo{
@EJBprivateMotormotor;
publicDoubleenAceleracionDePedal(intpresionDePedal){Doublevelocidad=null;motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();velocidad=...//realizaelcálculoreturnvelocidad;
}}
ConInyeccióndedependenciasconEJB
publicclassVehiculo{
@InjectprivateMotormotor;
publicDoubleenAceleracionDePedal(intpresionDePedal){Doublevelocidad=null;motor.setPresionDePedal(presionDePedal);inttorque=motor.getTorque();velocidad=...//realizaelcálculoreturnvelocidad;
}}
Inversióndecontrol• InversionofControl(IoC)• Esunmétododeprogramaciónenelqueelflujodeejecucióndeun
programaseinvierterespectoalosmétodosdeprogramacióntradicionales,enlosquelainteracciónseexpresadeformaimperativahaciendollamadasaprocedimientos(procedurecalls)ofunciones.Tradicionalmenteelprogramadorespecificalasecuenciadedecisionesyprocedimientosquepuedendarseduranteelciclodevidadeunprogramamediantellamadasafunciones.Ensulugar,enlainversióndecontrolseespecificanrespuestasdeseadasasucesososolicitudesdedatosconcretas,dejandoquealgúntipodeentidadoarquitecturaexternalleveacabolasaccionesdecontrolqueserequieranenelordennecesarioyparaelconjuntodesucesosquetenganqueocurrir.
Inversióndecontrol
• Principio de Hollywood– “no nos llames; nosotros te llamaremos”
• Lasimplementacionesmascomunes:– Callback– Listener
Laboratorio inyeccióndedependencias
ConfigurandoaplicaciónusandoSpring
• AgregardepenciasdeSpring• InstalarelcontenedordeInversióndeControldeSpring(IoC)
• CrearyconfigurarunBeanenelcontenedordeInversióndeControl.
• UsandoelAuto-WritingBeanconXML• UsandoelAuto-WritingBeanconAnotaciones.
SpringconMaven<dependencies>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.8.RELEASE</version>
</dependency></dependencies>
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>
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>
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>
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>
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>
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>
EscenariosdeUso
Typical full-fledged Springwebapplication
Springmiddle-tierusingathird-Web
Remotingusagescenario
EJBs- WrappingexistingPOJOs
Informacióndecontacto
• SitioWeb:www.vortexbird.com• Blog:http://blog.vortexbird.com• Contactovíamail:[email protected]• Direccion:Calle18#118-241Oficina21• Teléfonos:+57- (3164824629)