Skip to content

Commit 9a73e84

Browse files
author
eugenp
committed
- major refactoring of the Spring context initialization in tests as
well as in web.xml
1 parent ed78829 commit 9a73e84

28 files changed

Lines changed: 345 additions & 300 deletions

‎pom.xml‎

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,18 @@
113113
<version>${mysql-connector-java.version}</version>
114114
<scope>runtime</scope>
115115
</dependency>
116-
<dependency>
116+
<!-- <dependency>
117117
<groupId>commons-dbcp</groupId>
118118
<artifactId>commons-dbcp</artifactId>
119119
<version>${commons-dbcp.version}</version>
120-
</dependency>
120+
</dependency> -->
121121

122122
<dependency>
123123
<groupId>org.hibernate</groupId>
124124
<artifactId>hibernate-core</artifactId>
125125
<version>${hibernate.version}</version>
126126
</dependency>
127-
<!-- hibernate-commons-annotations seems to be a weird dependency of hibernate-core
128-
<br> - issue: it is old and has a dependency on slf4j-api 1.5.8, which I
129-
cannot shake - issue: there is another 3.3.0.ga version out there - "Version
130-
'3.3.0.ga' was a mistake during the release process, please disregard. There
131-
is not such version 3.3.0.ga." -->
132-
<dependency>
133-
<groupId>org.hibernate</groupId>
134-
<artifactId>hibernate-commons-annotations</artifactId>
135-
<version>${hibernate-commons-annotations.version}</version>
136-
</dependency>
137-
<dependency>
138-
<groupId>org.hibernate</groupId>
139-
<artifactId>hibernate-validator</artifactId>
140-
<version>4.2.0.Final</version>
141-
</dependency>
142-
127+
143128
<!-- logging -->
144129
<dependency>
145130
<groupId>org.slf4j</groupId>
@@ -217,7 +202,7 @@
217202
<dependency>
218203
<groupId>com.jayway.restassured</groupId>
219204
<artifactId>rest-assured</artifactId>
220-
<version>1.4</version>
205+
<version>1.4.5</version>
221206
<scope>test</scope>
222207
<exclusions>
223208
<exclusion>
@@ -432,16 +417,16 @@
432417
<javassist.version>3.15.0-GA</javassist.version>
433418
<cglib.version>2.2.2</cglib.version>
434419

435-
<commons-lang3.version>3.0.1</commons-lang3.version>
420+
<commons-lang3.version>3.1</commons-lang3.version>
436421
<guava.version>10.0.1</guava.version>
437422

438-
<jackson-mapper-asl.version>1.8.5</jackson-mapper-asl.version>
423+
<jackson-mapper-asl.version>1.9.2</jackson-mapper-asl.version>
439424
<xstream.version>1.2.2</xstream.version>
440425
<jaxb-api.version>2.2.4</jaxb-api.version>
441426

442427
<!-- VERSIONS - persistence -->
443428
<mysql-connector-java.version>5.1.18</mysql-connector-java.version> <!-- latest version on: 02.10.2011 - http://dev.mysql.com/downloads/connector/j/ -->
444-
<hibernate.version>3.6.7.Final</hibernate.version>
429+
<hibernate.version>3.6.8.Final</hibernate.version>
445430
<hibernate-commons-annotations.version>3.2.0.Final</hibernate-commons-annotations.version>
446431
<commons-dbcp.version>1.4</commons-dbcp.version>
447432

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.rest.common.dao;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
6+
import org.hibernate.SessionFactory;
7+
import org.hibernate.classic.Session;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.transaction.annotation.Propagation;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
import com.google.common.base.Preconditions;
13+
14+
@SuppressWarnings( "unchecked" )
15+
@Transactional( propagation = Propagation.SUPPORTS )
16+
public abstract class AbstractDAO< T extends Serializable > implements ICommonOperations< T >{
17+
private final Class< T > clazz;
18+
19+
@Autowired
20+
SessionFactory sessionFactory;
21+
22+
public AbstractDAO( final Class< T > clazzToSet ){
23+
Preconditions.checkNotNull( clazzToSet );
24+
this.clazz = clazzToSet;
25+
}
26+
27+
// get
28+
29+
@Override
30+
@Transactional( readOnly = true )
31+
public T getById( final Long id ){
32+
Preconditions.checkArgument( id != null );
33+
34+
return (T) this.getCurrentSession().get( this.clazz, id );
35+
}
36+
37+
@Override
38+
@Transactional( readOnly = true )
39+
public List< T > getAll(){
40+
return this.getCurrentSession().createQuery( "from " + this.clazz.getName() ).list();
41+
}
42+
43+
// create/persist
44+
45+
@Override
46+
public void create( final T entity ){
47+
Preconditions.checkNotNull( entity );
48+
49+
this.getCurrentSession().persist( entity );
50+
}
51+
52+
// update
53+
54+
@Override
55+
public void update( final T entity ){
56+
Preconditions.checkNotNull( entity );
57+
58+
this.getCurrentSession().merge( entity );
59+
}
60+
61+
// delete
62+
63+
@Override
64+
public void delete( final T entity ){
65+
Preconditions.checkNotNull( entity );
66+
67+
this.getCurrentSession().delete( entity );
68+
}
69+
70+
public void deleteById( final Long entityId ){
71+
final T entity = this.getById( entityId );
72+
Preconditions.checkState( entity != null );
73+
74+
this.delete( entity );
75+
}
76+
77+
// util
78+
79+
protected Session getCurrentSession(){
80+
return this.sessionFactory.getCurrentSession();
81+
}
82+
83+
}

‎src/main/java/org/rest/common/dao/CustomHibernateDaoSupport.java‎

Lines changed: 0 additions & 71 deletions
This file was deleted.

‎src/main/java/org/rest/common/dao/ICommonOperations.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public interface ICommonOperations< T extends Serializable >{
1414

1515
List< T > getAll();
1616

17-
// create
17+
// create/persist
1818

19-
Long create( final T entity );
19+
void create( final T entity );
2020

2121
// update
2222

‎src/main/java/org/rest/controller/foo/FooController.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
import org.springframework.web.bind.annotation.ResponseBody;
2222
import org.springframework.web.bind.annotation.ResponseStatus;
2323

24-
/**
25-
* @author eugenp
26-
*/
2724
@Controller
2825
final class FooController{
2926

@@ -58,9 +55,9 @@ public final Foo get( @PathVariable( "id" ) final Long id, final HttpServletRequ
5855
@ResponseStatus( HttpStatus.CREATED )
5956
public final void create( @RequestBody final Foo resource, final HttpServletRequest request, final HttpServletResponse response ){
6057
RestPreconditions.checkRequestElementNotNull( resource );
61-
final Long idOfCreatedResource = this.service.create( resource );
58+
this.service.create( resource );
6259

63-
this.eventPublisher.publishEvent( new ResourceCreated( this, request, response, idOfCreatedResource ) );
60+
this.eventPublisher.publishEvent( new ResourceCreated( this, request, response, resource.getId() ) );
6461
}
6562

6663
@RequestMapping( value = "admin/foo",method = RequestMethod.PUT )

‎src/main/java/org/rest/dao/foo/IFooDAO.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
import org.rest.common.dao.ICommonOperations;
44
import org.rest.model.Foo;
5-
import org.springframework.orm.hibernate3.HibernateTemplate;
65

76
/**
87
* @author eugenp
98
*/
109
public interface IFooDAO extends ICommonOperations< Foo >{
11-
12-
void setHibernateTemplate( final HibernateTemplate hibernateTemplate );
13-
10+
//
1411
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package org.rest.dao.foo.impl;
22

3-
import org.rest.common.dao.CustomHibernateDaoSupport;
3+
import org.rest.common.dao.AbstractDAO;
44
import org.rest.dao.foo.IFooDAO;
55
import org.rest.model.Foo;
66
import org.springframework.stereotype.Repository;
77

8-
/**
9-
* @author eugenp
10-
*/
118
@Repository
12-
public class FooDAO extends CustomHibernateDaoSupport< Foo > implements IFooDAO{
9+
public class FooDAO extends AbstractDAO< Foo > implements IFooDAO{
1310

1411
public FooDAO(){
1512
super( Foo.class );
1613
}
1714

18-
// API
19-
2015
}

‎src/main/java/org/rest/model/Foo.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import javax.persistence.Entity;
77
import javax.persistence.GeneratedValue;
88
import javax.persistence.Id;
9-
import javax.validation.constraints.NotNull;
109
import javax.xml.bind.annotation.XmlRootElement;
1110

1211
import org.hibernate.annotations.GenericGenerator;
@@ -21,8 +20,7 @@ public final class Foo implements Serializable{
2120

2221
private Long id;
2322

24-
@NotNull
25-
@Column( unique = true )
23+
@Column( unique = true,nullable = false )
2624
private String name;
2725

2826
public Foo(){

‎src/main/java/org/rest/service/foo/impl/FooService.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
/**
1717
* @author eugenp
1818
*/
19-
// TODO: later on this should not be public
2019
@Service
2120
@Transactional( propagation = Propagation.REQUIRED )
22-
public class FooService implements IFooService{
21+
class FooService implements IFooService{
2322

2423
@Autowired
2524
IFooDAO dao;
@@ -36,8 +35,8 @@ public Foo getById( final Long id ){
3635
}
3736

3837
@Override
39-
public Long create( final Foo entity ){
40-
return this.dao.create( entity );
38+
public void create( final Foo entity ){
39+
this.dao.create( entity );
4140
}
4241

4342
@Override
@@ -63,7 +62,7 @@ public void deleteById( final Long id ){
6362

6463
final Foo entity = this.getById( id );
6564
RestPreconditions.checkNotNull( entity );
66-
65+
6766
this.delete( entity );
6867
}
6968

src/main/java/org/rest/spring/root/ApplicationConfig.java renamed to src/main/java/org/rest/spring/application/ApplicationConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.rest.spring.root;
1+
package org.rest.spring.application;
22

33
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
44
import org.springframework.context.annotation.Bean;

0 commit comments

Comments
 (0)