Skip to content

Commit 70ba3c1

Browse files
author
eugenp
committed
- full migration from XML to Java based configuration for Spring
1 parent d5e0772 commit 70ba3c1

18 files changed

Lines changed: 137 additions & 170 deletions

‎.project‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>REST</name>
3+
<name>rest</name>
44
<comment></comment>
55
<projects>
66
</projects>

‎.settings/org.eclipse.jdt.core.prefs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Wed Oct 19 00:25:56 EEST 2011
1+
#Sun Oct 23 00:05:59 EEST 2011
22
eclipse.preferences.version=1
33
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
44
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
@@ -35,7 +35,7 @@ org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=error
3535
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=error
3636
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=error
3737
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
38-
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
38+
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore
3939
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=error
4040
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=error
4141
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=error
@@ -49,13 +49,13 @@ org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
4949
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=error
5050
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=error
5151
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=error
52-
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=warning
53-
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=error
52+
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
53+
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=warning
5454
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=enabled
5555
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=error
5656
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
5757
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
58-
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=error
58+
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=warning
5959
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error
6060
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
6161
org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project-modules id="moduleCoreId" project-version="1.5.0">
3-
<wb-module deploy-name="REST">
3+
<wb-module deploy-name="rest">
44
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
55
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
66
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
7-
<property name="context-root" value="REST"/>
8-
<property name="java-output-path" value="/REST/target/classes"/>
7+
<property name="java-output-path" value="/rest/target/classes"/>
8+
<property name="context-root" value="rest"/>
99
</wb-module>
1010
</project-modules>

‎pom.xml‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,8 @@
4848
<version>${jackson-mapper-asl.version}</version>
4949
<scope>runtime</scope>
5050
</dependency>
51-
<dependency>
52-
<groupId>xstream</groupId>
53-
<artifactId>xstream</artifactId>
54-
<version>${xstream.version}</version>
55-
<scope>runtime</scope>
56-
</dependency>
51+
<!-- <dependency> <groupId>xstream</groupId> <artifactId>xstream</artifactId> <version>${xstream.version}</version> <scope>runtime</scope>
52+
</dependency> -->
5753

5854
<!-- persistence -->
5955
<dependency>
@@ -78,7 +74,6 @@
7874
<groupId>commons-dbcp</groupId>
7975
<artifactId>commons-dbcp</artifactId>
8076
<version>${commons-dbcp.version}</version>
81-
<scope>runtime</scope>
8277
</dependency>
8378

8479
<dependency>
@@ -328,17 +323,18 @@
328323
<enabled>true</enabled>
329324
</snapshots>
330325
</repository>
331-
326+
332327
</repositories>
333328

334329
<properties>
335330
<target>dev</target>
336331

337332
<!-- VERSIONS -->
333+
<!-- 3.1.0.M2 -->
338334
<spring.version>3.1.0.BUILD-SNAPSHOT</spring.version>
339335
<javassist.version>3.15.0-GA</javassist.version>
340336
<cglib.version>2.2.2</cglib.version>
341-
337+
342338
<commons-lang3.version>3.0.1</commons-lang3.version>
343339
<guava.version>10.0-rc2</guava.version>
344340

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public final List< Foo > getAll(){
3838

3939
@RequestMapping( value = "foo/{id}",method = RequestMethod.GET )
4040
@ResponseBody
41-
public final Foo getById( @PathVariable( "id" ) final Long id ){
41+
public final Foo get( @PathVariable( "id" ) final Long id ){
4242
return RestPreconditions.checkNotNull( this.service.getById( id ) );
4343
}
4444

4545
@RequestMapping( value = "foo",method = RequestMethod.POST )
46-
@ResponseStatus( HttpStatus.CREATED )
4746
@ResponseBody
47+
@ResponseStatus( HttpStatus.CREATED )
4848
public final Long create( @RequestBody final Foo entity ){
4949
RestPreconditions.checkNotNullFromRequest( entity );
5050
return this.service.create( entity );
@@ -60,7 +60,7 @@ public final void update( @RequestBody final Foo entity ){
6060

6161
@RequestMapping( value = "foo/{id}",method = RequestMethod.DELETE )
6262
@ResponseStatus( HttpStatus.OK )
63-
public final void deleteById( @PathVariable( "id" ) final Long id ){
63+
public final void delete( @PathVariable( "id" ) final Long id ){
6464
this.service.deleteById( id );
6565
}
6666

‎src/main/java/org/rest/exceptions/BadRequestException.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66
@ResponseStatus( value = HttpStatus.BAD_REQUEST )
77
public final class BadRequestException extends RuntimeException{
88
private static final long serialVersionUID = -3357578235086772559L;
9-
10-
//
119
}

‎src/main/java/org/rest/exceptions/ResourceNotFoundException.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,4 @@
66
@ResponseStatus( value = HttpStatus.NOT_FOUND )
77
public final class ResourceNotFoundException extends RuntimeException{
88
private static final long serialVersionUID = -3357578235086772559L;
9-
10-
//
11-
129
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.util.List;
44

5+
import org.rest.common.util.RestPreconditions;
56
import org.rest.dao.foo.IFooDAO;
6-
import org.rest.exceptions.ResourceNotFoundException;
77
import org.rest.model.Foo;
88
import org.rest.service.foo.IFooService;
99
import org.springframework.beans.factory.annotation.Autowired;
@@ -62,9 +62,8 @@ public void deleteById( final Long id ){
6262
Preconditions.checkNotNull( id );
6363

6464
final Foo entity = this.getById( id );
65-
if( entity == null ){
66-
throw new ResourceNotFoundException();
67-
}
65+
RestPreconditions.checkNotNull( entity );
66+
6867
this.delete( entity );
6968
}
7069

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.rest.spring;
2+
3+
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.core.io.ClassPathResource;
8+
import org.springframework.core.io.Resource;
9+
10+
@Configuration
11+
@ComponentScan( basePackages = "org.rest",excludeFilters = { @ComponentScan.Filter( Configuration.class ) } )
12+
public class ApplicationConfig{
13+
14+
// API
15+
16+
@Bean
17+
public PropertyPlaceholderConfigurer properties(){
18+
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
19+
final Resource[] resources = new ClassPathResource[ ] { new ClassPathResource( "persistence.properties" ), new ClassPathResource( "restfull.properties" ) };
20+
ppc.setLocations( resources );
21+
ppc.setIgnoreUnresolvablePlaceholders( true );
22+
return ppc;
23+
}
24+
25+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.rest.spring;
2+
3+
import java.util.Properties;
4+
5+
import org.apache.commons.dbcp.BasicDataSource;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.orm.hibernate3.HibernateTransactionManager;
10+
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
11+
import org.springframework.transaction.annotation.EnableTransactionManagement;
12+
13+
@Configuration
14+
@EnableTransactionManagement
15+
public class PersistenceConfig{
16+
17+
@Value( "${driverClassName}" )
18+
private String driverClassName;
19+
20+
@Value( "${url}" )
21+
private String url;
22+
23+
@Value( "${hibernate.dialect}" )
24+
String hibernateDialect;
25+
26+
@Value( "${hibernate.show_sql}" )
27+
String hibernateShowSql;
28+
29+
@Value( "${hibernate.hbm2ddl.auto}" )
30+
String hibernateHbm2ddlAuto;
31+
32+
// beans
33+
34+
@Bean
35+
public BasicDataSource restDataSource(){
36+
final BasicDataSource basicDataSource = new BasicDataSource();
37+
basicDataSource.setDriverClassName( this.driverClassName );
38+
basicDataSource.setUrl( this.url );
39+
basicDataSource.setUsername( "restUser" );
40+
basicDataSource.setPassword( "restmy5ql" );
41+
42+
return basicDataSource;
43+
}
44+
45+
@Bean
46+
public AnnotationSessionFactoryBean alertsSessionFactory(){
47+
final AnnotationSessionFactoryBean alertsSessionFactory = new AnnotationSessionFactoryBean();
48+
alertsSessionFactory.setDataSource( this.restDataSource() );
49+
alertsSessionFactory.setPackagesToScan( new String[ ] { "org.rest" } );
50+
alertsSessionFactory.setHibernateProperties( this.hibernateProperties() );
51+
52+
return alertsSessionFactory;
53+
}
54+
55+
@Bean
56+
public HibernateTransactionManager transactionManager(){
57+
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
58+
transactionManager.setSessionFactory( this.alertsSessionFactory().getObject() );
59+
60+
return transactionManager;
61+
}
62+
63+
final Properties hibernateProperties(){
64+
return new Properties(){
65+
{
66+
this.put( "hibernate.dialect", PersistenceConfig.this.hibernateDialect );
67+
this.put( "hibernate.show_sql", PersistenceConfig.this.hibernateShowSql );
68+
this.put( "hibernate.hbm2ddl.auto", PersistenceConfig.this.hibernateHbm2ddlAuto );
69+
70+
// in progresses still
71+
this.put( "hibernate.transaction.factory_class", "org.springframework.orm.hibernate3.SpringTransactionFactory" ); // SpringTransactionFactory.class.getSimpleName()
72+
this.put( "hibernate.current_session_context_class", "thread" );
73+
}
74+
};
75+
}
76+
77+
}

0 commit comments

Comments
 (0)