java configuration deep dive with spring
DESCRIPTION
a good primer on Spring's Java configuration style. I present simialar content to this as an introduction to many other talks.TRANSCRIPT
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
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...
Not confidential. Tell everyone.
It’s Easy to Use Spring’s Annotations in Your Code
3
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){ ... }
}
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; }
}
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; }
}
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) { ... }
...
}
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); } ...}
Not confidential. Tell everyone.
...But Where’d the SessionFactory come from?
9
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
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
....<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 );
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() { ... }}
REST DESIGN WITH SPRING
Questions
@starbuxman [email protected]
[email protected]/joshlong
slideshare.net/joshlong ?Any