2. 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...
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.