跳至主要内容

博文

Use native JPA API

Use native JPA API Since Spring 3.0, Spring embrace JPA quickly, and JPA is the first class citizen in Spring core framework. And in the latest Spring, JPA support is improved and the usage of JPA is more friendly than Hibernate. Configuration An example of XML format configuration is shown below: <jdbc:embedded-database id="dataSource" > </jdbc:embedded-database> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="persistenceUnit" /> <property name="dataSource" ref="dataSource" /> <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property> <property name="packagesToScan"> <array> <value>com.hantsylabs.example.spring.model</value> ...

Use native Hibernate 4 API

Use native Hibernate 4 API In Spring 3.1, a new package named org.springframework.orm.hibernate4 is included( in spring-orm maven dependency). With this new APIs, using Hibernate 4 in Spring projects becomes more easy than before, you are not required to extend the HiberanteDaoSupport class or use HibernateTemplate in your implemnetation class. Only a simple registration is required in Spring configuration, you can use the Hibernate native Session API freely in your Spring projects. Register a DataSource bean <jdbc:embedded-database id="dataSource" > </jdbc:embedded-database> Declare Spring specific LocalSessionFactoryBean bean. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.hantsylabs.e...

Simplified Jdbc operations

Simplified Jdbc operations As I stated in the last post, Spring DaoSupport and Template API is not recommended in new projects. But for Jdbc API, how to simplify the data operations? Spring jdbc support(spring-jdbc maven dependency) provides some simplified APIs for Jdbc operations. Overview Unlike the approach of JdbcDaoSupport, it is no need to subclass the JdbcDaoSupport class. A SQL query(and modifying query) execution will be wrapped in a single object. All you need to do is creating a new class(by subclassing the related operation object) for every SQL query execution. In these operation classes, MappingSqlQuery , SqlUpdate , SimpleJdbcInsert are used frequently. Follow the following steps to use them. Compose sql query statement, and declare parameters used in the sql statement. Call compile() method from super class to prepare the sql statement. Call execute methods to execute the query. MappingSqlQuery MappingSqlQuery is designated for e...

Legacy DaoSupport And Template API

Legacy DaoSupport And Template API In the Spring core framework(provided in the spring-jdbc and spring-orm maven dependency), it has provided a series of standard DaoSupport APIs and Template pattern to help user to retrieve data from database and save data back into database. Jdbc support There is a JdbcDaoSupport class and JdbcTempalte class provided for Jdbc operations. Using these APIs in your project is very simple and stupid. Firstly register a DataSource bean, and also declare a Jdbc transaction manager(we will discuss Spring transaction in future posts). <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:/com/hantsylabs/example/spring/config/schema.sql" /> </jdbc:embedded-database> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="da...

Spring Data Access overview

This is the first post of my Spring Data Persistence series, all are based on my personal idea about Spring, it is NOT the official guide. If something is described incorrectly, please let me know. Thanks. Since Spring 1.x, in the core framework, Spring provided a generic DaoSupport API to simplify the data operations, and now a new Spring Data project was born which provides a modern way for data operations. DaoSupport and Template API The legacy DaoSupport and Template API tries to simplify data access of DBRMS, it supports Hibernate, Jdbc, iBatis and JDO etc. In the new Spring, JDO support is deprecated and removed, and the third party project MyBatis (the successor of iBatis) hands over the Spring data access support. Spring provides lots of encapsulation for the Jdbc operations, and tries to free developers from the tedious Jdbc APIs. Till now, Jdbc support is still considered valuable when Jdbc is chosen in your project. Besides this, Spring provides a abstraction...

Deploy application into Cloudfoundry

VMware(the SpringSource's mother company) provides a PAAS cloud service(aka CloudFoundry.com), you can easily deploy your application into CloudFoundry.com via Roo CloudFoundry addon or CloudFoundry.com client tools. And VMware also provides a Eclipse IDE plugin, with which you can deploy the application into CloudFoundry in IDE, just like deploy into your local server instance. Configuration for CloudFoundry The following steps are based on the project we have created in before post. Add cloudfoundry dependency in your pom.xml . <dependency> <groupId>org.cloudfoundry</groupId> <artifactId>cloudfoundry-runtime</artifactId> <version>0.8.2</version> </dependency> Configure a cloud data source in spring configuration file applicationContext.xml . <beans profile="cloud"> <cloud:data-source id="dataSource"/> </beans> CloudFoundry also supports other service, such as MongoDb,...

Tao of Spring Roo

As you see, Spring Roo is not only a simple scollad tool for code generation, it also try to introduce a new programming model. AspectJ and AOP programming Roo is heavily dependent on AspectJ, the famous AOP framework, which is a subproject of Eclipse.org . With the benifit of AspectJ, Roo try to separate the concern of an entity class. The entity class itself focuses on the declaration of business fields, and only includes the basic definition of the properties and bean validation declaration, the JPA specific definitions are moved to _Roo_Jpa_Entity aspect, and the toString helper method is included in the _Roo_ToString aspect, the setter and getter methods of the properties are categorized in the _Roo_JavaBean aspect. As you have seen in the before section, the entity class becomes slim and clean. Roo will process the .aj files and compile them into classes at compile time. Thus all extra features in the .aj files will be accessible in the entity class at runtim...

More fun with Spring Roo

more fun with roo We have created a Spring web project using Spring Roo, now let us have a look at the generated codes. Import codes into Eclipse workspace If you created the project from Roo console, you can follow the following steps to import the project codes into your Eclipse workspace. If you created the project from Eclipse directly, skip this section. Start Eclipse IDE. Select Import... menu item from File menu in the Eclipse main menu. In the popup Import... dialog, expand the Maven node, and select Existing Maven Projects and click Next button. Choose the created project folder, it will scan the project files, and recognize the pom.xml file, select it by checking the checkbox, and click Finish button. Please keep patient, and wait for one or two minutes. After the project is imported Eclipse will resolve the dependencies automatically. Explore the project codes Switch to Spring perspective, and open Package view. In t...

Create a Spring project from Spring ToolSuite

The Spring company, SpringSource provides a full stack solution for Spring development, including excellent IDE support, SpringSource provides an Eclipse based IDE to speed up the development of Spring project. Install Spring ToolSuite You can download a copy of Spring ToolSuite from SpringSource , and install it into your local disk. Alternatively, you can get a copy of the JEE bundle from Eclipse website and install Spring ToolSuite plugin manually from Eclipse Marketplace. Download the newest Eclipse JEE Bundle from the Eclipse website . Extract the files from the zip archive into your local disk. Start up Eclipse IDE. Open Eclpse Marketplace from Help menu in the IDE menu bar. Search SpringSource to find the Spring ToolSuite plugin. Click Install button and wait the installation done. Restart IDE according to the prompt, and apply the change. Note: In the further content, I will use the words IDE or Eclipse , it refers to Spring ToolSuite or Eclipse...

Create a Spring Project with Roo

Spring Roo is a rapid scollad tool brought by SpringSource , which can help you to create a data centered project in several minutes. Assumption I assume you are familiar with Apache Maven and Eclipse IDE . I also assume you have installed the latest Oracle JDK 7 in your local system. Install Spring Roo Download Spring Roo from SpringSource , and extract the file into your local disk. Add /bin to your system PATH environment variable. For Linux user, export PATH=$PATH: /bin (Linux) For Windows user, set PATH=%PATH%; /bin (Windows) Create a project Create a new folder as the new project root folder. Open your system terminal, and enter the new created folder. Run roo command, it will launch the Roo console and display the Roo welcome message. ____ ____ ____ / __ \/ __ \/ __ \ / /_/ / / / / / / / / _, _/ /_/ / /_/ / /_/ |_|\____/\____/ 1.2.3.RELEASE [rev 7fd62b6] Welcome to Spring Roo. For assistance press TAB or type "hint" then hit EN...

Book Review: Arquillian Testing Guide

Today I have received my hard copy of the book, Arquillian Testing Guide. Comparing to the resource I had got at the end of last year, much content are improved. Arquillian is a modern Java EE testing framework, based the well-known JUnit or TestNG, brought by JBoss.org. In fact, currently Arquillian is beyond Java EE, and not only for Java EE application and Java EE container, such as it also supports Android application test and can run test in Android system(or Android simulator) . Most of my Arquillian experience is based on Java EE. If you have some experience of JUnit experience before, and eager to find a testing solution for your Java EE application and want to run the test case in container automatically, this book is for you. With Arquillian, you are free from preparing the test fixture before you write real tests. You can write tests in the nature way, such as using @Inject, @Resource etc in your test case directly as the way in yo...

Click4nb Update for NetBeans 7.3

I just updated the click4nb and rebuilt the source code against NetBeans IDE 7.3. No new features added. Follow the installation guide to install it into your IDE. Installation Read this wiki page to start your first Click application. Getting Started with Apache Click and NetBeans IDE Enjoy!

Getting Started with Apache Click and NetBeans IDE

Note: this post is from my project wiki pages, please read https://github.com/hantsy/click4nb/wiki/Getting-Started-with-Apache-Click-and-NetBeans-IDE for the newest version. If you have some suggestion or find some errors, please file an issue. Thanks. After moved from Google Code, I have cleaned the codes and just released a new version of click4nb , which was tested against the stable NetBeans IDE 7.2. Installation Following the steps in Installation wiki page to install click4nb into your NetBeans IDE. Create a web project with Apache Click Support Create a Web Application in NetBeans is very easy, follow the following steps to create one for yourself. Click the New Project button or click "File/New Project" menu from main menu in NetBeans IDE. In the popup New Project dialog, select Java Web in the Categories list, and Web Application in the "Projects", click Next button. In the Name and Location dialog, input the project related info, such...