淺析Spring IOC 依賴查找你需要知道的幾種方式
首先,我們創(chuàng)建一個(gè)包含 spring-context 依賴的 maven 項(xiàng)目,然后定義一個(gè) User.class
public class User { private long id; private String name; public long getId() {return id; } public void setId(long id) {this.id = id; } public String getName() {return name; } public void setName(String name) {this.name = name; } @Override public String toString() {return 'User{' +'id=' + id +', name=’' + name + ’’’ +’}’; } }
在 resources 目錄下,創(chuàng)建 dependency-look-up.xml 配置文件
<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd'> <bean class='org.example.overview.dependency.domain.User'><property name='id' value='1'/><property name='name' value='彭于晏'/> </bean> <bean parent='user' primary='true'><property name='address' value='杭州'/> </bean> <bean class='org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean'><property name='targetBeanName' value='user'/> </bean></beans>1. 根據(jù) Bean 名稱查找實(shí)時(shí)查找
實(shí)時(shí)查找的意思就是說直接獲取 beanFactory, 通過 beanFactory 獲取 user 對應(yīng)的 bean,代碼如下所示
BeanFactory beanFactory = new ClassPathXmlApplicationContext('classpath:/META-INF/dependency-look-up.xml');// 這里的 “user” 就是 xml 文件中的 id,即名稱實(shí)時(shí)查找User user = (User) beanFactory.getBean('user');System.out.println('實(shí)時(shí)加載: ' + bean);延時(shí)查找
這里的延遲就是說,通過其他的對象來獲取 user 對應(yīng)的 Bean,代碼如下所示:
ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean('objectFactory');User user = objectFactory.getObject();System.out.println('延遲加載' + user);2. 根據(jù) Bean 類型查找
這里的類型指的就是 user.class
單個(gè) Bean 對象User bean = beanFactory.getBean(User.class);集合 Bean 對象
if(beanFactory instanceof ListableBeanFactory){ ListableBeanFactory listableBeanFactory = (ListableBeanFactory)beanFactory; Map<String, User> beansOfType = listableBeanFactory.getBeansOfType(User.class); System.out.println('集合類型:' + beansOfType); }3. 根據(jù) Bean 類型 + 名稱查找4. 根據(jù) Java 注解查找
首先我們自定義一個(gè)注解 @Super
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface Super {}
將注解表示到 superUser 類中,這里的 superUser 繼承了 user 類
@Superpublic class SuperUser extends User { private String address; public String getAddress() {return address; } public void setAddress(String address) {this.address = address; } @Override public String toString() {return 'SuperUser{' +'address=’' + address + ’’’ +'} ' + super.toString(); }}
最后根據(jù) spring 提供的 api,獲取注解 bean
if(beanFactory instanceof ListableBeanFactory){ ListableBeanFactory listableBeanFactory = (ListableBeanFactory)beanFactory; Map<String, Object> beansOfType = listableBeanFactory.getBeansWithAnnotation(Super.class); System.out.println('查找 @super 的:' + beansOfType); }
到此這篇關(guān)于淺析Spring IOC 依賴查找你需要知道的幾種方式的文章就介紹到這了,更多相關(guān)Spring IOC 依賴查找內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP使用MySQL數(shù)據(jù)庫的方法2. ASP中解決“對象關(guān)閉時(shí),不允許操作?!钡脑幃悊栴}……3. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁的問題4. xml中的空格之完全解說5. WMLScript的語法基礎(chǔ)6. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法7. XML入門的常見問題(四)8. ASP中if語句、select 、while循環(huán)的使用方法9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享
