spring mvc里, jsp中怎么调用service,service是使用注解的方式注入的,用WebApplicationContext弄不出来
service里的代码
@Service("systemService")
public class SystemService extends BaseService
{
//数据库操作
}
JSP代码
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.bada.service.system.SystemService,java.util.*,
com.bada.model.system.SystemParam,org.springframework.web.context.WebApplicationContext
,org.springframework.web.context.support.WebApplicationContextUtils
,org.springframework.context.ApplicationContext,com.bada.service.SpringUtils" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="path" value="${pageContext.request.contextPath }" />
<%
ServletContext context = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
WebApplicationContextUtils.getWebApplicationContext(application).getBean("systemService");
//SystemService sysservice=(SystemService)ctx.getBean("systemService");
SystemService sysservice=(SystemService)SpringUtils.getApplicationContext().getBean("systemService");
%>
web.xml里配置了监听器
listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
SprintUtils , 在你的jsp页面上面import 这个类。然后通过
SpringUtils.getApplicationContext().getBean("dataSource")
获取指定的bean
package com.jsits.core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;
@Component
public class SpringUtils implements BeanFactoryPostProcessor,
BeanPostProcessor, ApplicationContextAware {
protected static Logger logger = LoggerFactory.getLogger(SpringUtils.class);
private static ConfigurableApplicationContext applicationContext = null;
private static ConfigurableListableBeanFactory beanFactory = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringUtils.applicationContext = (ConfigurableApplicationContext) applicationContext;
}
public static ConfigurableApplicationContext getApplicationContext() {
return applicationContext;
}
public static ConfigurableListableBeanFactory getConfigurableListableBeanFactory() {
return beanFactory;
}
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
SpringUtils.beanFactory = beanFactory;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
return bean;
}
public static void registerBean(String resource) {
BeanDefinitionRegistry beanDefinitionRegistry = ((BeanDefinitionRegistry)getConfigurableListableBeanFactory());
XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(
beanDefinitionRegistry);
definitionReader.setResourceLoader(getApplicationContext());
definitionReader.setEntityResolver(new ResourceEntityResolver(
getApplicationContext()));
definitionReader.setValidating(false);
definitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
definitionReader.loadBeanDefinitions(new FileSystemResource(resource));
}
public static void unregisterBean(String beanName) {
if (containsBean(beanName)) {
BeanDefinitionRegistry beanDefinitionRegistry = ((BeanDefinitionRegistry)getConfigurableListableBeanFactory());
beanDefinitionRegistry.removeBeanDefinition(beanName);
}
}
public static boolean containsBean(String beanName) {
return getConfigurableListableBeanFactory().containsBeanDefinition(beanName);
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。