第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Spring xml中配置的bean 如何在代碼中使用

Spring xml中配置的bean 如何在代碼中使用

溫溫醬 2019-03-01 10:50:55
例如我配置了這樣兩個(gè)bean <bean id="userDao" class="com.baobaotao.anno.UserDao"/> <bean id="logDao" class="com.baobaotao.anno.LogDao"/> 如何在代碼中使用。。 我實(shí)際遇到的問題 下面是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" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" > <context:annotation-config/> <cache:annotation-driven cache-manager="cacheManager"/> <!-- eh-cache --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" > <property name="configLocation" value="classpath:ehcache.xml"/> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean> </beans> public class EHCache { @Autowired EhCacheCacheManager cacheManager; public void getCacheElement(String cacheKey) throws Exception { System.out.println(cacheManager); } } 輸出的確實(shí)null。。。這是我的xml 有錯(cuò)還是用法有錯(cuò)
查看完整描述

3 回答

?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

搞了半天找到了答案
要想在代碼中通過@Autowired 注解獲取spring在xml中配置的bean需要在web.xml加入監(jiān)聽器

 <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  

這是我的測試結(jié)果

@Autowired
    private User user;
@RequestMapping("/user")
    public void user(Model model,HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println(user);
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();  
        System.out.println(wac);
        System.out.println(wac.getBean("user"));
        System.out.println(SpringUtil.getBean("user"));
    }

輸出

springmvc.User@60188eba
Root WebApplicationContext: startup date [Thu Jul 06 11:36:07 CST 2017]; root of context hierarchy
springmvc.User@60188eba
springmvc.User@60188eba

取到了同一對(duì)象

查看完整回答
反對(duì) 回復(fù) 2019-03-01
  • 3 回答
  • 0 關(guān)注
  • 765 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)