servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
来源:互联网 发布:ubuntu升级16.04后黑屏 编辑:程序博客网 时间:2024/05/17 05:57
最近由于项目中出现了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的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,的两种方式一种注解一种xml配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- servlet调用spring中的bean(注解方式)
- Spring Boot 普通类调用Bean对象的一种方式
- Spring的XML配置的bean调用注解方式的bean
- spring中配置Bean的xml方式和注解方式
- Spring(三):IoC容器装配Bean(xml配置方式和注解方式)
- SpringBoot配置Bean的两种方式--注解以及配置文件
- web.xml文件的一种配置方式
- action中调用Spring的bean时有两种方式:
- spring创建策略模式的两种方式(xml配置和纯注解)
- spring aop注解和xml两种方式的基础
- spring 注解方式配置Bean
- spring 注解方式配置Bean
- spring aop 第一种方式--注解
- Spring学习(二十二) Bean配置的三种方式(XML、注解、Java类)介绍与对比
- android中RadioButton中的自带的圆圈如何去掉,两种方法,一种.xml、一种代码中
- 对象析构谈—— delete this 的使用及注意事项
- Spring中@Autowired注解、@Resource注解的区别
- zabbix 自定义监控
- Java mock工具-mockito
- 利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
- servlet调用spring容器中的bean,的两种方式一种注解一种xml配置
- Ubuntu 12.04下编译OpenSceneGraph-3.0.1记录
- 程序员之路——一个老程序员对刚上大学的学弟学妹的忠告
- 使用“数据库配置助手”时报“使用Database Control配置数据库时,……配置监听程序”的警告
- 堆和栈的区别
- C#索引器
- 第一篇博客 写于2013春实训后
- C专家编程 前言 第一章
- 数据结构(C语言实现)