If you are like me, I like my project to always use the latest framework versions. You can easily modify the version under the properties tag at the bottom of the snippet.
Your pom.xml should have the following:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.webflow</groupId> <artifactId>spring-webflow</artifactId> <version>2.3.2.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.webflow</groupId> <artifactId>spring-js-resources</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> <properties> <aspectj.version>1.7.2</aspectj.version> <hibernate.version>4.1.10.Final</hibernate.version> <spring.version>3.2.1.RELEASE</spring.version> </properties> |
You can now do a mvn eclipse:eclipse to update your project to include the jars. Now it’s time to edit your spring context to make use of these. We will be gunning for annotation rather than XML configuration.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="a.b.catalog" /> <context:annotation-config /> <mvc:annotation-driven /> <tx:annotation-driven transaction-manager="tx" /> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/dbname" /> <property name="username" value="postgres" /> <property name="password" value="postgres" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="packagesToScan" value="a.b.catalog.bo"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <bean id="tx" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="tx"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="createOperation" expression="execution(* a.b.catalog.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation" /> </aop:config> </beans> |
I think many of Spring/Hibernate configuration tutorials out there do not really explain what they are doing in the context and I freaking hated it when I was researching this out. Hopefully, you the reader would have a less painful experience that I have had. All the things that I explain are based on my understanding of how the function work rather than copy pasting it from spring documentation to make it easier to understand. Let’s dig in!
|
1 2 3 4 |
<context:component-scan base-package="a.b.catalog" /> <context:annotation-config /> <mvc:annotation-driven /> <tx:annotation-driven transaction-manager="tx" /> |
Line 1 – You are telling spring to scan the package a.b.catalog and register the beans in the context. You must at least know a little bit about spring if you are reading this. You know that previously, spring needs XML entries for spring to know which beans it needs to manage. We can do that now through annotation. By the way, this is not new to Spring 3 just in case you are wondering.
Line 2- activating the annotation for already registered beans.
Here is an excellent stackoverflow link about our first two lines.
Line 3 – Tells spring to allow dispatching requests to controllers.
Line 4 – Basically telling spring to manage the transaction and use the transaction we have. In this case, tx. But what exactly is tx? We will cover that below.
tx is the spring bean id of our transaction. If you do not know what a transaction is, better google it now before proceeding.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<bean id="myDataSource" destroy-method="close"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/dbname" /> <property name="username" value="postgres" /> <property name="password" value="postgres" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource" /> <property name="packagesToScan" value="a.b.catalog.bo"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <bean id="tx" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> |
myDataSource – Creates a connection to your database. It is pretty ugly right now because we have it hard coded in you context xml. I will have another post for this using property placeholders in spring.
sessionFactory – creates a hibernate session factory base on the datasource we defined earlier. Something to take note of is the packageToScan line. Like I mentioned earlier, the goal is to use annotation. This is pretty convenient since we do not have to tell what all classes or hbm.xml files we need to load.
tx – Spring hibernate transaction manager. Do not really need to explain this.
This is a good time to take a break. We have a few more paragraphs to read through so take 15 and let’s continue after that.
Ready? Here we go.
So recalling what we have already done, we already have told spring that we are using annotation for transactions. We already told spring to create the transaction. What is missing? When should spring really start monitoring for transaction to start and end? That is where AOP comes in.
|
1 2 3 4 5 6 7 8 9 10 |
<tx:advice id="txAdvice" transaction-manager="tx"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="createOperation" expression="execution(* a.b.catalog.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation" /> </aop:config> |
1st, we create an transaction advice telling spring to monitor all methods for transaction. The aop:config tells aspectJ (won’t go into details about this for now) to monitor the package a.b.catalog.service and use our advise to process the calls to those method.
Done! I will do another follow up post on how our java code should look. Don’t want to bore everyone with one long post!