例如我配置了這樣兩個(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ì)象
添加回答
舉報(bào)
0/150
提交
取消