java configuration deep dive with spring

25
Josh Long (之春) @starbuxman joshlong.com [email protected] slideshare.net/joshlong github.com/joshlong http://spring.io JAVA CONFIGURATION DEEP DIVE WITH

Upload: josh-long

Post on 25-Jan-2015

797 views

Category:

Technology


1 download

DESCRIPTION

a good primer on Spring's Java configuration style. I present simialar content to this as an introduction to many other talks.

TRANSCRIPT

Page 1: Java Configuration Deep Dive with Spring

Josh Long (⻰龙之春)@starbuxman joshlong.com

[email protected] slideshare.net/joshlong

github.com/joshlonghttp://spring.io

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

Page 2: Java Configuration Deep Dive with Spring

REST DESIGN WITH SPRING

Spring Developer AdvocateAbout Josh Long (⻰龙之春)

@starbuxman [email protected] |

Jean Claude van Damme! Java mascot Duke some thing’s I’ve authored...

Page 3: Java Configuration Deep Dive with Spring

Not confidential. Tell everyone.

It’s Easy to Use Spring’s Annotations in Your Code

3

Page 4: Java Configuration Deep Dive with Spring

Not confidential. Tell everyone.

I want Database Access ... with Hibernate 4 Support

4

@Servicepublic class CustomerService {

public Customer getCustomerById( long customerId) { ... }

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

}

Page 5: Java Configuration Deep Dive with Spring

Not confidential. Tell everyone.

I want Database Access ... with Hibernate 4 Support

5

@Servicepublic 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; }

}

Page 6: Java Configuration Deep Dive with Spring

Not confidential. Tell everyone.

I want Database Access ... with Hibernate 4 Support

6

@Servicepublic 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; }

}

Page 7: Java Configuration Deep Dive with Spring

Not confidential. Tell everyone.

I want Declarative Cache Management...

7

@Servicepublic class CustomerService {

@Inject private SessionFactory sessionFactory;

@Transactional(readOnly = true) @Cacheable(“customers”) public Customer getCustomerById( long customerId) { ... }

...

}

Page 8: Java Configuration Deep Dive with Spring

Not confidential. Tell everyone.

I want a RESTful Endpoint...

8

package org.springsource.examples.spring31.web;..

@RestControllerclass CustomerController {

@Inject CustomerService customerService;

@RequestMapping(value = "/customer/{id}" ) Customer customerById( @PathVariable Integer id ) { return customerService.getCustomerById(id); } ...}

Page 9: Java Configuration Deep Dive with Spring

Not confidential. Tell everyone.

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

9

Page 10: Java Configuration Deep Dive with Spring

The Spring ApplicationContext

10

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 XML:

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"); }}

From Java Configuration

Page 11: Java Configuration Deep Dive with Spring

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” );

Page 12: Java Configuration Deep Dive with Spring

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” );

Page 13: Java Configuration Deep Dive with Spring

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 );

Page 14: Java Configuration Deep Dive with Spring

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” );

Page 15: Java Configuration Deep Dive with Spring

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 );

Page 16: Java Configuration Deep Dive with Spring

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 );

Page 17: Java Configuration Deep Dive with Spring

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 );

Page 18: Java Configuration Deep Dive with Spring

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” );

Page 19: Java Configuration Deep Dive with Spring

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 );

Page 20: Java Configuration Deep Dive with Spring

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” );

Page 21: Java Configuration Deep Dive with Spring

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 );

Page 22: Java Configuration Deep Dive with Spring

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” );

Page 23: Java Configuration Deep Dive with Spring

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 );

Page 24: Java Configuration Deep Dive with Spring

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() { ... }}

Page 25: Java Configuration Deep Dive with Spring

REST DESIGN WITH SPRING

Questions

@starbuxman [email protected]

[email protected]/joshlong

slideshare.net/joshlong ?Any