SlideShare a Scribd company logo
Josh Long (⻰龙之春)
@starbuxman
joshlong.com
josh.long@springsource.com
slideshare.net/joshlong
github.com/joshlong
http://spring.io

J AVA C O N F I G U R AT I O N D E E P D I V E

WITH
REST DESIGN WITH SPRING

About Josh Long (⻰龙之春)
Spring Developer Advocate
@starbuxman
Jean Claude
van Damme!

| josh.long@springsource.com
Java mascot Duke

some thing’s I’ve authored...
It’s Easy to Use Spring’s Annotations in Your Code

Not confidential. Tell everyone.

3
I want Database Access ... with Hibernate 4 Support
@Service
public class CustomerService {
public Customer getCustomerById( long customerId)
...
}

{

public Customer createCustomer( String firstName, String lastName, Date date){
...
}
}

Not confidential. Tell everyone.

4
I want Database Access ... with Hibernate 4 Support
@Service
public class CustomerService {
@Inject // JSR 330
private SessionFactory sessionFactory;
public Customer createCustomer(String firstName,
String lastName,
Date signupDate) {
Customer customer = new Customer();
customer.setFirstName(firstName);
customer.setLastName(lastName);
customer.setSignupDate(signupDate);
sessionFactory.getCurrentSession().save(customer);
return customer;
}
}

Not confidential. Tell everyone.

5
I want Database Access ... with Hibernate 4 Support
@Service
public class CustomerService {
@Inject
private SessionFactory sessionFactory;
@Transactional
public Customer createCustomer(String firstName,
String lastName,
Date signupDate) {
Customer customer = new Customer();
customer.setFirstName(firstName);
customer.setLastName(lastName);
customer.setSignupDate(signupDate);
sessionFactory.getCurrentSession().save(customer);
return customer;
}
}

Not confidential. Tell everyone.

6
I want Declarative Cache Management...
@Service
public class CustomerService {
@Inject
private SessionFactory sessionFactory;
@Transactional(readOnly = true)
@Cacheable(“customers”)
public Customer getCustomerById( long customerId)
...
}

{

...
}

Not confidential. Tell everyone.

7
I want a RESTful Endpoint...
package org.springsource.examples.spring31.web;
..
@RestController
class CustomerController {
@Inject CustomerService customerService;
@RequestMapping(value = "/customer/{id}" )
Customer customerById( @PathVariable Integer id ) {
return customerService.getCustomerById(id);
}
...
}

Not confidential. Tell everyone.

8
...But Where’d the SessionFactory come from?

Not confidential. Tell everyone.

9
The Spring ApplicationContext
From XML:
public class Main {
public static void main(String [] args) throws Throwable {
ApplicationContext ctx =
new ClassPathXmlApplication( “my-config.xml” );
CustomerService serviceReference = ctx.getBean( CustomerService.class );
Customer customer = serviceReference.createCustomer( "Juergen", "Hoeller");
}
}

From Java Configuration
public class Main {
public static void main(String [] args) throws Throwable {
ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
CustomerService serviceReference = ctx.getBean( CustomerService.class );
Customer customer = serviceReference.createCustomer( "Juergen", "Hoeller");
}
}

10
A Quick Primer on Configuration
....
<beans>
<tx:annotation-driven transaction-manager = "txManager" />
<context:component-scan base-package = "org.springsource.examples.spring31.services" />
<context:property-placeholder properties = "config.properties" />
<bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id = "dataSource" class = "..SimpleDriverDataSource">
<property name= "userName" value = "${ds.user}"/>
...
</bean>
</beans>

ApplicationContext ctx =
new ClassPathXmlApplication( “service-config.xml” );
Not confidential. Tell everyone.
A Quick Primer on Configuration
....
<beans>
<tx:annotation-driven transaction-manager = "txManager" />
<context:component-scan base-package = "org.springsource.examples.spring31.services" />
<context:property-placeholder properties = "config.properties" />
<bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id = "dataSource" class = "..SimpleDriverDataSource">
<property name= "userName" value = "${ds.user}"/>
...
</bean>
</beans>

ApplicationContext ctx =
new ClassPathXmlApplication( “service-config.xml” );
Not confidential. Tell everyone.
A Quick Primer on Configuration

@Configuration
@PropertySource("/config.properties")
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {CustomerService.class})
public class ServicesConfiguration {
@Inject private Environment environment;
@Bean
public PlatformTransactionManager txManager() throws Exception {
return new HibernateTransactionManager(this.sessionFactory());
}
@Bean
public SessionFactory sessionFactory() { ... }
@Bean
public DataSource dataSource(){
SimpleDriverDataSource sds = new SimpleDriverDataSource();
sds.setUserName( this.environment.getProperty( “ds.user”));
// ...
return sds;
}
}

ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
Not confidential. Tell everyone.
A Quick Primer on Configuration
....
<beans>
<tx:annotation-driven transaction-manager = "txManager" />
<context:component-scan base-package = "org.springsource.examples.spring31.services" />
<context:property-placeholder properties = "config.properties" />
<bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id = "dataSource" class = "..SimpleDriverDataSource">
<property name= "userName" value = "${ds.user}"/>
...
</bean>
</beans>

ApplicationContext ctx =
new ClassPathXmlApplication( “service-config.xml” );
Not confidential. Tell everyone.
A Quick Primer on Configuration

@Configuration
@PropertySource("/config.properties")
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {CustomerService.class})
public class ServicesConfiguration {
@Inject private Environment environment;
@Bean
public PlatformTransactionManager txManager() throws Exception {
return new HibernateTransactionManager(this.sessionFactory());
}
@Bean
public SessionFactory sessionFactory() { ... }
@Bean
public DataSource dataSource(){
SimpleDriverDataSource sds = new SimpleDriverDataSource();
sds.setUserName( this.environment.getProperty( “ds.user”));
// ...
return sds;
}
}

ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
Not confidential. Tell everyone.
A Quick Primer on Configuration : Tangent on Injection

@Configuration
@PropertySource("/config.properties")
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {CustomerService.class})
public class ServicesConfiguration {
@Inject private Environment environment;
@Bean
public PlatformTransactionManager txManager() throws Exception {
return new HibernateTransactionManager(this.sessionFactory());
}
@Bean
public SessionFactory sessionFactory() { ... }
@Bean
public DataSource dataSource(){
SimpleDriverDataSource sds = new SimpleDriverDataSource();
sds.setUserName( this.environment.getProperty( “ds.user”));
// ...
return sds;
}
}

ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
Not confidential. Tell everyone.
A Quick Primer on Configuration : Tangent on Injection

@Configuration
@PropertySource("/config.properties")
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {CustomerService.class})
public class ServicesConfiguration {
@Inject private Environment environment;
@Bean
public PlatformTransactionManager txManager( SessionFactory sessionFactory ) throws Exception {
return new HibernateTransactionManager( sessionFactory );
}
@Bean
public SessionFactory sessionFactory() { ... }
@Bean
public DataSource dataSource(){
SimpleDriverDataSource sds = new SimpleDriverDataSource();
sds.setUserName( this.environment.getProperty( “ds.user”));
// ...
return sds;
}
}

ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
Not confidential. Tell everyone.
A Quick Primer on Configuration
....
<beans>
<tx:annotation-driven transaction-manager = "txManager" />
<context:component-scan base-package = "org.springsource.examples.spring31.services" />
<context:property-placeholder properties = "config.properties" />
<bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id = "dataSource" class = "..SimpleDriverDataSource">
<property name= "userName" value = "${ds.user}"/>
...
</bean>
</beans>

ApplicationContext ctx =
new ClassPathXmlApplication( “service-config.xml” );
Not confidential. Tell everyone.
A Quick Primer on Configuration

@Configuration
@PropertySource("/config.properties")
@EnableTransactionManagement
@ComponentScan (basePackageClasses = {CustomerService.class})
public class ServicesConfiguration {
@Inject private Environment environment;
@Bean
public PlatformTransactionManager txManager() throws Exception {
return new HibernateTransactionManager(this.sessionFactory());
}
@Bean
public SessionFactory sessionFactory() { ... }
@Bean
public DataSource dataSource(){
SimpleDriverDataSource sds = new SimpleDriverDataSource();
sds.setUserName( this.environment.getProperty( “ds.user”));
// ...
return sds;
}
}

ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
Not confidential. Tell everyone.
A Quick Primer on Configuration
....
<beans>
<tx:annotation-driven transaction-manager = "txManager" />
<context:component-scan base-package = "org.springsource.examples.spring31.services" />
<context:property-placeholder properties = "config.properties" />
<bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id = "dataSource" class = "..SimpleDriverDataSource">
<property name= "userName" value = "${ds.user}"/>
...
</bean>
</beans>

ApplicationContext ctx =
new ClassPathXmlApplication( “service-config.xml” );
Not confidential. Tell everyone.
A Quick Primer on Configuration

@Configuration
@PropertySource("/config.properties")
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {CustomerService.class})
public class ServicesConfiguration {
@Inject private Environment environment;
@Bean
public PlatformTransactionManager txManager() throws Exception {
return new HibernateTransactionManager(this.sessionFactory());
}
@Bean
public SessionFactory sessionFactory() { ... }
@Bean
public DataSource dataSource(){
SimpleDriverDataSource sds = new SimpleDriverDataSource();
sds.setUserName( this.environment.getProperty( “ds.user”));
// ...
return sds;
}
}

ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
Not confidential. Tell everyone.
A Quick Primer on Configuration
....
<beans>
<tx:annotation-driven transaction-manager = "txManager" />
<context:component-scan base-package = "org.springsource.examples.spring31.services" />
<context:property-placeholder properties = "config.properties" />
<bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "sessionFactory" />
</bean>
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id = "dataSource" class = "..SimpleDriverDataSource">
<property name= "userName" value = "${ds.user}"/>
...
</bean>
</beans>

ApplicationContext ctx =
new ClassPathXmlApplication( “service-config.xml” );
Not confidential. Tell everyone.
A Quick Primer on Configuration

@Configuration
@PropertySource("/config.properties")
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {CustomerService.class})
public class ServicesConfiguration {
@Inject private Environment environment;
@Bean
public PlatformTransactionManager txManager() throws Exception {
return new HibernateTransactionManager(this.sessionFactory());
}
@Bean
public SessionFactory sessionFactory() { ... }
@Bean
public DataSource dataSource(){
SimpleDriverDataSource sds = new SimpleDriverDataSource();
sds.setUserName( this.environment.getProperty( “ds.user”));
// ...
return sds;
}
}

ApplicationContext ctx =
new AnnotationConfigApplicationContext( ServicesConfiguration.class );
Not confidential. Tell everyone.
Test Context Framework
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader=AnnotationConfigContextLoader.class,
classes={TransferServiceConfig.class, DataConfig.class})
@ActiveProfiles("dev")
public class TransferServiceTest {
@Autowired
private TransferService transferService;
@Test
public void testTransferService() {
...
}
}

24
REST DESIGN WITH SPRING

@starbuxman
josh.long@springsource.com
josh@joshlong.com
github.com/joshlong
slideshare.net/joshlong

Any

?

Questions

More Related Content

PDF
Economies of Scaling Software
PDF
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
PDF
the Spring 4 update
PDF
Extending spring
KEY
Multi Client Development with Spring
PDF
Have You Seen Spring Lately?
KEY
Multi Client Development with Spring
PDF
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Economies of Scaling Software
Multi Client Development with Spring for SpringOne 2GX 2013 with Roy Clarkson
the Spring 4 update
Extending spring
Multi Client Development with Spring
Have You Seen Spring Lately?
Multi Client Development with Spring
Developing Modern Java Web Applications with Java EE 7 and AngularJS

What's hot (19)

ODP
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
PDF
Spring 4 - A&BP CC
PDF
Spring Mvc Rest
PDF
Meetup Performance
KEY
#NewMeetup Performance
PDF
Getting Started with WebSockets and Server-Sent Events
PPT
Jsp/Servlet
PPTX
Rest with Java EE 6 , Security , Backbone.js
PPTX
Angular beans
PDF
Future of Web Apps: Google Gears
PDF
Simple REST with Dropwizard
KEY
Multi client Development with Spring
PDF
Building a Backend with Flask
PPTX
Building Your First App with MongoDB
PPTX
Java Play Restful JPA
PPT
Intoduction to Play Framework
PPT
CTS Conference Web 2.0 Tutorial Part 2
PDF
Scala and Spring
PDF
Dropwizard and Friends
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Spring 4 - A&BP CC
Spring Mvc Rest
Meetup Performance
#NewMeetup Performance
Getting Started with WebSockets and Server-Sent Events
Jsp/Servlet
Rest with Java EE 6 , Security , Backbone.js
Angular beans
Future of Web Apps: Google Gears
Simple REST with Dropwizard
Multi client Development with Spring
Building a Backend with Flask
Building Your First App with MongoDB
Java Play Restful JPA
Intoduction to Play Framework
CTS Conference Web 2.0 Tutorial Part 2
Scala and Spring
Dropwizard and Friends
Ad

Similar to Java Configuration Deep Dive with Spring (20)

PPT
Spring and Cloud Foundry; a Marriage Made in Heaven
PDF
Durable functions 2.0 (2019-10-10)
PDF
Implement Service Broker with Spring Boot #cf_tokyo
PDF
Dropwizard
PDF
Java Svet - Communication Between Android App Components
PDF
Java Svet - Communication Between Android App Components
PDF
Symfony2 - from the trenches
PDF
softshake 2014 - Java EE
PDF
Symfony2 from the Trenches
PDF
Advanced #2 networking
PDF
Integrating SAP the Java EE Way - JBoss One Day talk 2012
KEY
Spring in the Cloud - using Spring with Cloud Foundry
PDF
PPTX
Test your microservices with REST-Assured
PDF
Modern Android app library stack
PDF
Azure Durable Functions (2019-03-30)
KEY
A Walking Tour of (almost) all of Springdom
PPTX
Developing your first application using FI-WARE
PDF
May 2010 - RestEasy
PDF
Taking Web Apps Offline
Spring and Cloud Foundry; a Marriage Made in Heaven
Durable functions 2.0 (2019-10-10)
Implement Service Broker with Spring Boot #cf_tokyo
Dropwizard
Java Svet - Communication Between Android App Components
Java Svet - Communication Between Android App Components
Symfony2 - from the trenches
softshake 2014 - Java EE
Symfony2 from the Trenches
Advanced #2 networking
Integrating SAP the Java EE Way - JBoss One Day talk 2012
Spring in the Cloud - using Spring with Cloud Foundry
Test your microservices with REST-Assured
Modern Android app library stack
Azure Durable Functions (2019-03-30)
A Walking Tour of (almost) all of Springdom
Developing your first application using FI-WARE
May 2010 - RestEasy
Taking Web Apps Offline
Ad

More from Joshua Long (19)

PDF
Bootiful Code with Spring Boot
PDF
Microservices with Spring Boot
PDF
Boot It Up
PDF
the Spring Update from JavaOne 2013
PDF
REST APIs with Spring
PDF
The spring 32 update final
KEY
Integration and Batch Processing on Cloud Foundry
KEY
using Spring and MongoDB on Cloud Foundry
PDF
Spring in-the-cloud
KEY
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
KEY
Spring Batch Behind the Scenes
KEY
Cloud Foundry Bootcamp
PPT
Spring 3.1: a Walking Tour
PDF
Extending Spring for Custom Usage
PPT
Using Spring's IOC Model
PPT
Enterprise Integration and Batch Processing on Cloud Foundry
PDF
a Running Tour of Cloud Foundry
PDF
Cloud Foundry, Spring and Vaadin
PDF
Messaging sz
Bootiful Code with Spring Boot
Microservices with Spring Boot
Boot It Up
the Spring Update from JavaOne 2013
REST APIs with Spring
The spring 32 update final
Integration and Batch Processing on Cloud Foundry
using Spring and MongoDB on Cloud Foundry
Spring in-the-cloud
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
Spring Batch Behind the Scenes
Cloud Foundry Bootcamp
Spring 3.1: a Walking Tour
Extending Spring for Custom Usage
Using Spring's IOC Model
Enterprise Integration and Batch Processing on Cloud Foundry
a Running Tour of Cloud Foundry
Cloud Foundry, Spring and Vaadin
Messaging sz

Recently uploaded (20)

PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Newfamily of error-correcting codes based on genetic algorithms
PDF
Event Presentation Google Cloud Next Extended 2025
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Omni-Path Integration Expertise Offered by Nor-Tech
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
REPORT: Heating appliances market in Poland 2024
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Advanced Soft Computing BINUS July 2025.pdf
Chapter 2 Digital Image Fundamentals.pdf
Smarter Business Operations Powered by IoT Remote Monitoring
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Newfamily of error-correcting codes based on genetic algorithms
Event Presentation Google Cloud Next Extended 2025
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
Big Data Technologies - Introduction.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Omni-Path Integration Expertise Offered by Nor-Tech
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Reimagining Insurance: Connected Data for Confident Decisions.pdf
REPORT: Heating appliances market in Poland 2024
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf

Java Configuration Deep Dive with Spring

  • 2. REST DESIGN WITH SPRING About Josh Long (⻰龙之春) Spring Developer Advocate @starbuxman Jean Claude van Damme! | [email protected] Java mascot Duke some thing’s I’ve authored...
  • 3. It’s Easy to Use Spring’s Annotations in Your Code Not confidential. Tell everyone. 3
  • 4. I want Database Access ... with Hibernate 4 Support @Service public class CustomerService { public Customer getCustomerById( long customerId) ... } { public Customer createCustomer( String firstName, String lastName, Date date){ ... } } Not confidential. Tell everyone. 4
  • 5. I want Database Access ... with Hibernate 4 Support @Service public class CustomerService { @Inject // JSR 330 private SessionFactory sessionFactory; public Customer createCustomer(String firstName, String lastName, Date signupDate) { Customer customer = new Customer(); customer.setFirstName(firstName); customer.setLastName(lastName); customer.setSignupDate(signupDate); sessionFactory.getCurrentSession().save(customer); return customer; } } Not confidential. Tell everyone. 5
  • 6. I want Database Access ... with Hibernate 4 Support @Service public class CustomerService { @Inject private SessionFactory sessionFactory; @Transactional public Customer createCustomer(String firstName, String lastName, Date signupDate) { Customer customer = new Customer(); customer.setFirstName(firstName); customer.setLastName(lastName); customer.setSignupDate(signupDate); sessionFactory.getCurrentSession().save(customer); return customer; } } Not confidential. Tell everyone. 6
  • 7. I want Declarative Cache Management... @Service public class CustomerService { @Inject private SessionFactory sessionFactory; @Transactional(readOnly = true) @Cacheable(“customers”) public Customer getCustomerById( long customerId) ... } { ... } Not confidential. Tell everyone. 7
  • 8. I want a RESTful Endpoint... package org.springsource.examples.spring31.web; .. @RestController class CustomerController { @Inject CustomerService customerService; @RequestMapping(value = "/customer/{id}" ) Customer customerById( @PathVariable Integer id ) { return customerService.getCustomerById(id); } ... } Not confidential. Tell everyone. 8
  • 9. ...But Where’d the SessionFactory come from? Not confidential. Tell everyone. 9
  • 10. The Spring ApplicationContext From XML: public class Main { public static void main(String [] args) throws Throwable { ApplicationContext ctx = new ClassPathXmlApplication( “my-config.xml” ); CustomerService serviceReference = ctx.getBean( CustomerService.class ); Customer customer = serviceReference.createCustomer( "Juergen", "Hoeller"); } } From Java Configuration public class Main { public static void main(String [] args) throws Throwable { ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); CustomerService serviceReference = ctx.getBean( CustomerService.class ); Customer customer = serviceReference.createCustomer( "Juergen", "Hoeller"); } } 10
  • 11. A Quick Primer on Configuration .... <beans> <tx:annotation-driven transaction-manager = "txManager" /> <context:component-scan base-package = "org.springsource.examples.spring31.services" /> <context:property-placeholder properties = "config.properties" /> <bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name = "sessionFactory" ref = "sessionFactory" /> </bean> <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ... </bean> <bean id = "dataSource" class = "..SimpleDriverDataSource"> <property name= "userName" value = "${ds.user}"/> ... </bean> </beans> ApplicationContext ctx = new ClassPathXmlApplication( “service-config.xml” ); Not confidential. Tell everyone.
  • 12. A Quick Primer on Configuration .... <beans> <tx:annotation-driven transaction-manager = "txManager" /> <context:component-scan base-package = "org.springsource.examples.spring31.services" /> <context:property-placeholder properties = "config.properties" /> <bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name = "sessionFactory" ref = "sessionFactory" /> </bean> <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ... </bean> <bean id = "dataSource" class = "..SimpleDriverDataSource"> <property name= "userName" value = "${ds.user}"/> ... </bean> </beans> ApplicationContext ctx = new ClassPathXmlApplication( “service-config.xml” ); Not confidential. Tell everyone.
  • 13. A Quick Primer on Configuration @Configuration @PropertySource("/config.properties") @EnableTransactionManagement @ComponentScan(basePackageClasses = {CustomerService.class}) public class ServicesConfiguration { @Inject private Environment environment; @Bean public PlatformTransactionManager txManager() throws Exception { return new HibernateTransactionManager(this.sessionFactory()); } @Bean public SessionFactory sessionFactory() { ... } @Bean public DataSource dataSource(){ SimpleDriverDataSource sds = new SimpleDriverDataSource(); sds.setUserName( this.environment.getProperty( “ds.user”)); // ... return sds; } } ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); Not confidential. Tell everyone.
  • 14. A Quick Primer on Configuration .... <beans> <tx:annotation-driven transaction-manager = "txManager" /> <context:component-scan base-package = "org.springsource.examples.spring31.services" /> <context:property-placeholder properties = "config.properties" /> <bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name = "sessionFactory" ref = "sessionFactory" /> </bean> <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ... </bean> <bean id = "dataSource" class = "..SimpleDriverDataSource"> <property name= "userName" value = "${ds.user}"/> ... </bean> </beans> ApplicationContext ctx = new ClassPathXmlApplication( “service-config.xml” ); Not confidential. Tell everyone.
  • 15. A Quick Primer on Configuration @Configuration @PropertySource("/config.properties") @EnableTransactionManagement @ComponentScan(basePackageClasses = {CustomerService.class}) public class ServicesConfiguration { @Inject private Environment environment; @Bean public PlatformTransactionManager txManager() throws Exception { return new HibernateTransactionManager(this.sessionFactory()); } @Bean public SessionFactory sessionFactory() { ... } @Bean public DataSource dataSource(){ SimpleDriverDataSource sds = new SimpleDriverDataSource(); sds.setUserName( this.environment.getProperty( “ds.user”)); // ... return sds; } } ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); Not confidential. Tell everyone.
  • 16. A Quick Primer on Configuration : Tangent on Injection @Configuration @PropertySource("/config.properties") @EnableTransactionManagement @ComponentScan(basePackageClasses = {CustomerService.class}) public class ServicesConfiguration { @Inject private Environment environment; @Bean public PlatformTransactionManager txManager() throws Exception { return new HibernateTransactionManager(this.sessionFactory()); } @Bean public SessionFactory sessionFactory() { ... } @Bean public DataSource dataSource(){ SimpleDriverDataSource sds = new SimpleDriverDataSource(); sds.setUserName( this.environment.getProperty( “ds.user”)); // ... return sds; } } ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); Not confidential. Tell everyone.
  • 17. A Quick Primer on Configuration : Tangent on Injection @Configuration @PropertySource("/config.properties") @EnableTransactionManagement @ComponentScan(basePackageClasses = {CustomerService.class}) public class ServicesConfiguration { @Inject private Environment environment; @Bean public PlatformTransactionManager txManager( SessionFactory sessionFactory ) throws Exception { return new HibernateTransactionManager( sessionFactory ); } @Bean public SessionFactory sessionFactory() { ... } @Bean public DataSource dataSource(){ SimpleDriverDataSource sds = new SimpleDriverDataSource(); sds.setUserName( this.environment.getProperty( “ds.user”)); // ... return sds; } } ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); Not confidential. Tell everyone.
  • 18. A Quick Primer on Configuration .... <beans> <tx:annotation-driven transaction-manager = "txManager" /> <context:component-scan base-package = "org.springsource.examples.spring31.services" /> <context:property-placeholder properties = "config.properties" /> <bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name = "sessionFactory" ref = "sessionFactory" /> </bean> <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ... </bean> <bean id = "dataSource" class = "..SimpleDriverDataSource"> <property name= "userName" value = "${ds.user}"/> ... </bean> </beans> ApplicationContext ctx = new ClassPathXmlApplication( “service-config.xml” ); Not confidential. Tell everyone.
  • 19. A Quick Primer on Configuration @Configuration @PropertySource("/config.properties") @EnableTransactionManagement @ComponentScan (basePackageClasses = {CustomerService.class}) public class ServicesConfiguration { @Inject private Environment environment; @Bean public PlatformTransactionManager txManager() throws Exception { return new HibernateTransactionManager(this.sessionFactory()); } @Bean public SessionFactory sessionFactory() { ... } @Bean public DataSource dataSource(){ SimpleDriverDataSource sds = new SimpleDriverDataSource(); sds.setUserName( this.environment.getProperty( “ds.user”)); // ... return sds; } } ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); Not confidential. Tell everyone.
  • 20. A Quick Primer on Configuration .... <beans> <tx:annotation-driven transaction-manager = "txManager" /> <context:component-scan base-package = "org.springsource.examples.spring31.services" /> <context:property-placeholder properties = "config.properties" /> <bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name = "sessionFactory" ref = "sessionFactory" /> </bean> <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ... </bean> <bean id = "dataSource" class = "..SimpleDriverDataSource"> <property name= "userName" value = "${ds.user}"/> ... </bean> </beans> ApplicationContext ctx = new ClassPathXmlApplication( “service-config.xml” ); Not confidential. Tell everyone.
  • 21. A Quick Primer on Configuration @Configuration @PropertySource("/config.properties") @EnableTransactionManagement @ComponentScan(basePackageClasses = {CustomerService.class}) public class ServicesConfiguration { @Inject private Environment environment; @Bean public PlatformTransactionManager txManager() throws Exception { return new HibernateTransactionManager(this.sessionFactory()); } @Bean public SessionFactory sessionFactory() { ... } @Bean public DataSource dataSource(){ SimpleDriverDataSource sds = new SimpleDriverDataSource(); sds.setUserName( this.environment.getProperty( “ds.user”)); // ... return sds; } } ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); Not confidential. Tell everyone.
  • 22. A Quick Primer on Configuration .... <beans> <tx:annotation-driven transaction-manager = "txManager" /> <context:component-scan base-package = "org.springsource.examples.spring31.services" /> <context:property-placeholder properties = "config.properties" /> <bean id = "txManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name = "sessionFactory" ref = "sessionFactory" /> </bean> <bean id = "sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean"> ... </bean> <bean id = "dataSource" class = "..SimpleDriverDataSource"> <property name= "userName" value = "${ds.user}"/> ... </bean> </beans> ApplicationContext ctx = new ClassPathXmlApplication( “service-config.xml” ); Not confidential. Tell everyone.
  • 23. A Quick Primer on Configuration @Configuration @PropertySource("/config.properties") @EnableTransactionManagement @ComponentScan(basePackageClasses = {CustomerService.class}) public class ServicesConfiguration { @Inject private Environment environment; @Bean public PlatformTransactionManager txManager() throws Exception { return new HibernateTransactionManager(this.sessionFactory()); } @Bean public SessionFactory sessionFactory() { ... } @Bean public DataSource dataSource(){ SimpleDriverDataSource sds = new SimpleDriverDataSource(); sds.setUserName( this.environment.getProperty( “ds.user”)); // ... return sds; } } ApplicationContext ctx = new AnnotationConfigApplicationContext( ServicesConfiguration.class ); Not confidential. Tell everyone.
  • 24. Test Context Framework @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( loader=AnnotationConfigContextLoader.class, classes={TransferServiceConfig.class, DataConfig.class}) @ActiveProfiles("dev") public class TransferServiceTest { @Autowired private TransferService transferService; @Test public void testTransferService() { ... } } 24
  • 25. REST DESIGN WITH SPRING @starbuxman [email protected] [email protected] github.com/joshlong slideshare.net/joshlong Any ? Questions