Skip to content

Commit c5dada3

Browse files
committed
Added Sample project to show how to set up interceptors for repositories.
1 parent 65ecdba commit c5dada3

File tree

9 files changed

+253
-0
lines changed

9 files changed

+253
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<modules>
1616
<module>spring-data-jpa-example</module>
1717
<module>spring-data-jpa-showcase</module>
18+
<module>spring-data-jpa-interceptors</module>
1819
</modules>
1920

2021
<developers>

spring-data-jpa-interceptors/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.data.jpa.examples</groupId>
8+
<artifactId>spring-data-jpa-examples-parent</artifactId>
9+
<version>1.1.0.RELEASE</version>
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
13+
<artifactId>spring-data-jpa-interceptors</artifactId>
14+
<name>Spring Data JPA - Interceptor sample</name>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.spring.data.jpa.sample.interceptors;
17+
18+
import org.springframework.aop.Advisor;
19+
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
20+
import org.springframework.aop.interceptor.CustomizableTraceInterceptor;
21+
import org.springframework.aop.support.DefaultPointcutAdvisor;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
25+
import org.springframework.context.annotation.Import;
26+
27+
@Configuration
28+
@Import(InfrastructureConfiguration.class)
29+
@EnableAspectJAutoProxy
30+
public class ApplicationConfiguration {
31+
32+
@Bean
33+
public CustomizableTraceInterceptor interceptor() {
34+
35+
CustomizableTraceInterceptor interceptor = new CustomizableTraceInterceptor();
36+
interceptor.setEnterMessage("Entering $[methodName]($[arguments]).");
37+
interceptor.setExitMessage("Leaving $[methodName](..) with return value $[returnValue], took $[invocationTime]ms.");
38+
39+
return interceptor;
40+
}
41+
42+
@Bean
43+
public Advisor traceAdvisor() {
44+
45+
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
46+
pointcut.setExpression("execution(public * org.springframework.data.repository.Repository+.*(..))");
47+
48+
return new DefaultPointcutAdvisor(pointcut, interceptor());
49+
}
50+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.spring.data.jpa.sample.interceptors;
17+
18+
import javax.persistence.Entity;
19+
import javax.persistence.GeneratedValue;
20+
import javax.persistence.Id;
21+
22+
@Entity
23+
public class Customer {
24+
25+
@Id
26+
@GeneratedValue
27+
Long id;
28+
29+
String firstname;
30+
String lastname;
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.spring.data.jpa.sample.interceptors;
17+
18+
import org.springframework.data.repository.CrudRepository;
19+
20+
public interface CustomerRepository extends CrudRepository<Customer, Long> {
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.spring.data.jpa.sample.interceptors;
17+
18+
import javax.sql.DataSource;
19+
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
22+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
23+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
24+
import org.springframework.orm.jpa.JpaTransactionManager;
25+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
26+
import org.springframework.orm.jpa.vendor.Database;
27+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
28+
import org.springframework.transaction.PlatformTransactionManager;
29+
30+
@EnableJpaRepositories
31+
public class InfrastructureConfiguration {
32+
33+
@Bean
34+
public DataSource dataSource() {
35+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
36+
return builder.setType(EmbeddedDatabaseType.HSQL).build();
37+
}
38+
39+
@Bean
40+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
41+
42+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
43+
vendorAdapter.setDatabase(Database.HSQL);
44+
vendorAdapter.setGenerateDdl(true);
45+
46+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
47+
factory.setJpaVendorAdapter(vendorAdapter);
48+
factory.setPackagesToScan(getClass().getPackage().getName());
49+
factory.setDataSource(dataSource());
50+
51+
return factory;
52+
}
53+
54+
@Bean
55+
public PlatformTransactionManager transactionManager() {
56+
57+
JpaTransactionManager txManager = new JpaTransactionManager();
58+
txManager.setEntityManagerFactory(entityManagerFactory().getObject());
59+
return txManager;
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.spring.data.jpa.sample.interceptors;
17+
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.test.context.ContextConfiguration;
22+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23+
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@ContextConfiguration(classes = ApplicationConfiguration.class)
26+
public class InterceptorIntegrationTest {
27+
28+
@Autowired
29+
CustomerRepository repository;
30+
31+
@Test
32+
public void foo() {
33+
34+
Customer customer = new Customer();
35+
customer.firstname = "Dave";
36+
customer.lastname = "Matthews";
37+
38+
repository.save(customer);
39+
}
40+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Direct log messages to stdout
2+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3+
log4j.appender.stdout.Target=System.out
4+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5+
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %50.50c:%4L - %m%n
6+
7+
# Root logger option
8+
log4j.rootLogger=WARN, stdout
9+
10+
# Detailed log levels
11+
log4j.logger.org.springframework.data=DEBUG
12+
log4j.logger.org.springframework.aop.interceptor=TRACE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Bundle-ManifestVersion: 2
2+
Bundle-SymbolicName: org.springframework.data.jpa.interceptors
3+
Bundle-Name: ${project.name}
4+
Bundle-Vendor: SpringSource
5+
Bundle-Version: ${project.version}
6+
Bundle-RequiredExecutionEnvironment: J2SE-1.6
7+
Import-Package:
8+
javax.sql;version="0.0.0",
9+
org.hsqldb;version="[1.8.0,1.9.0)"
10+
Export-Template:
11+
org.springframework.data.jpa.sample.*;version="${project.version}"
12+
Import-Template:
13+
javax.persistence.*;version="${jpa.version:[=.=.=,+1.0.0)}",
14+
org.aopalliance.aop;version="[1.0,2.0)",
15+
org.springframework.*;version="${spring.version:[=.=.=.=,+1.0.0)}",
16+
org.springframework.data.*;version="${project.version:[=.=.=.=,+1.0.0)}"
17+
Import-Library:
18+
org.springframework.spring;version="${spring.version:[=.=.=.=,+1.0.0)}",
19+
org.hibernate.ejb;version="[4.1.4,5.0.0)"

0 commit comments

Comments
 (0)