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
;
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.Import
;
/**
* Created by gyf .
* 2020/10/16 .
*/
@Configuration
@Import
(
TestAopFactoryBeanImportBeanDefinitionRegistrar
.
class
)
//
@Import(TestAopFactoryBeanImportBeanDefinitionRegistrar.class)
public
class
TestAopBeanConfig
{
//
@RefreshScope
//
@Bean
//
public TestAopBean testAopBean() {
// return new TestAopBean(
);
//
}
@RefreshScope
@Bean
public
TestAopBean
testAopBean
()
{
return
new
TestAopBean
(
"主类"
);
}
//
@Bean
//
public static TestAopBeanPostProcessor testAopBeanPostProcessor(){
//
return new TestAopBeanPostProcessor();
//
}
@Bean
public
static
TestAopBeanPostProcessor
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
;
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.MethodInvocation
;
import
org.springframework.aop.framework.ProxyFactory
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.beans.factory.config.
DestructionAware
BeanPostProcessor
;
/**
* Created by gyf .
* 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
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
throws
BeansException
{
if
(
bean
.
getClass
()
==
TestAopBean
.
class
)
{
TestAopBean
normalBean
=
(
TestAopBean
)
bean
;
TestAopBean
perftestBean
=
new
TestAopBean
(
"代理类"
);
map
.
put
(
normalBean
,
perftestBean
);
ProxyFactory
factory
=
new
ProxyFactory
();
factory
.
setTarget
(
bean
);
factory
.
addAdvice
(
new
TestAopBeanMethodInterceptor
());
Object
proxy
=
factory
.
getProxy
();
return
proxy
;
factory
.
addAdvice
(
new
TestAopBeanMethodInterceptor
(
normalBean
,
perftestBean
));
return
factory
.
getProxy
();
}
return
bean
;
}
...
...
@@ -32,11 +52,27 @@ public class TestAopBeanPostProcessor implements BeanPostProcessor {
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
public
Object
invoke
(
MethodInvocation
invocation
)
throws
Throwable
{
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
();
}
}
}
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
@Override
public
TestAopBean
getObject
()
throws
Exception
{
System
.
out
.
println
(
this
+
" getObject"
);
return
testAopBean
;
}
...
...
@@ -54,4 +55,5 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
public
void
afterPropertiesSet
()
throws
Exception
{
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
beanDefinitionHolder
=
ScopedProxyUtils
.
createScopedProxy
(
beanDefinitionHolder
,
registry
,
true
);
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 {
@SneakyThrows
@GetMapping
(
"/test"
)
public
String
test
()
{
testAopBean
.
destroy
();
testAopBean
.
afterPropertiesSet
();
//
testAopBean.destroy();
//
testAopBean.afterPropertiesSet();
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;
*/
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
public
void
destroy
()
throws
Exception
{
// System.out.println("我被销毁了" + ExceptionUtils.getStackTrace(new Exception()));
System
.
out
.
println
(
"我被销毁了
"
);
System
.
out
.
println
(
"我被销毁了
"
+
name
);
}
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
System
.
out
.
println
(
"我初始化完成了
"
);
System
.
out
.
println
(
"我初始化完成了
"
+
name
);
}
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