15 years ago, on a sultry morning in June, I climbed into an old-fashioned glass-fibre canoe. It was so old that some small pieces almost fell into my fingers, and the length of the paddle was about twice times that of the traditional rapids. I have to go boating more when I swim, but it doesn't matter. 15 years later, I'm still fascinated by it.
About 2 years ago, I tried out the spring project highlighted on the hibernate site. I think it's like the old canoe mentioned above: it suits me just the same. For enterprise hard coding, Spring is so deeply rooted in my programming work that I use it as the theme of my 4th Java book, Spring:a Developer's notebook. In this article, I'll explain why.
1. Spring offers a better advantage
In the river, I learned to use my waist and back to paddle more, because my arm muscles could not insist on rowing on the River all day. I became more efficient and I got better utilization. With spring, I can make every line of code do more things. With spring, you can discover a number of additional tools, the biggest of which is persistence. The following is a method for Hibernate data Access objects:
Public List getreservations () {return gethibernatetemplate (). Find ("from reservation");
Notice what you don't see. There is no transaction processing here. Spring allows you to build configuration code to handle transactions. You do not have to manage resources by shutting down sessions. You do not have to make your own configuration. You do not have to manage exceptions at this level, because exceptions are unchecked. Be free to manage them in the most appropriate place. Here's another hibernate method that doesn't use spring:
Public List Getbikesoldway () throws Exception { List bikes = null; session s = null; try { s = mysessionfactory.opensession (); Bikes = S.find ("from Bike"); } catch (Exception ex) { //handle Exception gracefully }finally { s.close (); } return bikes;
Spring provides you with a better advantage. With spring, you can write code more quickly and do less maintenance.
2. Spring supports Pojo programming
After the EJB 2.x failed, we were looking for ways to express enterprise services without using clumsy models to invade each bean. Of course, we need transactions, security, persistence, and sometimes remote control. For EJBS, I have to learn a widely used API and work through new tools and deployment processes. I am a slave to the service provided by the container. With spring, I can choose my own service and persistence framework. I do Pojo programming and use configuration files to add Enterprise Services to them.
In the book "Spring:a Developer's Notebook", I built a rentabike application. I call my Pojo a hibrentabike, not a session bean or entity bean; it's used as my data access object. I also added a service elsewhere. The spring configuration file, called the context, is an XML file that contains all the beans in the container and the properties and services required by the bean. Let's take a look at the following.
Goal:
<bean id= "Rentabiketarget" class= "Com.springbook.HibRentABike" > <property name= " Storename "> <value>bruce ' s bikes</value> </property> <property name=" Sessionfactory "> & Lt;ref local= "Sessionfactory"/> </property> <property name= "TransactionManager" > <ref local= "Trans" ActionManager "/> </property></bean> Interceptor:
<bean name= "Transactioninterceptor" class= "Org.springframework.transaction.interceptor".
Transactioninterceptor "> <property name=" TransactionManager "> <ref local=" TransactionManager "/> </property> <property name=" Transactionattributesource "> <value> com.springbook.rentabike.transferreservation= propagation_required,-reservationtransferexception Com.springbook.rentabike.save*=propagation_required com.springbook.rentabike.*=propagation_required, ReadOnly </value> </property></bean>
Agent:
<bean id= "Rentabike" class= "ORG.SPRINGFRAMEWORK.AOP".
Framework. Proxyfactorybean "> <property name=" proxyinterfaces "> <value>com.springbook.RentABike< /value> </property> <property name= "Interceptornames" > <value> Transactioninterceptor,rentabiketarget</value> </property></bean>
Note that there are 3 different types of beans: agents, targets, and interceptors. The agent will invoke Pojo and any services required by Pojo. The interceptor contains a combination of code for invoking the service (glue code), and also specifies how to handle each method in the target. Anyone who needs access to rentabike invokes the proxy, and the broker invokes the transaction interceptor, and then the transaction interceptor starts a transaction and invokes the target (POJO). The target completes its work, returns it to the Interceptor (which is responsible for committing the transaction), and returns it to the agent and the agent's caller.
Figure 1. Pojo Programming in progress
You build and configure the program outside of Pojo, and the rest of the work is given to spring. I am a pojo programmer.
[1] [2] Next page