ssh中不通过Action如何注入

danengnai__ 2009-03-28 11:31:15

private ServiceImplPublicQuery services = null;

public int getTotalCount() {
int returnValue = 38;
return returnValue;
}

public ServiceImplPublicQuery getServices() {
return services;
}

public void setServices(ServiceImplPublicQuery services) {
this.services = services;
}


我通过Flex直接调用了这个方法,但是services总是为空。
...全文
128 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
如一宝宝 2009-03-28
  • 打赏
  • 举报
回复

package net.phoenix.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.phoenix.comm.AppContext;
import net.phoenix.comm.Constants;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;

/**
* 系统启动将要运行的类
*
* @author 咖啡
*
*/
public class StartupListener extends ContextLoaderListener implements ServletContextListener {

private static final Log log = LogFactory.getLog(StartupListener.class);

/**
* 设置程序根目录
*/
public void contextInitialized(ServletContextEvent event) {
String rootpath = event.getServletContext().getRealPath("/");
if (rootpath != null) {
rootpath = rootpath.replaceAll("\\\\", "/");
} else {
rootpath = "/";
}
if (!rootpath.endsWith("/")) {
rootpath = rootpath + "/";
}
Constants.ROOTPATH = rootpath;
if (log.isDebugEnabled()) {
log.debug("initializing context...");
}
super.contextInitialized(event);

ServletContext context = event.getServletContext();
String daoType = context.getInitParameter(Constants.DAO_TYPE);
if (daoType == null) {
log.warn("No 'daoType' context carameter, using hibernate");
daoType = Constants.DAO_TYPE_HIBERNATE;
}
Map config = (HashMap) context.getAttribute(Constants.CONFIG);

if (config == null) {
config = new HashMap();
}
config.put(Constants.DAO_TYPE, daoType);
context.setAttribute(Constants.CONFIG, config);
if (log.isDebugEnabled()) {
log.debug("daoType: " + daoType);
log.debug("populating drop-downs...");
}

setupContext(context);
}

// 登录加载spring的context
public static void setupContext(ServletContext context) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
AppContext.setContext(ctx);
if (log.isDebugEnabled()) {
log.debug("drop-down initialization complete [OK]");
}
}
}


忘了贴上servlet的监听器,在web.xml里面配置下
如一宝宝 2009-03-28
  • 打赏
  • 举报
回复

package com.phoenix.service;

import net.phoenix.comm.AppContext;

public interface IService {

public static AdminInfoService adminInfoService = (AdminInfoService) AppContext.getInstance().getAppContext().getBean("adminInfoTarget");
}



通过一个接口来获取spring的bean,这样就不用action注入了

其中AppContext是spring的上下文的单例类,在服务器启动时加载

这是AppContext的代码

package net.phoenix.comm;

import org.springframework.context.ApplicationContext;

/**
* 单例类,用于设置,获取Spring上下文的配置信息
*/
public class AppContext {

private static AppContext instance;
private static ApplicationContext appContext;

private AppContext() {

}

public synchronized static AppContext getInstance() {
if (instance == null) {
instance = new AppContext();
}
return instance;
}

public static void setContext(ApplicationContext setContext) {
appContext = setContext;
}

public ApplicationContext getAppContext() {
return appContext;
}
}

如一宝宝 2009-03-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 danengnai__ 的回复:]
lowson0 再问你一下
Constants是什么东西?
[/Quote]

哦,都是一些常用的常量,封装到一个常量类里面了
danengnai__ 2009-03-28
  • 打赏
  • 举报
回复
哦 那个是你自己写的类?
danengnai__ 2009-03-28
  • 打赏
  • 举报
回复
lowson0 再问你一下
Constants是什么东西?
danengnai__ 2009-03-28
  • 打赏
  • 举报
回复
回答的这么详细呀,不给分就太说不过去了

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧