您好,登錄后才能下訂單哦!
本篇內容主要講解“Spring IOC特性有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Spring IOC特性有哪些”吧!
多線程、鎖、JVM調優,都背出花啦,怎么一寫代碼還是亂糟糟?
為什么這些無論從書本、課堂、面試都顯得非常重要的知識,但是在實際的編程中沒有提升你的編碼能力呢?
首先這些這些知識在實際的互聯網業務開發中,幾乎是不常用的,幾乎有鎖和多線程的場景,為了性能的提升也基本都是采用分布式設計和實現了。而這些看上去很有技術含量的知識多數都被包裝在非業務邏輯功能的組件中,而程序員在做業務開發時候幾乎是關心不到。所以會了這些也幾乎不太可能就把你的編碼能提升起來,多數提升的是你在查復雜bug時候有一臂之力。
就像會漢字就能寫出詩詞歌賦嗎?懂RGB就能繪出山河大川嗎?能蹦跳就可以舞出搖曳生姿嗎?那都是不可能的,不要想著屁股圍噶布就說會武術!
如果真的想把代碼寫好,就要一點點從積累數據結構和算法邏輯(不只是機械式的刷幾道題就算了。你不理解為什么,刷再多也只是徒勞),接下來要做的是對設計模式和架構設計的理解,最終是不斷的運用和總結。在這個過程你會接觸到業務、產品、運營,編碼只是最后的具體實現,并不是全流程中最重要的一部分,與編碼相比更重要的是邏輯設計。
謝飛機,小記!
,這次放假一遍擼串一遍被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 {
}
}
到此,相信大家對“Spring IOC特性有哪些”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。