1010import org .springframework .beans .BeanMetadataAttribute ;
1111import org .springframework .beans .BeanUtils ;
1212import org .springframework .beans .BeansException ;
13+ import org .springframework .beans .factory .BeanFactory ;
14+ import org .springframework .beans .factory .BeanFactoryAware ;
15+ import org .springframework .beans .factory .InitializingBean ;
1316import org .springframework .beans .factory .annotation .Autowired ;
17+ import org .springframework .beans .factory .annotation .Value ;
1418import org .springframework .beans .factory .config .*;
1519import org .springframework .beans .factory .parsing .DefaultsDefinition ;
1620import org .springframework .beans .factory .support .*;
21+ import org .springframework .boot .context .properties .ConfigurationProperties ;
1722import org .springframework .boot .jdbc .DataSourceBuilder ;
1823import org .springframework .boot .json .YamlJsonParser ;
1924import org .springframework .cglib .beans .BeanMap ;
25+ import org .springframework .context .ApplicationContext ;
26+ import org .springframework .context .ApplicationContextAware ;
27+ import org .springframework .context .ApplicationEvent ;
28+ import org .springframework .context .ApplicationListener ;
2029import org .springframework .context .annotation .Bean ;
2130import org .springframework .context .annotation .ComponentScan ;
2231import org .springframework .context .annotation .Configuration ;
32+ import org .springframework .context .annotation .ConfigurationClassPostProcessor ;
33+ import org .springframework .core .Ordered ;
2334import org .springframework .core .env .ConfigurableEnvironment ;
2435import org .springframework .core .env .Environment ;
2536import org .springframework .core .env .MutablePropertySources ;
3445import org .yaml .snakeyaml .representer .Representer ;
3546
3647import javax .sql .DataSource ;
48+ import java .sql .SQLException ;
49+ import java .time .LocalDateTime ;
3750import java .util .*;
3851
3952/**
4053 *
4154 */
4255@ Component
43- public class DsScannerConfigurer {
56+ public class DsScannerConfigurer implements BeanFactoryAware , Ordered , ApplicationContextAware {
4457
58+ private BeanFactory beanFactory ;
4559
4660 private static final String PREFIX = "druid2." ;
4761 private static final String DRUID_DS_PROPS_PREFIX = "druid." ;
4862 public static final String TRANSACTIONMANAGER = "TransactionManager" ;
4963 private static final String MAPPERSCANNERCONFIGURER = "MapperScannerConfigurer" ;
5064 public static final String SQLSESSIONFACTORY = "SqlSessionFactory" ;
5165
52- @ Bean
53- public BeanDefinitionRegistryPostProcessor factory (final StandardServletEnvironment environment ) {
54-
55- return new BeanDefinitionRegistryPostProcessor () {
56- @ Override
57- public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry ) throws BeansException {
58- MutablePropertySources propertySources = environment .getPropertySources ();
59- Properties properties = new Properties ();
60- propertySources .forEach (x -> {
61- Object source = x .getSource ();
62- if (source instanceof LinkedHashMap ) {
63- LinkedHashMap props = (LinkedHashMap ) source ;
64- props .forEach ((k , v ) -> {
65- String key = k .toString ();
66- boolean druid2 = key .startsWith (PREFIX );
67- if (druid2 ) {
68- properties .put (key , v );
69- }
70- });
71- }
72- });
73- boolean empty = properties .isEmpty ();
74- if (!empty ) {
75- DbConf1 dbConf1 = getDsConfBean (properties );
76- try {
77- registryDs (dbConf1 , registry );
78- } catch (Exception e ) {
79- e .printStackTrace ();
80- }
81- }
82- }
8366
67+ @ Autowired
68+ private DbConf dbConf ;
8469
85- @ Override
86- public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory ) throws BeansException {
70+ @ Override
71+ public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
72+ this .beanFactory = beanFactory ;
73+ }
74+
75+
76+ @ Override
77+ public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
78+ BeanDefinitionRegistry registry = (BeanDefinitionRegistry ) beanFactory ;
79+ try {
80+ registryDs (registry );
81+ } catch (ClassNotFoundException e ) {
82+ e .printStackTrace ();
83+ }
8784
88- }
89- };
9085 }
9186
92- private void registryDs (DbConf1 properties , BeanDefinitionRegistry registry ) throws Exception {
93- String poolType = properties .getPoolType ();
87+ @ Override
88+ public int getOrder () {
89+ return -100 ;
90+ }
91+
92+
93+ private void registryDs (BeanDefinitionRegistry registry ) throws ClassNotFoundException {
94+ String poolType = dbConf .getPoolType ();
9495 if (Objects .nonNull (poolType )) {
95- Map <String , Db > dbs = properties .getDbs ();
96+
97+ Map <String , Db > dbs = dbConf .getDbs ();
9698 Set <Map .Entry <String , Db >> entries = dbs .entrySet ();
9799 for (Map .Entry <String , Db > entry : entries ) {
98100 String dbName = entry .getKey ();
@@ -101,18 +103,24 @@ private void registryDs(DbConf1 properties, BeanDefinitionRegistry registry) thr
101103 String mapperScannerPackage = value .getMapperScannerPackage ();
102104 String typeHandlersPackage = value .getTypeHandlersPackage ();
103105 Properties masterProperties = getDsProps (value .getMaster ());
104- Properties slaveProperties = getDsProps (value .getSlave ());
105-
106+ Map <String , Properties > slaves = value .getSlaves ();
107+ List <Properties > salves1 = new ArrayList <>();
108+ if (Objects .nonNull (slaves )) {
109+ slaves .entrySet ().forEach (x -> salves1 .add (getDsProps (slaves .get (x ))));
110+ }
106111 DruidDataSource master = new DruidDataSource ();
107112 master .setConnectProperties (masterProperties );
108- master .init ();
109-
110- DruidDataSource slave = new DruidDataSource ();
111- slave .setConnectProperties (slaveProperties );
112- slave .init ();
113+ List <DruidDataSource > slaveDatasources = new ArrayList <>();
114+ if (!salves1 .isEmpty ()) {
115+ salves1 .forEach (x -> {
116+ DruidDataSource ds = new DruidDataSource ();
117+ ds .setConnectProperties (x );
118+ slaveDatasources .add (ds );
119+ });
120+ }
113121
114122 //获取动态数据源
115- registryDynamicDs (master , slave , dbName , registry );
123+ registryDynamicDs (master , slaveDatasources , dbName , registry );
116124 //事物
117125 registryDynamicDsTrancationManager (dbName , registry );
118126
@@ -125,6 +133,7 @@ private void registryDs(DbConf1 properties, BeanDefinitionRegistry registry) thr
125133 }
126134 }
127135
136+
128137 private Properties getDsProps (Properties master ) {
129138 Properties props = new Properties ();
130139 Enumeration <?> enumeration = master .propertyNames ();
@@ -171,11 +180,14 @@ private void registryDynamicDsTrancationManager(String dbName, BeanDefinitionReg
171180 registry .registerBeanDefinition (dbName + TRANSACTIONMANAGER , beanDefinition );
172181 }
173182
174- private void registryDynamicDs (DruidDataSource master , DruidDataSource slave , String dbName , BeanDefinitionRegistry registry ) {
183+ private void registryDynamicDs (DruidDataSource master , List < DruidDataSource > slaves , String dbName , BeanDefinitionRegistry registry ) {
175184
176185 Map <Object , Object > targetDataResources = new HashMap <>();
177186 targetDataResources .put (DbContextHolder .DbType .MASTER , master );
178- targetDataResources .put (DbContextHolder .DbType .SLAVE , slave );
187+ if (Objects .nonNull (slaves ) && !slaves .isEmpty ()) {
188+ targetDataResources .put (DbContextHolder .DbType .SLAVE , slaves .get (0 ));
189+ }
190+
179191
180192 GenericBeanDefinition beanDefinition = new GenericBeanDefinition ();
181193 beanDefinition .setBeanClass (MasterSlaveRoutingDataSource .class );
@@ -186,7 +198,13 @@ private void registryDynamicDs(DruidDataSource master, DruidDataSource slave, St
186198 }
187199
188200
189- private DbConf1 getDsConfBean (Properties properties ) {
201+ /**
202+ * 目前无效代码了
203+ *
204+ * @param properties
205+ * @return
206+ */
207+ private DbConf getDsConfBean (Properties properties ) {
190208 PropertiesToYamlConverter converter = new PropertiesToYamlConverter ();
191209
192210 StringBuilder sb = new StringBuilder ();
@@ -207,25 +225,8 @@ private DbConf1 getDsConfBean(Properties properties) {
207225
208226 Map ymalMap = yaml .loadAs (docment , Map .class );
209227 String s = JsonUtil .toJsonStr (ymalMap );
210- DbConf1 dbConf11 = JsonUtil .json2Bean (s , DbConf1 .class );
228+ DbConf dbConf11 = JsonUtil .json2Bean (s , DbConf .class );
211229 return dbConf11 ;
212230 }
213-
214- public static void main (String [] args ) {
215- Map ymalMap = new HashMap ();
216- ymalMap .put ("poolType" , "wwwww" );
217- DbConf1 dbConf1 = new DbConf1 ();
218- BeanMap beanMap = BeanMap .create (dbConf1 );
219- beanMap .putAll (ymalMap );
220-
221- String s = JsonUtil .toJsonStr (ymalMap );
222- DbConf1 dbConf11 = JsonUtil .json2Bean (s , DbConf1 .class );
223-
224-
225- System .out .println (dbConf11 );
226-
227- }
228-
229-
230231}
231232
0 commit comments