Skip to content

Commit 1f9021f

Browse files
Add files via upload
1 parent 0c6b379 commit 1f9021f

File tree

7 files changed

+292
-0
lines changed

7 files changed

+292
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>spring-boot:run</goal>
10+
</goals>
11+
<properties>
12+
<spring-boot.run.jvmArguments>-noverify -XX:TieredStopAtLevel=1 </spring-boot.run.jvmArguments>
13+
<spring-boot.run.mainClass>com.jpa.JpaApplication</spring-boot.run.mainClass>
14+
<Env.SPRING_OUTPUT_ANSI_ENABLED>always</Env.SPRING_OUTPUT_ANSI_ENABLED>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>spring-boot:run</goal>
24+
</goals>
25+
<properties>
26+
<spring-boot.run.jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -noverify -XX:TieredStopAtLevel=1 </spring-boot.run.jvmArguments>
27+
<spring-boot.run.mainClass>com.jpa.JpaApplication</spring-boot.run.mainClass>
28+
<Env.SPRING_OUTPUT_ANSI_ENABLED>always</Env.SPRING_OUTPUT_ANSI_ENABLED>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath com.jpa.JpaApplication</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.jpa</groupId>
7+
<artifactId>jpa</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>jpa</name>
12+
<description>JPA project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
26+
<datasource-proxy.version>1.4.9</datasource-proxy.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-jdbc</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>mysql</groupId>
44+
<artifactId>mysql-connector-java</artifactId>
45+
<scope>runtime</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>net.ttddyy</groupId>
49+
<artifactId>datasource-proxy</artifactId>
50+
<version>${datasource-proxy.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
69+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.jpa;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.ApplicationRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class DataSourceProxyApplication {
11+
12+
@Autowired
13+
private UserRepository userRepository;
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(DataSourceProxyApplication.class, args);
17+
}
18+
19+
@Bean
20+
public ApplicationRunner init() {
21+
return args -> {
22+
23+
User user = new User();
24+
25+
user.setName("Jacky Francisco");
26+
user.setCity("Banesti");
27+
user.setAge(24);
28+
29+
userRepository.save(user);
30+
31+
};
32+
}
33+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.jpa;
2+
3+
import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel;
4+
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
5+
import org.aopalliance.intercept.MethodInterceptor;
6+
import org.aopalliance.intercept.MethodInvocation;
7+
import org.springframework.aop.framework.ProxyFactory;
8+
import org.springframework.beans.factory.config.BeanPostProcessor;
9+
import org.springframework.stereotype.Component;
10+
import org.springframework.util.ReflectionUtils;
11+
12+
import javax.sql.DataSource;
13+
import java.lang.reflect.Method;
14+
import java.util.logging.Logger;
15+
16+
@Component
17+
public class DatasourceProxyBeanPostProcessor implements BeanPostProcessor {
18+
19+
private static final Logger logger
20+
= Logger.getLogger(DatasourceProxyBeanPostProcessor.class.getName());
21+
22+
@Override
23+
public Object postProcessAfterInitialization(Object bean, String beanName) {
24+
25+
if (bean instanceof DataSource) {
26+
27+
logger.info(() -> "DataSource bean has been found: " + bean);
28+
29+
final ProxyFactory proxyFactory = new ProxyFactory(bean);
30+
31+
proxyFactory.setProxyTargetClass(true);
32+
proxyFactory.addAdvice(new ProxyDataSourceInterceptor((DataSource) bean));
33+
34+
return proxyFactory.getProxy();
35+
}
36+
return bean;
37+
}
38+
39+
@Override
40+
public Object postProcessBeforeInitialization(Object bean, String beanName) {
41+
return bean;
42+
}
43+
44+
private static class ProxyDataSourceInterceptor implements MethodInterceptor {
45+
46+
private final DataSource dataSource;
47+
48+
public ProxyDataSourceInterceptor(final DataSource dataSource) {
49+
super();
50+
this.dataSource = ProxyDataSourceBuilder.create(dataSource)
51+
.name("DATA_SOURCE_PROXY")
52+
.logQueryBySlf4j(SLF4JLogLevel.INFO)
53+
.multiline()
54+
.build();
55+
}
56+
57+
@Override
58+
public Object invoke(final MethodInvocation invocation) throws Throwable {
59+
60+
final Method proxyMethod = ReflectionUtils.
61+
findMethod(this.dataSource.getClass(),
62+
invocation.getMethod().getName());
63+
64+
if (proxyMethod != null) {
65+
return proxyMethod.invoke(this.dataSource, invocation.getArguments());
66+
}
67+
68+
return invocation.proceed();
69+
}
70+
}
71+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.jpa;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class User implements Serializable {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.IDENTITY)
16+
private Long id;
17+
18+
private String name;
19+
private String city;
20+
private int age;
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getCity() {
39+
return city;
40+
}
41+
42+
public void setCity(String city) {
43+
this.city = city;
44+
}
45+
46+
public int getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(int age) {
51+
this.age = age;
52+
}
53+
54+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.jpa;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface UserRepository extends JpaRepository<User, Long> {
8+
9+
public User findByName(String name);
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.datasource.url=jdbc:mysql://localhost:3306/db_users
2+
spring.datasource.username=root
3+
spring.datasource.password=root
4+
5+
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.show-sql=true
7+
8+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
9+
spring.jpa.properties.format-sql=true

0 commit comments

Comments
 (0)