springMvc中获取spring中的bean
我有个类,web.xml 配置
<servlet>
<servlet-name>vdc_ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-servlet.xml</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
applicationContext-servlet.xml中,配置了一个bean,这个bean
<bean id="testA" class="com.bb.testA"></bean>
在controllerer中的某个方法里,使用下面的代码获取:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
service=(LogInjectionSaf)webApplicationContextgetBean("testA");
为什么显示 service为null呢,怎么才能得到?
<servlet>
<servlet-name>vdc_ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-servlet.xml</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
applicationContext-servlet.xml中,配置了一个bean,这个bean
<bean id="testA" class="com.bb.testA"></bean>
在controllerer中的某个方法里,使用下面的代码获取:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
service=(LogInjectionSaf)webApplicationContextgetBean("testA");
为什么显示 service为null呢,怎么才能得到?
...全文
相关推荐
SpringMVC+Spring+Mybatis集成开发环境
在IT行业中,SpringMVC、Spring和Mybatis是三大核心框架,它们的集成使用是Java Web开发中的常见实践。
Spring+SpringMVC+Mybatis SSM三大框架的运行流程、原理、核心技术详解
- **第二步:调用getBean方法** - `getBean`方法用于获取配置文件中的bean实例。该方法通常需要传入bean的id,返回对应bean的实例。
Spring整合Springmvc的相关介绍
子容器可以访问父容器中的Bean,但父容器无法直接访问子容器的Bean。这种设计允许在Spring MVC中使用由Spring管理的服务,而不会导致混乱。5.
Spring+SpringMVC+MyBatis整合
**Spring管理**:Spring作为整体的容器,管理SpringMVC和MyBatis的所有组件,包括Bean的生命周期和依赖关系。2.
spring+springmvc+mybatis
业务逻辑层通常会包含Spring管理的Service Bean,这些Service Bean可以通过Spring的DI机制获取到MyBatis的SqlSession,执行数据库操作。


