Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
spring-test-web
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
郭燕飞
spring-test-web
Commits
8651c310
Commit
8651c310
authored
Nov 16, 2020
by
郭燕飞
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
version
parent
b5609bb6
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
80 additions
and
22 deletions
+80
-22
TestAopBeanConfig.java
...n/com/duiba/spring/test/web/config/TestAopBeanConfig.java
+13
-11
TestAopBeanPostProcessor.java
...uiba/spring/test/web/config/TestAopBeanPostProcessor.java
+42
-6
TestAopFactoryBean.java
.../com/duiba/spring/test/web/config/TestAopFactoryBean.java
+2
-0
TestAopFactoryBeanImportBeanDefinitionRegistrar.java
...nfig/TestAopFactoryBeanImportBeanDefinitionRegistrar.java
+1
-0
TestAopBeanController.java
...iba/spring/test/web/controller/TestAopBeanController.java
+2
-2
TestAopBean.java
...ava/cn/com/duiba/spring/test/web/service/TestAopBean.java
+20
-3
No files found.
src/main/java/cn/com/duiba/spring/test/web/config/TestAopBeanConfig.java
View file @
8651c310
package
cn
.
com
.
duiba
.
spring
.
test
.
web
.
config
;
package
cn
.
com
.
duiba
.
spring
.
test
.
web
.
config
;
import
cn.com.duiba.spring.test.web.service.TestAopBean
;
import
org.springframework.cloud.context.config.annotation.RefreshScope
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
/**
/**
* Created by gyf .
* Created by gyf .
* 2020/10/16 .
* 2020/10/16 .
*/
*/
@Configuration
@Configuration
@Import
(
TestAopFactoryBeanImportBeanDefinitionRegistrar
.
class
)
//
@Import(TestAopFactoryBeanImportBeanDefinitionRegistrar.class)
public
class
TestAopBeanConfig
{
public
class
TestAopBeanConfig
{
//
@RefreshScope
@RefreshScope
//
@Bean
@Bean
//
public TestAopBean testAopBean() {
public
TestAopBean
testAopBean
()
{
// return new TestAopBean(
);
return
new
TestAopBean
(
"主类"
);
//
}
}
//
@Bean
@Bean
//
public static TestAopBeanPostProcessor testAopBeanPostProcessor(){
public
static
TestAopBeanPostProcessor
testAopBeanPostProcessor
(){
//
return new TestAopBeanPostProcessor();
return
new
TestAopBeanPostProcessor
();
//
}
}
}
}
src/main/java/cn/com/duiba/spring/test/web/config/TestAopBeanPostProcessor.java
View file @
8651c310
package
cn
.
com
.
duiba
.
spring
.
test
.
web
.
config
;
package
cn
.
com
.
duiba
.
spring
.
test
.
web
.
config
;
import
cn.com.duiba.spring.test.web.service.TestAopBean
;
import
cn.com.duiba.spring.test.web.service.TestAopBean
;
import
java.util.HashMap
;
import
java.util.Map
;
import
lombok.SneakyThrows
;
import
org.aopalliance.intercept.MethodInterceptor
;
import
org.aopalliance.intercept.MethodInterceptor
;
import
org.aopalliance.intercept.MethodInvocation
;
import
org.aopalliance.intercept.MethodInvocation
;
import
org.springframework.aop.framework.ProxyFactory
;
import
org.springframework.aop.framework.ProxyFactory
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.beans.factory.config.
DestructionAware
BeanPostProcessor
;
/**
/**
* Created by gyf .
* Created by gyf .
* 2020/10/16 .
* 2020/10/16 .
*/
*/
public
class
TestAopBeanPostProcessor
implements
BeanPostProcessor
{
public
class
TestAopBeanPostProcessor
implements
DestructionAwareBeanPostProcessor
{
private
Map
<
TestAopBean
,
TestAopBean
>
map
=
new
HashMap
<>();
@SneakyThrows
@Override
public
void
postProcessBeforeDestruction
(
Object
bean
,
String
beanName
)
throws
BeansException
{
if
(
bean
.
getClass
()
==
TestAopBean
.
class
)
{
TestAopBean
perftestBean
=
map
.
get
(
bean
);
map
.
remove
(
bean
);
perftestBean
.
destroy
();
System
.
out
.
println
(
"我进来过,销毁"
);
}
}
@Override
@Override
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
throws
BeansException
{
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
throws
BeansException
{
if
(
bean
.
getClass
()
==
TestAopBean
.
class
)
{
if
(
bean
.
getClass
()
==
TestAopBean
.
class
)
{
TestAopBean
normalBean
=
(
TestAopBean
)
bean
;
TestAopBean
perftestBean
=
new
TestAopBean
(
"代理类"
);
map
.
put
(
normalBean
,
perftestBean
);
ProxyFactory
factory
=
new
ProxyFactory
();
ProxyFactory
factory
=
new
ProxyFactory
();
factory
.
setTarget
(
bean
);
factory
.
setTarget
(
bean
);
factory
.
addAdvice
(
new
TestAopBeanMethodInterceptor
());
factory
.
addAdvice
(
new
TestAopBeanMethodInterceptor
(
normalBean
,
perftestBean
));
Object
proxy
=
factory
.
getProxy
();
return
factory
.
getProxy
();
return
proxy
;
}
}
return
bean
;
return
bean
;
}
}
...
@@ -32,11 +52,27 @@ public class TestAopBeanPostProcessor implements BeanPostProcessor {
...
@@ -32,11 +52,27 @@ public class TestAopBeanPostProcessor implements BeanPostProcessor {
private
static
class
TestAopBeanMethodInterceptor
implements
MethodInterceptor
{
private
static
class
TestAopBeanMethodInterceptor
implements
MethodInterceptor
{
private
TestAopBean
normalBean
;
private
TestAopBean
perftestBean
;
public
TestAopBeanMethodInterceptor
(
TestAopBean
normalBean
,
TestAopBean
perftestBean
)
{
this
.
normalBean
=
normalBean
;
this
.
perftestBean
=
perftestBean
;
}
@Override
@Override
public
Object
invoke
(
MethodInvocation
invocation
)
throws
Throwable
{
public
Object
invoke
(
MethodInvocation
invocation
)
throws
Throwable
{
String
methodName
=
invocation
.
getMethod
().
getName
();
String
methodName
=
invocation
.
getMethod
().
getName
();
System
.
out
.
println
(
"我进来过,"
+
methodName
);
System
.
out
.
println
(
"代理:我进来过,"
+
methodName
);
if
(
methodName
.
equals
(
"afterPropertiesSet"
))
{
perftestBean
.
afterPropertiesSet
();
return
invocation
.
proceed
();
}
else
if
(
methodName
.
equals
(
"destroy"
))
{
perftestBean
.
destroy
();
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
}
return
invocation
.
proceed
();
}
}
}
}
}
src/main/java/cn/com/duiba/spring/test/web/config/TestAopFactoryBean.java
View file @
8651c310
...
@@ -37,6 +37,7 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
...
@@ -37,6 +37,7 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
@Override
@Override
public
TestAopBean
getObject
()
throws
Exception
{
public
TestAopBean
getObject
()
throws
Exception
{
System
.
out
.
println
(
this
+
" getObject"
);
return
testAopBean
;
return
testAopBean
;
}
}
...
@@ -54,4 +55,5 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
...
@@ -54,4 +55,5 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
public
void
afterPropertiesSet
()
throws
Exception
{
public
void
afterPropertiesSet
()
throws
Exception
{
testAopBean
.
afterPropertiesSet
();
testAopBean
.
afterPropertiesSet
();
}
}
}
}
src/main/java/cn/com/duiba/spring/test/web/config/TestAopFactoryBeanImportBeanDefinitionRegistrar.java
View file @
8651c310
...
@@ -31,4 +31,5 @@ public class TestAopFactoryBeanImportBeanDefinitionRegistrar implements ImportBe
...
@@ -31,4 +31,5 @@ public class TestAopFactoryBeanImportBeanDefinitionRegistrar implements ImportBe
beanDefinitionHolder
=
ScopedProxyUtils
.
createScopedProxy
(
beanDefinitionHolder
,
registry
,
true
);
beanDefinitionHolder
=
ScopedProxyUtils
.
createScopedProxy
(
beanDefinitionHolder
,
registry
,
true
);
BeanDefinitionReaderUtils
.
registerBeanDefinition
(
beanDefinitionHolder
,
registry
);
BeanDefinitionReaderUtils
.
registerBeanDefinition
(
beanDefinitionHolder
,
registry
);
}
}
}
}
src/main/java/cn/com/duiba/spring/test/web/controller/TestAopBeanController.java
View file @
8651c310
...
@@ -21,8 +21,8 @@ public class TestAopBeanController {
...
@@ -21,8 +21,8 @@ public class TestAopBeanController {
@SneakyThrows
@SneakyThrows
@GetMapping
(
"/test"
)
@GetMapping
(
"/test"
)
public
String
test
()
{
public
String
test
()
{
testAopBean
.
destroy
();
//
testAopBean.destroy();
testAopBean
.
afterPropertiesSet
();
//
testAopBean.afterPropertiesSet();
return
testAopBean
.
test
();
return
testAopBean
.
test
();
}
}
}
}
src/main/java/cn/com/duiba/spring/test/web/service/TestAopBean.java
View file @
8651c310
...
@@ -9,18 +9,35 @@ import org.springframework.beans.factory.InitializingBean;
...
@@ -9,18 +9,35 @@ import org.springframework.beans.factory.InitializingBean;
*/
*/
public
class
TestAopBean
implements
InitializingBean
,
DisposableBean
{
public
class
TestAopBean
implements
InitializingBean
,
DisposableBean
{
private
String
name
;
public
TestAopBean
()
{
}
public
TestAopBean
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
@Override
@Override
public
void
destroy
()
throws
Exception
{
public
void
destroy
()
throws
Exception
{
// System.out.println("我被销毁了" + ExceptionUtils.getStackTrace(new Exception()));
// System.out.println("我被销毁了" + ExceptionUtils.getStackTrace(new Exception()));
System
.
out
.
println
(
"我被销毁了
"
);
System
.
out
.
println
(
"我被销毁了
"
+
name
);
}
}
@Override
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
public
void
afterPropertiesSet
()
throws
Exception
{
System
.
out
.
println
(
"我初始化完成了
"
);
System
.
out
.
println
(
"我初始化完成了
"
+
name
);
}
}
public
String
test
()
{
public
String
test
()
{
return
"ok
"
;
return
"ok
"
+
name
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment