Spring in Action 3rd Edition.
Link to it-ebooks to Download
- Spring
- What is in 3.0
- Full support to REST using XML, JSON, RSS
- New annotations for Spring MVC
- Support declarative Validation
- Object to XML mapping
- Application Context
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext
- XmlWebApplicationContext
- Example os a simple spring bean
package com.springinaction.springidol;
public class Juggler implements Performer{
private int beanBags=3;
public Juggler(){ }
public Juggler(int beanBags){
this.beanBags=beanBags;
}
public PoeticJuggler(int beanBags,Poem poem){
this.beanBags = beanBags;
this.poem=poem;
}
public voidperform()throwsPerformanceException{
System.out.println("JUGGLING"+beanBags+"BEANBAGS");
}
}
<bean id="duke" class="com.springinaction.springidol.Juggler">
<constructor-argvalue="15"/>
<constructor-argref="sonnet29"/>
</bean>
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
Performer performer=(Performer)ctx.getBean("duke");
performer.perform();
- List
<bean id="hank" class="com.springinaction.springidol.OneManBand">
<propertyname="instruments">
<set> -- <list> --- <prop>
<refbean="guitar"/>
<refbean="cymbal"/>
<refbean="harmonica"/>
<refbean="harmonica"/>
</set>
or
<map>
<entrykey="GUITAR"value-ref="guitar"/>
<entrykey="CYMBAL"value-ref="cymbal"/>
<entrykey="HARMONICA"value-ref="harmonica"/>
</map>
</property>
</bean>
- Scoping
- Singleton: Only one
- prototype: Always instantiate.
- Used in spring MVC
- request: scope of to HTTP request
- session: scope of to HTTP session
- Used in portal context
- global-session: scope to a global session.
- AutoWiring
- Find a already created object to resolve ambiguity
- The moon is bright tonight. Which moon. Everybody knows which moon.
- For Kinds
- byName: Try to find beans that have the same name
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist" autowire="byName">
<propertyname="song"value="JingleBells"/>
</bean>
<bean id="instrument" class="com.springinaction.springidol.Saxophone"/>
- Tell spring to consider all properties and look for beans that are declared with the same name as the properties.
- byType: Try to find a beans by type
- Constructor: Try to match up a constructor of the wired bean with beans whose types are assigned to the constructor arguments
- autodetect: Attempts to apply constructor autowiring first. It that fails bytype will tried.
- Wiring with annotations
<context:annotation-config/> This tells Spring that you intend to use annotation-based wiring in spring.
- 3 annottions
- @Autowired
- @Inject
- @Resource
- Other Annotations
- @Component: Stteriotype indicating that a bean is a Spring Component
Should use <context:component-scan> to avoid using the XML configuration.
@Component("eddie")
public classInstrumentalistimplementsPerformer{ ..
- @Controller: A Spring MVC controller
- @Repository: Class that defines a data repository
- @Service: Defines a service
- Declaring a simple bean
- @Bean
public Performer duke(){
return newJuggler();
}
@Bean tells spring that this method will return an object that should be registered as a bean in Spring application context.
The id will be the method name.
- Aspect-Oriented Spring
- Cross-cutting programming
- Usually used in: Loggin, Transaction, Security
- Terminology
- Advice: Indicates the job that a aspect does. Defines WHAT and WHEN of a aspect.
- 5 kinds of advice:
- Before, After, After-returning, After-throwing, Around(before and after)
- Join Points: Point of execution of the application where an aspect can be plugged in(method, exception, field modified).
- Pointcuts: Matches one or more join points at which advice should be woven(entrelaçado).
- Aspects: Merge of advice and pointcuts.
- Weaving
- Compile time
- Classload time
- Runtime
Nenhum comentário:
Postar um comentário