Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
武汉红喜
whatsmars
提交
8a1f6d94
W
whatsmars
项目概览
武汉红喜
/
whatsmars
通知
3
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8a1f6d94
编写于
6月 23, 2020
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
beans test
上级
f7a1c9f8
变更
19
隐藏空白更改
内联
并排
Showing
19 changed file
with
307 addition
and
8 deletion
+307
-8
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean/src/main/resources/application.yml
...smars-boot-sample-bean/src/main/resources/application.yml
+0
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/pom.xml
...s-spring-boot-samples/whatsmars-boot-sample-beans/pom.xml
+0
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/Application.java
...a/org/hongxi/whatsmars/boot/sample/beans/Application.java
+1
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/ConditionalBean.java
...g/hongxi/whatsmars/boot/sample/beans/ConditionalBean.java
+1
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/DemoBean.java
...java/org/hongxi/whatsmars/boot/sample/beans/DemoBean.java
+1
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/OrderedDemoBean.java
...g/hongxi/whatsmars/boot/sample/beans/OrderedDemoBean.java
+1
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/Planet.java
...n/java/org/hongxi/whatsmars/boot/sample/beans/Planet.java
+17
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/ApplicationConfiguration.java
.../sample/beans/autoconfigure/ApplicationConfiguration.java
+75
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/PlanetProperties.java
...ars/boot/sample/beans/autoconfigure/PlanetProperties.java
+22
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/PlanetRegisterConfiguration.java
...mple/beans/autoconfigure/PlanetRegisterConfiguration.java
+44
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/condition/OrCondition.java
...xi/whatsmars/boot/sample/beans/condition/OrCondition.java
+1
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/environment/EnvironmentProcessor.java
...s/boot/sample/beans/environment/EnvironmentProcessor.java
+1
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/factory/PlanetFactory.java
...xi/whatsmars/boot/sample/beans/factory/PlanetFactory.java
+15
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/factory/PlanetFactoryBean.java
...hatsmars/boot/sample/beans/factory/PlanetFactoryBean.java
+33
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/postprocessor/DemoBeanPostProcessor.java
...oot/sample/beans/postprocessor/DemoBeanPostProcessor.java
+2
-1
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/register/PlanetBeanDefinitionRegistryPostProcessor.java
...s/register/PlanetBeanDefinitionRegistryPostProcessor.java
+41
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/register/PlanetBeanDefinitionRegistryPostProcessor2.java
.../register/PlanetBeanDefinitionRegistryPostProcessor2.java
+44
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/resources/META-INF/spring.factories
...sample-beans/src/main/resources/META-INF/spring.factories
+2
-0
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/resources/application.yml
...mars-boot-sample-beans/src/main/resources/application.yml
+6
-0
未找到文件。
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean/src/main/resources/application.yml
已删除
100644 → 0
浏览文件 @
f7a1c9f8
a
:
xx
\ No newline at end of file
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean/pom.xml
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s
/pom.xml
浏览文件 @
8a1f6d94
文件已移动
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bean
/Application.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans
/Application.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
...
...
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bean
/ConditionalBean.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans
/ConditionalBean.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s
;
/**
* Created by shenhongxi on 2020/6/22.
...
...
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bean
/DemoBean.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans
/DemoBean.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s
;
import
org.springframework.beans.factory.InitializingBean
;
...
...
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bean
/OrderedDemoBean.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans
/OrderedDemoBean.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s
;
import
org.springframework.core.Ordered
;
...
...
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/Planet.java
0 → 100644
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.beans
;
/**
* Created by shenhongxi on 2020/6/23.
*/
public
class
Planet
{
private
String
name
;
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bean
/ApplicationConfiguration.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure
/ApplicationConfiguration.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s.autoconfigure
;
import
org.hongxi.whatsmars.boot.sample.beans.*
;
import
org.hongxi.whatsmars.boot.sample.beans.condition.OrCondition
;
import
org.hongxi.whatsmars.boot.sample.beans.postprocessor.DemoBeanPostProcessor
;
import
org.hongxi.whatsmars.boot.sample.beans.register.PlanetBeanDefinitionRegistryPostProcessor
;
import
org.hongxi.whatsmars.boot.sample.beans.register.PlanetBeanDefinitionRegistryPostProcessor2
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.core.env.Environment
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Created by shenhongxi on 2020/6/22.
...
...
@@ -39,4 +49,27 @@ public class ApplicationConfiguration {
public
ConditionalBean
conditionalBean
()
{
return
new
ConditionalBean
();
}
@Bean
@ConditionalOnProperty
(
name
=
"planet.names[0]"
)
public
PlanetBeanDefinitionRegistryPostProcessor
planetBeanDefinitionRegistryPostProcessor
(
Environment
environment
)
{
return
new
PlanetBeanDefinitionRegistryPostProcessor
(
parseNames
(
environment
));
}
@Bean
@ConditionalOnProperty
(
name
=
"planet.names[0]"
)
public
PlanetBeanDefinitionRegistryPostProcessor2
planetBeanDefinitionRegistryPostProcessor2
(
Environment
environment
)
{
return
new
PlanetBeanDefinitionRegistryPostProcessor2
(
parseNames
(
environment
));
}
public
List
<
String
>
parseNames
(
Environment
environment
)
{
List
<
String
>
names
=
new
ArrayList
<>();
String
configsKey
=
"planet.names[%d]"
;
int
configIndex
=
0
;
String
name
;
while
((
name
=
environment
.
getProperty
(
String
.
format
(
configsKey
,
configIndex
++)))
!=
null
)
{
names
.
add
(
name
);
}
return
names
;
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/PlanetProperties.java
0 → 100644
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.beans.autoconfigure
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
java.util.List
;
/**
* Created by shenhongxi on 2020/6/23.
*/
@ConfigurationProperties
(
prefix
=
"planet"
)
public
class
PlanetProperties
{
private
List
<
String
>
names
;
public
List
<
String
>
getNames
()
{
return
names
;
}
public
void
setNames
(
List
<
String
>
names
)
{
this
.
names
=
names
;
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/autoconfigure/PlanetRegisterConfiguration.java
0 → 100644
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.beans.autoconfigure
;
import
org.hongxi.whatsmars.boot.sample.beans.Planet
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.support.BeanDefinitionBuilder
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
/**
* Created by shenhongxi on 2020/6/23.
*/
@EnableConfigurationProperties
(
PlanetProperties
.
class
)
@Configuration
public
class
PlanetRegisterConfiguration
implements
InitializingBean
,
ApplicationContextAware
{
private
ConfigurableApplicationContext
applicationContext
;
@Autowired
private
PlanetProperties
planetProperties
;
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
planetProperties
.
getNames
().
forEach
(
name
->
{
BeanDefinitionBuilder
beanBuilder
=
BeanDefinitionBuilder
.
rootBeanDefinition
(
Planet
.
class
);
beanBuilder
.
addPropertyValue
(
"name"
,
name
);
DefaultListableBeanFactory
beanFactory
=
(
DefaultListableBeanFactory
)
applicationContext
.
getBeanFactory
();
beanFactory
.
registerBeanDefinition
(
"planet3_"
+
name
,
beanBuilder
.
getBeanDefinition
());
Planet
planet
=
beanFactory
.
getBean
(
"planet3_"
+
name
,
Planet
.
class
);
System
.
out
.
println
(
planet
.
getName
());
});
}
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
this
.
applicationContext
=
(
ConfigurableApplicationContext
)
applicationContext
;
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bea
n/OrCondition.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans/conditio
n/OrCondition.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s.condition
;
import
org.springframework.context.annotation.Condition
;
import
org.springframework.context.annotation.ConditionContext
;
...
...
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bean
/EnvironmentProcessor.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans/environment
/EnvironmentProcessor.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s.environment
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.env.EnvironmentPostProcessor
;
...
...
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/factory/PlanetFactory.java
0 → 100644
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.beans.factory
;
import
org.hongxi.whatsmars.boot.sample.beans.Planet
;
/**
* Created by shenhongxi on 2020/6/23.
*/
public
class
PlanetFactory
{
public
static
Planet
createPlanet
(
String
name
)
{
Planet
planet
=
new
Planet
();
planet
.
setName
(
name
);
return
planet
;
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/factory/PlanetFactoryBean.java
0 → 100644
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.beans.factory
;
import
org.hongxi.whatsmars.boot.sample.beans.Planet
;
import
org.springframework.beans.factory.FactoryBean
;
/**
* Created by shenhongxi on 2020/6/23.
*/
public
class
PlanetFactoryBean
implements
FactoryBean
<
Planet
>
{
private
String
name
;
public
PlanetFactoryBean
(
String
name
)
{
this
.
name
=
name
;
}
@Override
public
Planet
getObject
()
throws
Exception
{
Planet
planet
=
new
Planet
();
planet
.
setName
(
name
);
return
planet
;
}
@Override
public
Class
<?>
getObjectType
()
{
return
Planet
.
class
;
}
@Override
public
boolean
isSingleton
()
{
return
true
;
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
/src/main/java/org/hongxi/whatsmars/boot/sample/bean
/DemoBeanPostProcessor.java
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s/src/main/java/org/hongxi/whatsmars/boot/sample/beans/postprocessor
/DemoBeanPostProcessor.java
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.bean
;
package
org.hongxi.whatsmars.boot.sample.bean
s.postprocessor
;
import
org.hongxi.whatsmars.boot.sample.beans.DemoBean
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
...
...
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/register/PlanetBeanDefinitionRegistryPostProcessor.java
0 → 100644
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.beans.register
;
import
org.hongxi.whatsmars.boot.sample.beans.Planet
;
import
org.hongxi.whatsmars.boot.sample.beans.factory.PlanetFactory
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.support.BeanDefinitionBuilder
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistry
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
;
import
java.util.List
;
/**
* Created by shenhongxi on 2020/6/23.
*/
public
class
PlanetBeanDefinitionRegistryPostProcessor
implements
BeanDefinitionRegistryPostProcessor
{
private
List
<
String
>
names
;
public
PlanetBeanDefinitionRegistryPostProcessor
(
List
<
String
>
names
)
{
this
.
names
=
names
;
}
@Override
public
void
postProcessBeanDefinitionRegistry
(
BeanDefinitionRegistry
registry
)
throws
BeansException
{
names
.
forEach
(
name
->
{
BeanDefinitionBuilder
beanBuilder
=
BeanDefinitionBuilder
.
rootBeanDefinition
(
PlanetFactory
.
class
);
beanBuilder
.
setFactoryMethod
(
"createPlanet"
);
beanBuilder
.
addConstructorArgValue
(
name
);
registry
.
registerBeanDefinition
(
"planet_"
+
name
,
beanBuilder
.
getRawBeanDefinition
());
});
}
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
names
.
forEach
(
name
->
{
Planet
planet
=
beanFactory
.
getBean
(
"planet_"
+
name
,
Planet
.
class
);
System
.
out
.
println
(
planet
.
getName
());
});
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/java/org/hongxi/whatsmars/boot/sample/beans/register/PlanetBeanDefinitionRegistryPostProcessor2.java
0 → 100644
浏览文件 @
8a1f6d94
package
org.hongxi.whatsmars.boot.sample.beans.register
;
import
org.hongxi.whatsmars.boot.sample.beans.Planet
;
import
org.hongxi.whatsmars.boot.sample.beans.factory.PlanetFactoryBean
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.config.ConstructorArgumentValues
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistry
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
;
import
org.springframework.beans.factory.support.GenericBeanDefinition
;
import
java.util.List
;
/**
* Created by shenhongxi on 2020/6/23.
*/
public
class
PlanetBeanDefinitionRegistryPostProcessor2
implements
BeanDefinitionRegistryPostProcessor
{
private
List
<
String
>
names
;
public
PlanetBeanDefinitionRegistryPostProcessor2
(
List
<
String
>
names
)
{
this
.
names
=
names
;
}
@Override
public
void
postProcessBeanDefinitionRegistry
(
BeanDefinitionRegistry
registry
)
throws
BeansException
{
names
.
forEach
(
name
->
{
GenericBeanDefinition
genericBeanDefinition
=
new
GenericBeanDefinition
();
genericBeanDefinition
.
setBeanClass
(
PlanetFactoryBean
.
class
);
ConstructorArgumentValues
constructorArgumentValues
=
new
ConstructorArgumentValues
();
constructorArgumentValues
.
addIndexedArgumentValue
(
0
,
name
);
genericBeanDefinition
.
setConstructorArgumentValues
(
constructorArgumentValues
);
registry
.
registerBeanDefinition
(
"planet2_"
+
name
,
genericBeanDefinition
);
});
}
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
names
.
forEach
(
name
->
{
Planet
planet
=
beanFactory
.
getBean
(
"planet2_"
+
name
,
Planet
.
class
);
System
.
out
.
println
(
planet
.
getName
());
});
}
}
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean/src/main/resources/META-INF/spring.factories
→
whatsmars-spring-boot-samples/whatsmars-boot-sample-bean
s
/src/main/resources/META-INF/spring.factories
浏览文件 @
8a1f6d94
org.springframework.boot.env.EnvironmentPostProcessor=\
org.hongxi.whatsmars.boot.sample.bean.EnvironmentProcessor
\ No newline at end of file
org.hongxi.whatsmars.boot.sample.beans.environment.EnvironmentProcessor
\ No newline at end of file
whatsmars-spring-boot-samples/whatsmars-boot-sample-beans/src/main/resources/application.yml
0 → 100644
浏览文件 @
8a1f6d94
a
:
xx
planet
:
names
:
-
Mars
-
Earth
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录