
首先,获取的 bean 的时候要保证或取得的 bean 必须携带 spring 容器注入的属性,这里举一个简单的例子:
以 dao,service,action 这三种常见的数据模型,业务模型,控制器来举例子,(当然你使用 spring mvc 都是一样的,只不过省掉了 action,但都是控制器,你懂得):
如果jsp 视图需要接受一个 service 的 bean,必须要保证该 service 的 bean 中含有 注入的 dao 属性,这个懂吧?,如果直接在 jsp 中 new(实例化对象),这样将会丢失属性的,因为内存区域完全开辟了新的堆空间,跟 spring 容器所管理的 bean 完全不是一个,那么问题就来了,怎么样才能简单的得到这个拥有注入属性的 bean 呢?
可以尝试将该 bean 封装为控制器(controller)中的一个成员(属性),之后通过web 容器内置的request ,session ,application 等作用域来进行封装,这就是简单的解决办法了
写这篇笔记的原因是在配置微服务过程中,出现了如下错,原因很简单,但是因为不够熟悉困惑了一阵子。
Field userFeignClient in cntearntestademoaDemoaController required a bean of type 'cntearndemobsdkrestfulUserFeignClientB' that could not be found
Action:
Consider defining a bean of type 'cntearndemobsdkrestfulUserFeignClientB' in your configuration
场景:
创建两个微服务,服务消费者demoa和服务提供者demob,书上或网上一些例子大部分把demob的FeignClient接口写在demoa工程里,这样服务消费者在使用的时候都写一遍FeignClient,这是很麻烦,也是很多余的事情。把FeignClient做为一个单独的module,那么消费者要用的时候直接添加依赖就可以了,具体内容就不写了,下面列出架构,如下图:
服务消费额者:
这个module里也加了调用服务提供者的FeignClient(UserFeignClient),不用的,不推荐。
服务提供者:
服务提供者之FeignClient:
demoa添加demob-sdk以后之后,
在Controller里直接注入UserFeignClientB,demoa工程在启动的时候文章开头的错,
启动异常:
Field userFeignClient in cntearntestademoaDemoaController required a bean of type 'cntearndemobsdkrestfulUserFeignClientB' that could not be found
Action:
Consider defining a bean of type 'cntearndemobsdkrestfulUserFeignClientB' in your configuration
在deomoaApplication加了 @ComponetScan 扫描cntearndemobsdkrestful,不行!改成 @EnableFeignClients ({"cntearndemobsdkrestful"})就ok了,不加这两个注解导致的错误信息是一样的,原因可是不一样。回想上面demoa贴图里也有一个FeignClient类,为什么启动没问题也能调通呢?因为我在demoaApplication加了@EnableFeignClients,它能扫到当前所在的包。
这样虽然问题解决了,但是需要每个消费者都需要写一遍还是比较麻烦的,另外一种做法就是配置写在demobsdk里。
在demobsdk moudle的META-INF下添加springfactories文件(直接放在rescources下也一样),内容:
orgspringframeworkbootautoconfigureEnableAutoConfiguration=\
cntearndemobsdksdkConfig
sdkConfigjava里加上一句@EnableFeignClients({"cntearndemobsdkrestful"})就可以了,如下图:
方法如下:
实现
1创建一个类让其实现orgspringframeworkcontextApplicationContextAware接口来让Spring在启动的时候为我们注入ApplicationContext对象
示例代码:
import orgspringframeworkbeansBeansException;
import orgspringframeworkcontextApplicationContext;
import orgspringframeworkcontextApplicationContextAware;
public class MyApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
//声明一个静态变量保存
public void setApplicationContext(ApplicationContext contex) throws BeansException {
thiscontext=contex;
}
public static ApplicationContext getContext(){
return context;
}
public final static Object getBean(String beanName){
return contextgetBean(beanName);
}
public final static Object getBean(String beanName, Class<> requiredType) {
return contextgetBean(beanName, requiredType);
}
}
2在applicationContextxml文件中配置此bean,以便让Spring启动时自动为我们注入ApplicationContext对象
例:
<!-- 这个bean主要是为了得到ApplicationContext 所以它不需要其它属性-->
<bean class="orgingspringutilMyApplicationContextUtil"></bean>
3有了这个ApplicationContext之后我们就可以调用其getBean("beanName")方法来得到由Spring 管理所有对象
不用配置xml,直接java代码实现,参考代码如下:
public class GetApplicationContext {
private static class ApplicationContextHolder {
// 单例变量
private static ApplicationContext AC = new FileSystemXmlApplicationContext(
"classpath:applicationContextxml");
}
// 私有化的构造方法,保证外部的类不能通过构造器来实例化。
private GetApplicationContext() {
}
// 获取单例对象实例
public static ApplicationContext getInstance() {
if (ApplicationContextHolderAC == null) {
ApplicationContextHolderAC = new FileSystemXmlApplicationContext(
"classpath:applicationContextxml");
}
return ApplicationContextHolderAC;
}
}
获取所有spring自动装配的bean:
<span style="<a href=">
通过xml配置文件
bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext
第一种方式:FileSystemXmlApplicationContext
通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例:
ApplicationContext ac = new FileSystemXmlApplicationContext(applicationContextxml)
acgetBean(beanName);
第二种方式:WebApplicationContextUtil
以上就是关于spring mvc里,jsp中怎么获取bean全部的内容,包括:spring mvc里,jsp中怎么获取bean、spring cloud配置遇到的bean not found问题、工具类实现ApplicationContextAware后无法取得spring容器的bean等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)