最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理是不识别Servlet和filter的,所以无法使用注解方式引用,在网上查了资料后看到如下的代码:
第一种方式:在Servlet的init方法中来完成bean的实例化,初始化后可以在servlet中调用bean中的方法

WebApplicationContext cont = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
ts=(TestService)cont.getBean("testService")//ts为已经声明的类变量,括号内的名字默认为bean的类名,第一个字母小写,也可以设置唯一名称,如@Service(value="testService")

第二种方式:直接在Servlet的doPost方法中获取,代码如下

WebApplicationContext cont = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());

不过在我的项目中使用上面任何一种都无法得到任何bean,原因是上面的两种方式只对xml配置的bean有效,无法获取到注解的bean,最后在网上看到一篇关于“Spring对注解(Annotation)处理源码分析——扫描和读取Bean定义”的文章,才终于解决了这个问题,代码如下:
代码1:Service接口

package com.test.web.service;

public interface ITestService {
public void test();//测试方法
}

代码2:实现接口并使用注解方式

package com.test.web.service.impl;

import org.springframework.stereotype.Service;

import com.taokejh.web.test.ITestService;
//此处的注解部分可以给出唯一名称,如@Service(value="testServiceImpl"),等同于xml配置中bean的id
@Service
public class TestServiceImpl implements ITestService { @Override
public void test() {
System.out.println("测试打印");
} }

代码3:在Servlet中获取注解的bean并调用其测试方法

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("com.test.web.service.*");
ctx.refresh();
TestServiceImpl service = ctx.getBean(TestServiceImpl.class);//此处也可以使用ctx.getBean("testServiceImpl")
service.test();

这样就可以在Servlet或者filter中调用Spring注解方式的bean,其实整个过程就是模拟了SpringMVC在初始化的时候扫描组件包后完成对所有bean的注册并存放至管理容器中。如果大家有更好的解决办法,希望不吝赐教!

在Servlet中获取Spring注解的bean的更多相关文章

  1. Servlet中获取Spring管理的bean

    描述: 在Servlet中调用Spring管理的接口,可以使Dao/Service/ServiceImpl. 前提是在调用的bean中有注解: @Repository("beanName&q ...

  2. 如何在servlet中获取spring创建的bean

    package com.yxf.controller; import java.io.IOException; import javax.servlet.ServletException; impor ...

  3. 如何在线程中获取spring 管理的bean

    转载自:https://my.oschina.net/skyline520/blog/181158?fromerr=GjtR6Wec spring xml中定义 <!--spring 工具类-- ...

  4. tomcat启动后,在普通java类中获取spring管理的bean和ServletContext,(经过验证,可以取到)

    //从spring容易中获取bean public static Object getBean(String beanName){ ApplicationContext context = Conte ...

  5. 在Servlet中使用spring注入的bean

    package abu.csdn.servlet;    import java.io.IOException;    import javax.servlet.ServletContext;    ...

  6. 从servlet中获取spring的WebApplicationContext

    需要做一个参数初始化类,当web应用被加载时从数据库里取出相关的参数设置 ,并把这些参数放置到application里,jsp页面可以从中取出. 1.在web.xml中配置: <servlet& ...

  7. 在普通类中获取Spring管理的bean

    1.在项目中添加下面的类: import org.springframework.context.ApplicationContext; import org.springframework.cont ...

  8. 在servlet中使用spring注解

    @Autowired IAgreementPayService agreementPayService; /** * 支付参数 */ @Value("B{agreementPay.publi ...

  9. 在Servlet中获取spring容器WebApplicationContext

    WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ge ...

随机推荐

  1. Redis--各个数据类型最大存储量

    原文地址:https://redis.io/topics/data-types Strings类型:一个String类型的value最大可以存储512M Lists类型:list的元素个数最多为2^3 ...

  2. 微信小程序小程序使用scroll-view不能使用下拉刷新的解决办法

    <scroll-view class="movie-grid-container" scroll-y="true" scroll-x="fals ...

  3. Java集合整体框架

    Java中的集合类有List.Set.Map Collection的实现类:List.Set List的实现类:ArrayList.LinkedList.Vector Set的实现类:HashSet. ...

  4. Map.Entry遍历Map

    Map.entry是Java中的一个接口.每一个Map.entry代表一个map对象. 可以通过 Map是java中的接口,Map.Entry是Map的一个内部接口,它表示map中的一个实体(及一个k ...

  5. php函数引用参数解惑《细说php》

  6. 【bzoj2957】楼房重建 分块+二分查找

    题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子.为了简化问题,我们考虑这些事件发生在一个二 ...

  7. mac --snip 滚动截屏

    1.snip 下载配置:https://jingyan.baidu.com/article/fec4bce2458d03f2618d8b8e.html 2.mac的火狐浏览器好像不支持,必须在sofa ...

  8. P1196 [NOI2002]银河英雄传说

    题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...

  9. How to Create a Perl Based Custom Monitor on NetScaler

    How to Create a Perl Based Custom Monitor on NetScaler https://support.citrix.com/article/CTX227727 ...

  10. BZOJ 1040: [ZJOI2008]骑士 | 在基环外向树上DP

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1040 题解: 我AC了 是自己写的 超开心 的 考虑断一条边 这样如果根节点不选答案一定正确 ...