各位请看我下面的一段代码:ServletContext servletContext = session.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
// FDService service = (FDService)wac.getBean(className);
// sr = service.execute(request);
Object service = wac.getBean(className);//获得className的实例
这里我在网上面找 都有一个配置了bean的xml文件 可是我这个项目找不到 但是那个getBean也是能取到值的 ,我想知道这个getBean(className)的内部是不是通过java反射取出值的?不需要配置xml文件也是能执行的?
参数className是一个String类型的参数
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
// FDService service = (FDService)wac.getBean(className);
// sr = service.execute(request);
Object service = wac.getBean(className);//获得className的实例
这里我在网上面找 都有一个配置了bean的xml文件 可是我这个项目找不到 但是那个getBean也是能取到值的 ,我想知道这个getBean(className)的内部是不是通过java反射取出值的?不需要配置xml文件也是能执行的?
参数className是一个String类型的参数
在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用WebApplicationContext.getBean("beanName")来获得对象的引用,这实际上是使用了依赖查找来获得对象
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName">
<context:component-scan base-package="com.friendone.ws.*" />
<context:component-scan base-package="com.friendone.services.*" />
<context:component-scan base-package="com.friendone.listener.*" />
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="applicationContextProvider" class="com.friendone.common.SpringContextUtil"></bean>
<!-- jBPM4 configuration -->
<!-- jdbcTemplate configuration -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="200"/>
<property name="maxIdle" value="0"/>
<property name="maxWait" value="-1"/>
</bean>
<bean id="jdbctxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<import resource="classpath:spring-cxf-*.xml" /> <tx:annotation-driven transaction-manager="jdbctxManager"/>
</beans>
这个就是context里面的内容没有 和我需要的那个相关的bean
<context:component-scan base-package="com.friendone.services.*" />
<context:component-scan base-package="com.friendone.listener.*" />就是这个东西 把所有的bean都隐式注入了