浅析Spring IoC源码(五)再谈BeanPostProcessor(2)
  fztgkkRjHIsV 2023年11月09日 27 0


上一节,我们已经初步知道了beanPostProcessor这个接口是干什么的了,可以做什么样的事情,并且我们举了一个简单的“Teacher”的例子,但是大家也有点疑问,这个实际意义是什么呢?在项目中实际运用是什么呢?

   下面再给出一个例子,在实际项目中,我们一个项目一套代码,有生产环境和开发环境,数据库链接配置文件有2种,jdbc_dev.properties和jdbc_prd.properties这2个配置文件,我们只需要简单的修改一下,spring的配置文件就可以很快的切换数据源,这样支持多环境数据切换,我们就可以用BeanPostProcessor这个接口去实现

代码如下:

DataSourceConnection.Java

1. package org.study.spring.beanpostprocessor;  
2.   
3.   
4. public class DataSourceConnection{  
5.       
6. private String driver;  
7.       
8. private String jdbcName;  
9.       
10. private String password;  
11.       
12. private String url;  
13.       
14. /**
15.      * 这里得出数据库链接的各个属性
16.      */  
17. public void initConnection(){  
18. "I am database connection ,and I get connection by dirver :"+driver+" and jdbcName :"+jdbcName+" and password:"+password);  
19.     }  
20.       
21. public DataSourceConnection() {  
22.           
23.     }  
24.        
25.   
26. public DataSourceConnection(String driver, String jdbcName,  
27.             String password, String url) {  
28. this.driver = driver;  
29. this.jdbcName = jdbcName;  
30. this.password = password;  
31. this.url = url;  
32.     }  
33.   
34. public String getUrl() {  
35. return url;  
36.     }  
37.   
38. public void setUrl(String url) {  
39. this.url = url;  
40.     }  
41.   
42. public String getDriver() {  
43. return driver;  
44.     }  
45.   
46. public void setDriver(String driver) {  
47. this.driver = driver;  
48.     }  
49.   
50. public String getJdbcName() {  
51. return jdbcName;  
52.     }  
53.   
54. public void setJdbcName(String jdbcName) {  
55. this.jdbcName = jdbcName;  
56.     }  
57.   
58. public String getPassword() {  
59. return password;  
60.     }  
61.   
62. public void setPassword(String password) {  
63. this.password = password;  
64.     }  
65.       
66.   
67. }


ChangeConfigSupportMultiEnvironment.java


1. package org.study.spring.beanpostprocessor;  
2.   
3. import java.io.IOException;  
4. import java.io.InputStream;  
5. import java.util.Properties;  
6.   
7. import org.springframework.beans.BeansException;  
8. import org.springframework.beans.factory.config.BeanPostProcessor;  
9.   
10. public class ChangeConfigSupportMultiEnvironment implements BeanPostProcessor{  
11.       
12. /**
13.      * 目前所属的环境是dev还是prd
14.      */  
15. private String devlocation;  
16.       
17. private String configName;  
18.       
19.   
20. public String getDevlocation() {  
21. return devlocation;  
22.     }  
23.   
24. public void setDevlocation(String devlocation) {  
25. this.devlocation = devlocation;  
26.     }  
27.   
28. public String getConfigName() {  
29. return configName;  
30.     }  
31.   
32. public void setConfigName(String configName) {  
33. this.configName = configName;  
34.     }  
35.       
36. public Object postProcessBeforeInitialization(Object bean, String beanName)  
37. throws BeansException {  
38. /**
39.          * 如果该类是DataSourceConnection
40.          */  
41. if(bean instanceof DataSourceConnection){  
42. if(configName.contains("${")&&configName.contains("}")){  
43. 0,configName.indexOf("${")).concat(devlocation);  
44. new Properties();     
45. class.getResourceAsStream("/"+configName+".properties");   
46. try {  
47.                     prop.load(in);  
48. catch (IOException e) {  
49.                     e.printStackTrace();  
50.                 }  
51.                 DataSourceConnection dsc = (DataSourceConnection)bean;  
52.                 String driver = dsc.getDriver();  
53.                 String jdbcName = dsc.getJdbcName();  
54.                 String password = dsc.getPassword();  
55.                 String url = dsc.getUrl();  
56. if(driver.contains("${")&&driver.contains("}")){  
57. "${", "").replace("}", "");  
58.                 }  
59. if(jdbcName.contains("${")&&jdbcName.contains("}")){  
60. "${", "").replace("}", "");  
61.                 }  
62. if(password.contains("${")&&password.contains("}")){  
63. "${", "").replace("}", "");  
64.                 }  
65. if(url.contains("${")&&url.contains("}")){  
66. "${", "").replace("}", "");  
67.                 }  
68.                 driver = prop.getProperty(driver);  
69.                 jdbcName = prop.getProperty(jdbcName);  
70.                 password = prop.getProperty(password);  
71.                 url = prop.getProperty(url);  
72. return new DataSourceConnection(driver,jdbcName,password,url);  
73.             }  
74.               
75.         }  
76. return bean;  
77.     }  
78.   
79. public Object postProcessAfterInitialization(Object bean, String beanName)  
80. throws BeansException {  
81. //      System.out.println("after..."+this.devlocation);  
82. //      System.out.println("after..."+this.configName);  
83. return bean;  
84.     }  
85.       
86.       
87.   
88. }


jdbc_dev.properties


1. jdbc.driver= com.mysql.jdbc.Driver  
2. jdbc.url= jdbc:mysql:/127.0.0.1:3306/gms?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true  
3. jdbc.username= dev_user  
4. jdbc.password= dev_password


jdbc_prd.properties


1. jdbc.driver   = com.mysql.jdbc.Driver  
2. jdbc.url      = jdbc:mysql:/prd.com:3306/gms?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true  
3. jdbc.username = prd_user  
4. jdbc.password = prd_user


bean-post-processor.xml

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"  
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
4. xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"  
5. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"  
6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
8.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd  
9.         http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd  
10. >  
11.    
12.       
13.             
14. <bean id="changeConfigSupportMultiEnvironment" class="org.study.spring.beanpostprocessor.ChangeConfigSupportMultiEnvironment">  
15. <property name="devlocation" value="prd"/>  
16. <property name="configName" value="jdbc_${environment}"/>  
17. </bean>  
18.            
19.            
20. <bean id="dataSourceConnection" class="org.study.spring.beanpostprocessor.DataSourceConnection">  
21. <property name="driver" value="${jdbc.driver}"/>  
22. <property name="jdbcName" value="${jdbc.username}"/>  
23. <property name="password" value="${jdbc.password}"/>  
24. <property name="url" value="${jdbc.url}"/>  
25. </bean>  
26.   
27. </beans>


ChangeConfigSupportMultiEnvironmentTest.java


1. package org.study.spring.beanpostprocessor;  
2.   
3. import org.junit.Test;  
4. import org.springframework.context.ApplicationContext;  
5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
6.   
7. public class ChangeConfigSupportMultiEnvironmentTest{  
8.       
9.       
10. @Test  
11. public void test2() throws Exception{  
12. new ClassPathXmlApplicationContext("bean-post-processor.xml");  
13. "dataSourceConnection",DataSourceConnection.class);  
14.          dataSourceConnection.initConnection();  
15.            
16.     }  
17.   
18. }


当bean-post-processor.xml中devlocation的值设置为prd,运行截图是:


浅析Spring IoC源码(五)再谈BeanPostProcessor(2)_spring

设置为dev,运行截图是:


浅析Spring IoC源码(五)再谈BeanPostProcessor(2)_spring_02

这样就可以简单的切换开发环境了,可以根据这个思想,我们可以在开发过程中,灵活的运用spring的BeanPostProcessor~

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月09日 0

暂无评论

推荐阅读
fztgkkRjHIsV