z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
r | reload slides |
n | toggle notes |
p | run preshow |
spring ~= spring-framework
spring != spring-framework
spring >> spring-framework
spring ==
spring-framework
spring-security
spring-integration
spring-batch
spring-data-*
spring-social
spring ==
spring-roo
spring-amqp
spring-webflow
spring-ws
spring-net
...
spring == spring-*
spring ==
·
·
POJOs + 3rd party
components
·
POJOs + 3rd party
components
configuration
instructions
·
POJOs + 3rd party
components
↓
configuration ┌─────────────────────┐
instructions → │ the Spring container│
└─────────────────────┘
·
POJOs + 3rd party
components
↓
configuration ┌─────────────────────┐
instructions → │ (ApplicationContext)│
└─────────────────────┘
·
POJOs + 3rd party
components
↓
configuration ┌─────────────────────┐
instructions → │ the Spring container│
└─────────────────────┘
·
POJOs + 3rd party
components
↓
configuration ┌─────────────────────┐
instructions → │ the Spring container│
└─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
POJOs + 3rd party
components
↓
configuration ┌─────────────────────┐
instructions → │ the Spring container│
└─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
POJOs + 3rd party
components
↓
<beans> ┌─────────────────────┐
XML → │ the Spring container│
└─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
<beans/>
<beans>
</beans>
<beans>
<bean id="foo" class="com.foo.Foo"/>
</beans>
<beans>
<bean id="foo" class="com.foo.Foo"/>
<bean id="bar" class="com.foo.Bar">
<property name="foo" ref="foo"/>
</bean>
</beans>
<beans>
<bean id="foo" class="com.foo.Foo"/>
<bean id="bar" class="com.foo.Bar">
<property name="foo" ref="foo"/>
</bean>
<bean .../>
</beans>
<beans>
<bean id="foo" class="com.foo.Foo"/>
<bean id="bar" class="com.foo.Bar">
<property name="foo" ref="foo"/>
</bean>
<bean .../>
<bean .../>
</beans>
<beans>
<bean id="foo" class="com.foo.Foo"/>
<bean id="bar" class="com.foo.Bar">
<property name="foo" ref="foo"/>
</bean>
<bean .../>
<bean .../>
<bean .../>
</beans>
<beans>
<bean id="foo" class="com.foo.Foo"/>
<bean id="bar" class="com.foo.Bar">
<property name="foo" ref="foo"/>
</bean>
<bean .../>
<bean .../>
<bean .../>
<import resource="more-beans.xml"/>
</beans>
<beans>
xml<beans>
xml<namespace:*>
xml helps<tx:annotation-driven/>
<aop:aspectj-autoproxy/>
<jee:jndi-lookup/>
<util:list/>
<namespace:*>
xml<namespace:*>
xml POJOs + 3rd party
components
↓
<beans> ┌─────────────────────┐
XML → │ the Spring container│
└─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
POJOs + 3rd party
components
↓
<beans> + ┌─────────────────────┐
<namespace:*> → │ the Spring container│
XML └─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
POJOs + 3rd party
components
↓
@Component- ┌─────────────────────┐
scanning → │ the Spring container│
+ @Autowired └─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
@Component
public class Foo {
// ...
}
@Component
public class Bar {
private Foo foo;
@Autowired
public void setFoo(Foo foo) {
this.foo = foo;
}
}
@Component
-scanning + @Autowired
@Controllers
@Component
-scanning + @Autowired
@Qualifier
)package com.foo;
@Component
public class Bar {
@Autowired
public void setFoo(Foo foo) { ... }
}
package com.foo;
@Component
public class Bar {
@Autowired
public void setFoo(Foo foo) { ... }
}
<context:component-scan base-package="com.foo"/>
POJOs + 3rd party
components
↓
@Component- ┌─────────────────────┐
scanning → │ the Spring container│
+ @Autowired └─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
POJOs + 3rd party
components
↓
@Configuration ┌─────────────────────┐
+ @Bean → │ the Spring container│
└─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
@Configuration
public class AppConfig {
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
return quoteService;
}
}
// @Configuration classes =~ <beans/> documents
@Configuration
public class AppConfig {
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
return quoteService;
}
}
@Configuration
public class AppConfig {
// @Bean methods ~= <bean/> elements
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
return quoteService;
}
}
@Configuration
public class AppConfig {
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = // instantiate
return quoteService;
}
}
@Configuration
public class AppConfig {
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
// configure
return quoteService;
}
}
@Configuration
public class AppConfig {
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
return quoteService; // object managed by Spring
}
}
@Import(OtherConfig.class) // =~ <import/>
@Configuration
public class AppConfig {
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
return quoteService;
}
}
@Import(OtherConfig.class)
@Configuration
public class AppConfig {
@Autowired QuoteSource quoteSource; // from OtherConfig
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
return quoteService;
}
}
@Import(OtherConfig.class)
@Configuration
public class AppConfig {
@Autowired QuoteSource quoteSource;
@Bean
public QuoteService quoteService() {
RealTimeQuoteService quoteService = ...;
quoteService.setQuoteSource(quoteSource); // inject
return quoteService;
}
}
public class Main {
public static void main(String... args) {
}
}
public class Main {
public static void main(String... args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
QuoteService quoteService =
ctx.getBean(QuoteService.class);
System.out.println(quoteService.currentValue("AAPL"));
}
}
public class Main {
public static void main(String... args) {
// bootstrap the Spring container
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
QuoteService quoteService =
ctx.getBean(QuoteService.class);
System.out.println(quoteService.currentValue("AAPL"));
}
}
public class Main {
public static void main(String... args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
// retrieve the bean we want to use in typesafe fashion
QuoteService quoteService =
ctx.getBean(QuoteService.class);
System.out.println(quoteService.currentValue("AAPL"));
}
}
public class Main {
public static void main(String... args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
QuoteService quoteService =
ctx.getBean(QuoteService.class);
// use the bean however desired
System.out.println(quoteService.currentValue("AAPL"));
}
}
@Configuration
@Configuration
<namespace:*/>
@Enable*
@Enable*
(and friends)
@EnableTransactionManagement
@EnableAspectJAutoProxy
@EnableLoadTimeWeaving
@EnableScheduling
@EnableAsync
@EnableWebMvc
@ComponentScan
package com.foo;
@Component
public class Bar {
@Autowired
public void setFoo(Foo foo) { ... }
}
package com.foo;
@Component
public class Bar {
@Autowired
public void setFoo(Foo foo) { ... }
}
<context:component-scan base-package="com.foo"/>
package com.foo;
@Component
public class Bar {
@Autowired
public void setFoo(Foo foo) { ... }
}
@ComponentScan("com.foo")
package com.foo;
@Repository
public class WidgetRepo {
@Transactional
public void add(Widget widget) { ... }
}
package com.foo;
@Repository
public class WidgetRepo {
@Transactional
public void add(Widget widget) { ... }
}
<tx:annotation-driven/>
package com.foo;
@Repository
public class WidgetRepo {
@Transactional
public void add(Widget widget) { ... }
}
@EnableTransactionManagement
@EnableScheduling
package com.foo;
public class ChatterBox {
public void saySomething() {
System.out.println(randomWord());
}
}
package com.foo;
public class ChatterBox {
@Scheduled(fixedRate=1000)
public void saySomething() {
System.out.println(randomWord());
}
}
package com.foo;
public class ChatterBox {
// (@Scheduled has been
@Scheduled(fixedRate=1000) // around since Spring 3.0)
public void saySomething() {
System.out.println(randomWord());
}
}
package com.foo;
public class ChatterBox {
// "hey Spring, call this
@Scheduled(fixedRate=1000) // method every second"
public void saySomething() {
System.out.println(randomWord());
}
}
package com.foo.config;
@Configuration
public class Config {
@Bean
public ChatterBox chatterBox() {
return new ChatterBox();
}
}
package com.foo.config;
@Configuration
@EnableScheduling
public class Config {
@Bean
public ChatterBox chatterBox() {
return new ChatterBox();
}
}
package com.foo.config;
@Configuration
@EnableScheduling // look for and process @Scheduled
public class Config {
@Bean
public ChatterBox chatterBox() {
return new ChatterBox();
}
}
package com.foo.config;
@Configuration
@EnableScheduling
public class Config {
@Bean
public ChatterBox chatterBox() { // Spring ensures
return new ChatterBox(); // saySomething() method is
} // called once per second
}
@Enable*
benefits@EnableMongoRepositories
for dependency injection
for enabling features of the Spring container
for Hibernate integration, too
<bean id="sessionFactory"
class="org.sfwk.orm.hib3.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="annotatedClasses">
<list>
<value>com.foo.Person</value>
<value>com.foo.Account</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
@Bean
public SessionFactory sessionFactory() {
return new LocalSessionFactoryBuilder(dataSource())
.addAnnotatedClasses(Person.class, Account.class)
.buildSessionFactory();
}
and not just Spring XML
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="..."
xsi:schemaLocation="..."
version="2.0">
<persistence-unit name="sample">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
@Bean
public LocalContainerEntityManagerFactoryBean emf() {
LocalContainerEntityManagerFactoryBean emf =
new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPersistenceXmlLocation(
"classpath:META-INF/persistence.xml");
return emf;
}
@Bean
public LocalContainerEntityManagerFactoryBean emf() {
LocalContainerEntityManagerFactoryBean emf =
new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPersistenceXmlLocation(
"classpath:META-INF/persistence.xml"); // no more!
return emf;
}
@Bean
public LocalContainerEntityManagerFactoryBean emf() {
LocalContainerEntityManagerFactoryBean emf =
new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
emf.setPackagesToScan("com.foo.domain");
return emf;
}
@Bean
public LocalContainerEntityManagerFactoryBean emf() {
LocalContainerEntityManagerFactoryBean emf =
new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource());
// scan classpath for JPA @Entity types
emf.setPackagesToScan("com.foo.domain");
return emf;
}
and not just Spring XML
<web-app>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/dispatcher-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<web-app>
public class WebInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext ctx =
new AnnotationConfigWebApplicationContext()
ctx.register(DispatcherConfig.class);
ServletRegistration.Dynamic dispatcher =
container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
WebApplicationInitializer
ServletContainerInitializer
if you want it*
POJOs + 3rd party
components
↓
@Configuration ┌─────────────────────┐
+ @Bean → │ the Spring container│
└─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
POJOs + 3rd party
components
↓
@Configuration ┌─────────────────────┐
+ @Bean → │ the Spring container│
+ @Enable* └─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
POJOs + 3rd party
components
(hybrid) ↓
<bean> + ┌─────────────────────┐
@Component + → │ the Spring container│
@Configuration └─────────────────────┘
⇊
┌─────────────────────────┐
│ fully configured system │
│ ready for use │
└─────────────────────────┘
@Component
+ @Autowired
for "your" components@Bean
for third-party components@Enable
to control framework features@Configuration
classesspring-scala
projectspring-integration
Scala & Groovy DSLsspring-framework
3.2 RC1 is now out