最近由于项目中出现了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. ASP.NET程序中设置相对路径的方法

    如图所示,这是个绝对路径. 改为相对路径的方法是:AppDomain.CurrentDomain.BaseDirectory. 如下图所示:

  2. linux下搜索find命令拾遗

    强制删除项目下面的所有.svn文件目录,find . -name ‘.svn’ -exec rm -rf {} \; empty显示所有的空白文件,并显示详细:find . -empty size显示 ...

  3. PHP变量类型转换

    PHP数据类型转换 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: •(int).(integer):转换成整形 •(float).(double).(real):转换成浮点型 •(s ...

  4. JavaScript内置对象常用

    Math 提供了数学中常用的属性和方法,使用时直接用Math.属性/方法,而不需要new一个Math对象 Date 使用Date对象来对日期和时间进行操作.使用时,必须用new创建一个实例 windo ...

  5. C++——编程常见错误

    C++库函数 C++标准库比C标准库要复杂很多,需要大家认真学习.C++标准库建立时间较晚,解决了C标准库的一些问题.通过认真学习.熟练掌握会对代码质量的提高有一定帮助. 一些建议: 1. 尽量使用迭 ...

  6. codeforces 1015C

    C. Songs Compression time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. ireport写sql语句的按钮在哪

  8. wget下载HTTPS链接

    wget -c -O master.zip --no-check-certificate https://github.com/mitsuhiko/flask/archive/master.zip # ...

  9. bzoj 4004 [JLOI2015]装备购买 拟阵+线性基

    [JLOI2015]装备购买 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1820  Solved: 547[Submit][Status][Dis ...

  10. Death Note

    注:本文系作者原创,但可随意转载. ********************************************************************************** ...