我这里接口中有几个常量是这么定义
public interface Person {
public static final int BOSS_LEVEL = 5;
public static final int MANAGE_LEVEL = 2;
public static final int STAFF_LEVEL = 1;
//以下省略
}
在beans.xml中
<bean id="bossJob" class="person.attribute.Job">
<property name="jobName" value="takeMoney"/>
<property name="salary" value="20000" />
<property name="level" value="${问题在这里}"></property>
</bean>
我怎么才能把Person中的常量放进去,使得level能够获取到对应的常量,而不是直接写1、2、5。
还有 ${} ,在spring的xml中怎么用,能不能用它把程序中的某个属性传进去
public interface Person {
public static final int BOSS_LEVEL = 5;
public static final int MANAGE_LEVEL = 2;
public static final int STAFF_LEVEL = 1;
//以下省略
}
在beans.xml中
<bean id="bossJob" class="person.attribute.Job">
<property name="jobName" value="takeMoney"/>
<property name="salary" value="20000" />
<property name="level" value="${问题在这里}"></property>
</bean>
我怎么才能把Person中的常量放进去,使得level能够获取到对应的常量,而不是直接写1、2、5。
还有 ${} ,在spring的xml中怎么用,能不能用它把程序中的某个属性传进去
解决方案 »
- BBS论坛源代码
- struts1和struts2的线程安全问题
- 错误机制(ErrorPage[errorCode=404, location=/jsp/errorPage.jsp] )
- Struts2.1中vidation验证的国际化问题
- 帮忙找工作 重庆、成都均可 培训生附简历
- jboss发布ejb报错求援助
- 请教jdbc操作Oracle中CLOB或BLOB
- TomCat下静态网页左键点击超连接不能实现下载,却打开了文件???
- 关于Java排序
- StringRedisTemplate的opsForValue().setIfAbsent()方法是否存在bug?返回值为null
- Struts2示例项目中的一个有关通配符匹配的问题
- spring2.5与hibernate3.3.2整合出现的问题
这个太給力了,呵呵,我还只是没有毕业的学生,没那么厉害,还到不了自己修改框架的程度。只是纳闷为啥strtuts2有的功能,spring却没有做!
没懂。。求详解。。求代码。。你的意思是这样么:
我先定义一个map<Integer,Integer>,
然后 key = 从bean中传来的参数,然后去value寻找对应的 :value = Person.STAFF_LEVEL; ???
可是这样没有什么意义啊,感觉还不如传1、2、5呢,不知道你是不是这个意思?????求解。。
原地址:http://jitaguizhao.javaeye.com/blog/223832
几种获得spring里注册Bean的方法获得spring里注册Bean的四种方法,特别是第三种方法,简单:
一:方法一(多在struts框架中)继承BaseDispatchAction
import com.mas.wawacommunity.wap.service.UserManager;
public class BaseDispatchAction extends DispatchAction {
/**
* web应用上下文环境变量
*/
protected WebApplicationContext ctx;
protected UserManager userMgr;
/**
* 获得注册Bean
* @param beanName String 注册Bean的名称
* @return
*/
protected final Object getBean(String beanName) {
return ctx.getBean(beanName);
}
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
return mapping.findForward("index");
}
public void setServlet(ActionServlet servlet) {
this.servlet = servlet;
this.ctx = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());
this.userMgr = (UserManager) getBean("userManager");
}
}
二:方法二实现BeanFactoryAware
一定要在spring.xml中加上:
<bean id="serviceLocator" class="com.am.oa.commons.service.ServiceLocator" singleton="true" />
当对serviceLocator实例时就自动设置BeanFactory,以便后来可直接用beanFactorypublic class ServiceLocator implements BeanFactoryAware {
private static BeanFactory beanFactory = null;
private static ServiceLocator servlocator = null;
public void setBeanFactory(BeanFactory factory) throws BeansException {
this.beanFactory = factory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public static ServiceLocator getInstance() {
if (servlocator == null)
servlocator = (ServiceLocator) beanFactory.getBean("serviceLocator");
return servlocator;
}
/**
* 根据提供的bean名称得到相应的服务类
* @param servName bean名称
*/
public static Object getService(String servName) {
return beanFactory.getBean(servName);
}
/**
* 根据提供的bean名称得到对应于指定类型的服务类
* @param servName bean名称
* @param clazz 返回的bean类型,若类型不匹配,将抛出异常
*/
public static Object getService(String servName, Class clazz) {
return beanFactory.getBean(servName, clazz);
}
}
action调用:
public class UserAction extends BaseAction implements Action,ModelDriven{
private Users user = new Users();
protected ServiceLocator service = ServiceLocator.getInstance();
UserService userService = (UserService)service.getService("userService");
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return user;
}
public String getAllUser(){
HttpServletRequest request = ServletActionContext.getRequest();
List ls=userService.LoadAllObject( Users.class );
request.setAttribute("user",ls);
this.setUrl("/yonghu.jsp");
return SUCCESS;
}
}
三:方法三实现ApplicationContextAware
一定要在spring.xml中加上:
<bean id="SpringContextUtil " class="com.am.oa.commons.service.SpringContextUtil " singleton="true" />
当对SpringContextUtil 实例时就自动设置applicationContext,以便后来可直接用applicationContext
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; //Spring应用上下文环境
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
* @param name bean注册名
* @param requiredType 返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}
action调用:
package com.anymusic.oa.webwork;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.anymusic.oa.commons.service.ServiceLocator;
import com.anymusic.oa.hibernate.pojo.Role;
import com.anymusic.oa.hibernate.pojo.Users;
import com.anymusic.oa.spring.IUserService;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ModelDriven;
public class UserAction extends BaseAction implements Action,ModelDriven{
private Users user = new Users();
//不用再加载springContext.xml文件,因为在web.xml中配置了,在程序中启动是就有了.
UserService userService = (UserService) SpringContextUtil.getBean("userService");
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return user;
}
public String getAllUser(){
HttpServletRequest request = ServletActionContext.getRequest();
List ls=userService.LoadAllObject( Users.class );
request.setAttribute("user",ls);
this.setUrl("/yonghu.jsp");
return SUCCESS;
}
}
四.通过servlet 或listener设置spring的ApplicationContext,以便后来引用.
注意分别extends ContextLoaderListener和ContextLoaderServlet .然后就可用SpringContext来getBean.
覆盖原来在web.xml中配置的listener或servlet.
public class SpringContext {
private static ApplicationContext applicationContext; //Spring应用上下文环境
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
}
public class SpringContextLoaderListener extends ContextLoaderListener{ //
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
SpringContext.setApplicationContext(
WebApplicationContextUtils.getWebApplicationContext(event.getServletContext())
);
}
}
public class SpringContextLoaderServlet extends ContextLoaderServlet {
private ContextLoader contextLoader;
public void init() throws ServletException {
this.contextLoader = createContextLoader();
SpringContext.setApplicationContext(this.contextLoader.initWebApplicationContext(getServletContext()));
}
}
<bean id="field" class="org.springframework.beans.factory.config.FiledRetrievingFactoryBean">
<property name="staticField" value="xx.Xx.staticField">
</bean>
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:xxx.properties</value>
</list>
</property>
</bean>
如果是 配置文件的话, class就不知道了
哇唔!终于啊 哈哈哈哈啊,虽然你给的类名错了,但问题还是解决了。。非常感谢!
嗯,看看我的代码是不是和你期待的一样,也贴出来给碰到同样问题的人借鉴下吧 <!-- 将person接口中的常量 ,赋值给相应的bean,通过引用可以获取程序中的常量 -->
<bean id="BOSS_LEVEL_BEAN" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="person.member.Person.BOSS_LEVEL"/>
</bean>
<bean id="MANAGE_LEVEL_BEAN" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="person.member.Person.MANAGE_LEVEL"/>
</bean>
<bean id="STAFF_LEVEL_BEAN" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="person.member.Person.STAFF_LEVEL"/>
</bean>
<!-- 第二种方式也可以,感觉速度有点慢 也是通过引用-->
<!--
<bean id="person.member.Person.MANAGE_LEVEL"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
<bean id="person.member.Person.BOSS_LEVEL"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
<bean id="person.member.Person.STAFF_LEVEL"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
-->
<bean id="bossMessage" class="person.attribute.BaseMessage">
<property name="userName" value="老板"/>
<property name="age" value="30" />
</bean>
<bean id="bossJob" class="person.attribute.Job">
<property name="jobName" value="takeMoney"/>
<property name="salary" value="20000" />
<property name="level" ref="BOSS_LEVEL_BEAN"></property>
</bean>
<bean id="manageMessage" class="person.attribute.BaseMessage" scope="prototype">
<property name="userName" value="管理者"/>
<property name="age" value="25" />
</bean>
<bean id="manageJob" class="person.attribute.Job" scope="prototype">
<property name="jobName" value="management"/>
<property name="salary" value="10000" />
<property name="level" ref="MANAGE_LEVEL_BEAN"></property>
</bean>
<bean id="staffMessage" class="person.attribute.BaseMessage">
<property name="userName" value="职员"/>
<property name="age" value="20" />
</bean>
<bean id="staffJob" class="person.attribute.Job">
<property name="jobName" value="work"/>
<property name="salary" value="5000" />
<property name="level" ref="STAFF_LEVEL_BEAN"></property>
</bean>