servlet 调用ssh中的dao(hibernate)

 转 http://www.iteye.com/problems/21773


<h3><span style="font-size:18px;">先自己重写一个ContextLoaderListener ,然后配置在web.xml替代ContextLoaderListener </span></h3><span style="font-size:14px;">Java代码  
public class SpringLoaderListener extends ContextLoaderListener {   
  
    @Override  
    public void contextInitialized(ServletContextEvent event) {   
           
        super.contextInitialized(event);   
        ServletContext context = event.getServletContext();   
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);   
        SpringContextUtil.setApplicationContext(ctx);   
    }   
  
}  

public class SpringLoaderListener extends ContextLoaderListener {

	@Override
	public void contextInitialized(ServletContextEvent event) {
		
		super.contextInitialized(event);
		ServletContext context = event.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        SpringContextUtil.setApplicationContext(ctx);
	}

}


</span><h3><span style="font-size:14px;">后面就是一个操作SpringContextUtil </span></h3><span style="font-size:14px;">Java代码  
public class SpringContextUtil {   
    private static ApplicationContext context;   
    
    public static void setApplicationContext(ApplicationContext acx) {   
        context = acx;   
    }   
  
    public static ApplicationContext getApplicationContext() {   
        return context;   
    }   
  
    public static Object getBean(String name) throws BeansException {   
        return context.getBean(name);   
    }   
  
  
    public static Object getBean(String name, Class requiredType) throws BeansException {   
        return context.getBean(name, requiredType);   
    }   
  
    public static boolean containsBean(String name) {   
        return context.containsBean(name);   
    }   
  
    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {   
        return context.isSingleton(name);   
    }   
  
    public static Class getType(String name) throws NoSuchBeanDefinitionException {   
        return context.getType(name);   
    }   
  
    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {   
        return context.getAliases(name);   
    }   
}  

public class SpringContextUtil {
    private static ApplicationContext context;
 
    public static void setApplicationContext(ApplicationContext acx) {
        context = acx;
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public static Object getBean(String name) throws BeansException {
        return context.getBean(name);
    }


	public static Object getBean(String name, Class requiredType) throws BeansException {
        return context.getBean(name, requiredType);
    }

    public static boolean containsBean(String name) {
        return context.containsBean(name);
    }

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return context.isSingleton(name);
    }

    public static Class getType(String name) throws NoSuchBeanDefinitionException {
        return context.getType(name);
    }

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return context.getAliases(name);
    }
}


</span><h3><span style="font-size:14px;">你可以在servlet里通过该Utils工具方法可以得到你想要的bean,dao或者service </span></h3><span style="font-size:14px;">然后就可以操作数据库了 
</span>
<span style="font-size:14px;">
</span>
<span style="font-size:14px;"><span style="white-space:pre">	</span>UserDaoImp userDao = (UserDaoImp) SpringContextUtil.getApplicationContext().getBean("userDao");
<span style="white-space:pre">	</span>userDao.update(user);
</span>
<span style="font-size:14px;">
</span>

web.xml

<span style="font-size:14px;"><span style="white-space:pre">	</span><listener>
<span style="white-space:pre">		</span><listener-class>com.djh.listener.SpringLoaderListener</listener-class>
<span style="white-space:pre">	</span></listener>
</span>

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读