做者:小傅哥
博客:https://bugstack.cnhtml
沉澱、分享、成長,讓本身和他人都能有所收穫!😄
多線程、鎖、JVM調優,都背出花啦,怎麼一寫代碼仍是亂糟糟?
java
爲何這些不管從書本、課堂、面試都顯得很是重要的知識,可是在實際的編程中沒有提高你的編碼能力呢?程序員
首先這些這些知識在實際的互聯網業務開發中,幾乎是不經常使用的,幾乎有鎖和多線程的場景,爲了性能的提高也基本都是採用分佈式設計和實現了。而這些看上去頗有技術含量的知識多數都被包裝在非業務邏輯功能的組件中,而程序員在作業務開發時候幾乎是關心不到。因此會了這些也幾乎不太可能就把你的編碼能提高起來,多數提高的是你在查複雜bug時候有一臂之力。面試
就像會漢字就能寫出詩詞歌賦嗎?懂RGB就能繪出山河大川嗎?能蹦跳就能夠舞出搖曳生姿嗎?那都是不可能的,不要想着屁股圍噶布就說會武術!算法
若是真的想把代碼寫好,就要一點點從積累數據結構和算法邏輯(不僅是機械式的刷幾道題就算了。你不理解爲何,刷再多也只是徒勞),接下來要作的是對設計模式和架構設計的理解,最終是不斷的運用和總結。在這個過程你會接觸到業務、產品、運營,編碼只是最後的具體實現,並非全流程中最重要的一部分,與編碼相比更重要的是邏輯設計。spring
謝飛機,小記!
,此次放假一遍擼串一遍被Spring,嘿嘿,檢驗成果面試去!編程
面試官:飛機,今天準備咋樣,上次問你的都學會了嗎?設計模式
謝飛機:@Resource 是 JDK javax.annotation.Resource
提供的註解,哈哈哈哈哈,另外也學習了Bean的注入。緩存
面試官:挺好記住了一些,那你在作 Bean 注入學習的時候,有注意到 Spring IOC 的特性嗎,你都用到了什麼?數據結構
謝飛機:嗯,用到 Bean 的配置、BeanDefinitionRegistryPostProcessor 對 Bean 的定義、還有 FactoryBean
面試官:好,那今天再和你聊聊,alias、autowire、depends-on、factory-method、lookup-method等,實踐驗證下看看它們是怎麼應用的。
IOC(Inversion of Control),控制反轉的核心思想在於,資源的使用不禁使用各自管理,而是交給不使用資源的第三方進行管理。這樣的好處是資源是集中管理的,可配置、易維護,同時也下降了雙方的依賴度作到了低耦合。
早在1988年,Ralph E. Johnson & Brian Foote在論文《Designing Reusable Classes》
One important characteristic of a framework is that the methods defined by the user to tailor the framework will often be called from within the framework itself, rather than from the user's application code.
The framework often plays the role of the main program in coordinating and sequencing application activity.
This inversion of control gives frameworks the power to serve as extensible skeletons. The methods supplied by the user tailor the generic algorithms defined in the framework for a particular application.
接下來就給你們介紹一下 IOC 的一些核心特性,由於這些內容不只是面試考點,也是在開發中間件或者小組件時須要用到的功能類,歸納以下:
測試類
public class UserService { private UserDao userDao; public UserService() { System.out.println("我被初始化了,UserService"); } // ...get/set }
xml配置
<bean id="userService" class="org.itstack.interview.UserService"/> <!-- 起個別名 --> <alias name="userService" alias="userService-alias01"/> <!-- 別名的別名 --> <alias name="userService-alias01" alias="userService-alias02"/>
單元測試
@Test public void test_alias() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-alias.xml"); logger.info("獲取 Bean:{}", beanFactory.getBean("userService")); logger.info("獲取 Bean 經過別名:{}", beanFactory.getBean("userService-alias01")); logger.info("獲取 Bean 經過別名的別名:{}", beanFactory.getBean("userService-alias02")); }
測試結果
23:01:29.872 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean:org.itstack.interview.UserService@2a40cd94 23:01:29.872 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userService' 23:01:29.872 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean 經過別名:org.itstack.interview.UserService@2a40cd94 23:01:29.872 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userService' 23:01:29.872 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean 經過別名的別名:org.itstack.interview.UserService@2a40cd94
測試類
public class UserDao { public UserDao() { System.out.println("我被初始化了,UserDao"); } }
xml配置
<bean id="userDao" class="org.itstack.interview.UserDao"/> <!-- 手動配置依賴 --> <bean id="userService-by-property" class="org.itstack.interview.UserService"> <property name="userDao" ref="userDao"/> </bean> <!-- 自動配置依賴 --> <bean id="userService-by-autowire" class="org.itstack.interview.UserService" autowire="byName"/>
單元測試
@Test public void test_autowire() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-autowire.xml"); logger.info("獲取 Bean by 手動配置依賴:{}", beanFactory.getBean("userService-by-property")); logger.info("獲取 Bean by 自動配置依賴:{}", beanFactory.getBean("userService-by-autowire")); }
測試結果
23:05:55.501 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean by 手動配置依賴:org.itstack.interview.UserService@679b62af 23:05:55.501 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userService-by-autowire' 23:05:55.501 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean by 自動配置依賴:org.itstack.interview.UserService@5cdd8682
測試類
public class StaticFactoryBean { static public UserDao getUserDaoByStatic(){ return new UserDao(); } }
xml配置
<bean id="staticFactory-method" class="org.itstack.interview.StaticFactoryBean" factory-method="getUserDaoByStatic"/>
單元測試
@Test public void test_factory_method() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-factory-method.xml"); logger.info("獲取 Bean:{}", beanFactory.getBean("staticFactory-method")); }
測試結果
23:15:28.950 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean:org.itstack.interview.UserDao@588df31b 23:15:28.950 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'staticFactory-bean'
factory-method="getUserDaoByStatic"
,這樣就能夠在初始化時候調用對應靜態方法的實例化內容。測試類
public class StaticFactoryBean { public UserDao getUserDao(){ return new UserDao(); } }
xml配置
<bean id="staticFactory" class="org.itstack.interview.StaticFactoryBean"/> <bean id="staticFactory-bean" factory-bean="staticFactory" factory-method="getUserDao"/>
單元測試
@Test public void test_factory_bean_method() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-factory-method.xml"); logger.info("獲取 Bean:{}", beanFactory.getBean("staticFactory-bean")); }
測試結果
23:15:28.950 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean:org.itstack.interview.UserDao@33b37288
factory-method="getUserDao"
調用的是對應的費靜態方法返回實例化結果。xml配置
<bean id="userService" class="org.itstack.interview.UserService" depends-on="userDao"/> <bean id="userDao" class="org.itstack.interview.UserDao"/>
單元測試
@Test public void test_depends_on() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-depends-on.xml"); logger.info("獲取 Bean:{}", beanFactory.getBean(UserService.class, "userService").getUserDao()); }
測試結果
我被初始化了,UserDao 我被初始化了,UserService 23:24:14.678 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean:org.itstack.interview.UserDao@45afc369
depends-on="userDao"
,那麼按照 Spring 的配置最早初始化的是 UserService
,當你有須要處理初始化依賴時則須要使用到這個配置。測試類
public class UserDaoProvider implements ApplicationContextAware { private ApplicationContext applicationContext; public UserDao getUserDao() { return applicationContext.getBean("userDao", UserDao.class); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
xml配置
<bean id="userDao" class="org.itstack.interview.UserDao" scope="prototype"/> <bean id="provider" class="org.itstack.interview.UserDaoProvider"/>
單元測試
@Test public void test_lookup_method() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-lookup-method.xml"); logger.info("獲取 Bean:{}", beanFactory.getBean(UserDaoProvider.class, "provider").getUserDao()); logger.info("獲取 Bean:{}", beanFactory.getBean(UserDaoProvider.class, "provider").getUserDao()); }
測試結果
我被初始化了,UserDao 16:29:25.813 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'userDao' 16:29:25.813 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean:org.itstack.interview.UserDao@1188e820 16:29:25.813 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'userDao' 我被初始化了,UserDao 16:29:25.814 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'userDao' 16:29:25.814 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean:org.itstack.interview.UserDao@2f490758
scope="prototype"
配置,Spring 內部實現爲使用 Cglib 方法,從新生成子類,重寫配置的方法和返回對象,達到動態改變的效果。測試類
public class MyFactoryBean implements FactoryBean<UserDao> { @Override public UserDao getObject() throws Exception { return new UserDao(); } @Override public Class<?> getObjectType() { return UserDao.class; } @Override public boolean isSingleton() { return true; } }
xml配置
<bean id="userDao" class="org.itstack.interview.MyFactoryBean"/>
單元測試
@Test public void test_factory_bean() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-factory-bean.xml"); logger.info("獲取 Bean:{}", beanFactory.getBean("userDao")); }
測試結果
23:36:19.339 [main] INFO org.itstack.interview.test.ApiTest - 獲取 Bean:org.itstack.interview.UserDao@3bd94634
測試類
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("初始化前:" + beanName); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("初始化後:" + beanName); return bean; } }
xml配置
<bean id="beanPostProcessor" class="org.itstack.interview.MyBeanPostProcessor"/> <bean id="userDao" class="org.itstack.interview.UserDao"/>
單元測試
@Test public void test_bean_post_processor() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-config-bean-post-processor.xml"); }
測試結果
初始化前:userDao 初始化後:userDao 16:38:32.686 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'userDao'
測試類
public class MyBeanFactoryAware implements BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { } }