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

What's hot (19)

ODP
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Masoud Kalali
 
PDF
Spring 4 - A&BP CC
JWORKS powered by Ordina
 
PDF
Spring Mvc Rest
Craig Walls
 
PDF
Meetup Performance
Greg Whalin
 
KEY
#NewMeetup Performance
Justin Cataldo
 
PDF
Getting Started with WebSockets and Server-Sent Events
Arun Gupta
 
PPT
Jsp/Servlet
Sunil OS
 
PPTX
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
PPTX
Angular beans
Bessem Hmidi
 
PDF
Future of Web Apps: Google Gears
dion
 
PDF
Simple REST with Dropwizard
Andrei Savu
 
KEY
Multi client Development with Spring
Joshua Long
 
PDF
Building a Backend with Flask
Make School
 
PPTX
Building Your First App with MongoDB
MongoDB
 
PPTX
Java Play Restful JPA
Faren faren
 
PPT
Intoduction to Play Framework
Knoldus Inc.
 
PPT
CTS Conference Web 2.0 Tutorial Part 2
Geoffrey Fox
 
PDF
Scala and Spring
Eberhard Wolff
 
PDF
Dropwizard and Friends
Yun Zhi Lin
 
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Masoud Kalali
 
Spring 4 - A&BP CC
JWORKS powered by Ordina
 
Spring Mvc Rest
Craig Walls
 
Meetup Performance
Greg Whalin
 
#NewMeetup Performance
Justin Cataldo
 
Getting Started with WebSockets and Server-Sent Events
Arun Gupta
 
Jsp/Servlet
Sunil OS
 
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
Angular beans
Bessem Hmidi
 
Future of Web Apps: Google Gears
dion
 
Simple REST with Dropwizard
Andrei Savu
 
Multi client Development with Spring
Joshua Long
 
Building a Backend with Flask
Make School
 
Building Your First App with MongoDB
MongoDB
 
Java Play Restful JPA
Faren faren
 
Intoduction to Play Framework
Knoldus Inc.
 
CTS Conference Web 2.0 Tutorial Part 2
Geoffrey Fox
 
Scala and Spring
Eberhard Wolff
 
Dropwizard and Friends
Yun Zhi Lin
 

Similar to Java Configuration Deep Dive with Spring (20)

PDF
The Spring 4 Update - Josh Long
jaxconf
 
PPTX
Configuring jpa in a Spring application
Jayasree Perilakkalam
 
KEY
A Walking Tour of (almost) all of Springdom
Joshua Long
 
DOC
Integration Of Springs Framework In Hibernates
Sunkara Prakash
 
DOC
Integration Of Springs Framework In Hibernates
Sunkara Prakash
 
PPT
Spring and Cloud Foundry; a Marriage Made in Heaven
Joshua Long
 
PDF
Hibernate Presentation
guest11106b
 
PPTX
Hibernate
ksain
 
PDF
Advance Java Training in Bangalore | Best Java Training Institute
TIB Academy
 
PPT
Introduction to hibernate
Muhammad Zeeshan
 
PPTX
Spring & hibernate
Santosh Kumar Kar
 
PPTX
Hibernate
Prashant Kalkar
 
PPTX
Session 3 - hibernate - Configuration with Annotations.pptx
ASRPANDEY
 
PDF
hibernate-presentation-1196607644547952-4.pdf
Mytrux1
 
PDF
Hibernate presentation
Luis Goldster
 
PDF
Hibernate 3
Rajiv Gupta
 
PPT
Spring 3.1: a Walking Tour
Joshua Long
 
PDF
Spring db-access mod03
Guo Albert
 
PPT
Hibernate
Preetha Ganapathi
 
PDF
the Spring Update from JavaOne 2013
Joshua Long
 
The Spring 4 Update - Josh Long
jaxconf
 
Configuring jpa in a Spring application
Jayasree Perilakkalam
 
A Walking Tour of (almost) all of Springdom
Joshua Long
 
Integration Of Springs Framework In Hibernates
Sunkara Prakash
 
Integration Of Springs Framework In Hibernates
Sunkara Prakash
 
Spring and Cloud Foundry; a Marriage Made in Heaven
Joshua Long
 
Hibernate Presentation
guest11106b
 
Hibernate
ksain
 
Advance Java Training in Bangalore | Best Java Training Institute
TIB Academy
 
Introduction to hibernate
Muhammad Zeeshan
 
Spring & hibernate
Santosh Kumar Kar
 
Hibernate
Prashant Kalkar
 
Session 3 - hibernate - Configuration with Annotations.pptx
ASRPANDEY
 
hibernate-presentation-1196607644547952-4.pdf
Mytrux1
 
Hibernate presentation
Luis Goldster
 
Hibernate 3
Rajiv Gupta
 
Spring 3.1: a Walking Tour
Joshua Long
 
Spring db-access mod03
Guo Albert
 
the Spring Update from JavaOne 2013
Joshua Long
 
Ad

More from Joshua Long (18)

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

Recently uploaded (20)

PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PPTX
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
PDF
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PDF
Open Source Milvus Vector Database v 2.6
Zilliz
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PPTX
𝙳𝚘𝚠𝚗𝚕𝚘𝚊𝚍—Wondershare Filmora Crack 14.0.7 + Key Download 2025
sebastian aliya
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PPTX
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PDF
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
The Growing Value and Application of FME & GenAI
Safe Software
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
Open Source Milvus Vector Database v 2.6
Zilliz
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
𝙳𝚘𝚠𝚗𝚕𝚘𝚊𝚍—Wondershare Filmora Crack 14.0.7 + Key Download 2025
sebastian aliya
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
UiPath Agentic AI ile Akıllı Otomasyonun Yeni Çağı
UiPathCommunity
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 

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