160 likes | 318 Views
SpringFramework.Java Developer Session. The Peer Frameworks Series - .Net and Java. Solomon Duskis. SpringFramework Release. "Simple thing should be simple, and complex things should be possible." - Alan Kay "Unless simple things are simple, complex things are impossible." - Rod Johnson
E N D
SpringFramework.Java Developer Session The Peer Frameworks Series - .Net and Java Solomon Duskis
SpringFramework Release • "Simple thing should be simple, and complex things should be possible." - Alan Kay "Unless simple things are simple, complex things are impossible." - Rod Johnson • Spring 0.9 announced at The Serverside Java Symposium 2002
Anti-EJB • Spring In Action (Craig Walls) – EJB is complex, not for just being complex. It is complex because it attempts to provide solutions for complex problems • The complex problems include distributed transactions across a variety of system types, including Relational Databases, MoM and Legacy Systems • 2004
Martin Fowloer on IoC Containers • Martin Fowler (IoC Containers and the DIP) – Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels… For this new breed of containers the inversion is about how they lookup a plugin implementation.
People • Interface21 – International consulting company run by Rod Johnson. The Interface21 consultants are the core Spring Framework Developers. • Matt Raible: Creator of AppFuse Strategic adoption in many enterprises moving away from traditional costly, inefficient J2EE approaches This new paradigm is realized through WebLogic Real Time (WLRT). Extensive and growing usage across many industrites including: Retail and Investment banks, Insurance companies (US and Europe) and even Governments. In Banking: 5 out of the world's 10 largest banks are Spring users and Interface21 clients. That's pretty impressive IMO. Spring 2.0: builds on the solid base, pursues vision of POJO-based development and adds new capabilities and makes many tasks more elegant. They're hoping to release 2.0 in May 2006.
Spring DataSource Configuration <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean> <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/myds"/> </bean>
Spring Session Factory Configuration <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean”> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>product.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> </props> </property> </bean>
Spring Dao Configuration <bean id="myProductDao" class="product.ProductDaoImpl"> <property name="sessionFactory" ref="mySessionFactory"/> </bean>
Java Hibernate Dao Code public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao { public Collection loadProductsByCategory(String category) throws DataAccessException { return getHibernateTemplate().find( "from test.Product product where product.category=?", category); } }
Declarative Transaction Configuration <bean id="myTxManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory"/> </bean> <bean id="myTxInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="myTxManager"/> <property name="transactionAttributeSource"> <value> product.ProductService.increasePrice*=PROPAGATION_REQUIRED product.ProductService.someOtherBusinessMethod=PROPAGATION_MANDATORY </value> </property> </bean> <bean id="myProductServiceTarget" class="product.ProductServiceImpl"> <property name="productDao" ref="myProductDao"/> </bean> <bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>product.ProductService</value> </property> <property name="interceptorNames"> <list> <value>myTxInterceptor</value> <value>myProductServiceTarget</value> </list> </property> </bean>
Service Code, Fully Transactional via IoC/AOP public class ProductServiceImpl implements ProductService { private ProductDao productDao; public void setProductDao(ProductDao productDao) { this.productDao = productDao; } public void increasePriceOfAllProductsInCategory(final String category) { List productsToChange = this.productDAO.loadProductsByCategory(category); ... } ... }