我想根据应用程序运行的环境获取动态类名。
请帮助我,因为我完全被难倒了。
在application-context.xml文件中,我有类似下面的内容
应用程序的context.xml
<bean id="properyFilePlaceholderConfigurer" class="some class">
<property name="location">
<value>myxml.xml </value>
</property>
</bean>
<bean id="encryptionKey" class="${dynamic class}" />
<bean id="dataSource" class="some other class ">
<property name="targetDataSource" ref="targetDbDataSource"/>
<property name="username" value="${appdb.datasource.username}" />
<property name="password" value="${appdb.datasource.password}" />
<property name="encryptionKey" value=" encryptionKey "/>
</bean>
在myxml.xml中
For local
<item key="dynamic class" value="local.class " />
For Test
<item key="dynamic class" value="test.class " />
For Production
<item key="dynamic class" value="Production.class" />
它会抛出错误
g.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [${some class }] for bean with name 'encryptionKey' defined in ServletContext resource [/WEB-INF/application-context.xml]; nested exception is java.lang.ClassNotFoundException: ${ some class }
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:581)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:206)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:493)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:251)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:190)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:1651)
at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:410)
at com.ibm.ws.webcontainer.webapp.WebGroupImpl.addWebApplication(WebGroupImpl.java:88)
at com.ibm.ws.webcontainer.VirtualHostImpl.addWebApplication(VirtualHostImpl.java:169)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApp(WSWebContainer.java:749)
at com.ibm.ws.webcontainer.WSWebContainer.addWebApplication(WSWebContainer.java:634)
at com.ibm.ws.webcontainer.component.WebContainerImpl.install(WebContainerImpl.java:422)
at com.ibm.ws.webcontainer.component.WebContainerImpl.start(WebContainerImpl.java:714)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:1154)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.fireDeployedObjectStart(DeployedApplicationImpl.java:1369)
at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:638)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:967)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:760)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.start(ApplicationMgrImpl.java:2147)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:445)
at com.ibm.ws.runtime.component.CompositionUnitImpl.start(CompositionUnitImpl.java:123)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.start(CompositionUnitMgrImpl.java:388)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl.access$500(CompositionUnitMgrImpl.java:116)
at com.ibm.ws.runtime.component.CompositionUnitMgrImpl$CUInitializer.run(CompositionUnitMgrImpl.java:994)
at com.ibm.wsspi.runtime.component.WsComponentImpl$_AsynchInitializer.run(WsComponentImpl.java:349)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1659)
Caused by: java.lang.ClassNotFoundException: ${ some class }
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at com.ibm.ws.bootstrap.ExtClassLoader.findClass(ExtClassLoader.java:198)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:665)
at java.lang.ClassLoader.loadClass(ClassLoader.java:644)
at com.ibm.ws.bootstrap.ExtClassLoader.loadClass(ExtClassLoader.java:113)
at java.lang.ClassLoader.loadClass(ClassLoader.java:627)
at com.ibm.ws.classloader.ProtectionClassLoader.loadClass(ProtectionClassLoader.java:62)
at com.ibm.ws.classloader.ProtectionClassLoader.loadClass(ProtectionClassLoader.java:58)
at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:565)
at java.lang.ClassLoader.loadClass(ClassLoader.java:627)
at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:565)
at java.lang.ClassLoader.loadClass(ClassLoader.java:627)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:230)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:371)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1157)
答案 0 :(得分:1)
<bean id="properyFilePlaceholderConfigurer" class="some class">
没有some class
(显然)。您需要该属性中的有效类。
答案 1 :(得分:-1)
如果您没有使用xml,我建议改用.properties文件。下面的代码片段完美无缺。这可能对你有所帮助。如果根据不同的环境有不同的参数/类名,则可以在每个系统中放置一个属性文件。 Spring将在运行时从系统中读取文件。
applicationContext.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:C:\\mybeans.properties</value>
</property>
</bean>
<bean id="mybean" class="${mybean}"></bean>
</beans>
mybean.properties
:
mybean=com.main.BeanB
测试程序:
package com.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Context loaded successfully");
MyBean bean = (MyBean) context.getBean("mybean");
bean.show();
((ClassPathXmlApplicationContext)context).close();
}
}
Bean类:
package com.main;
public abstract class MyBean {
abstract public void show();
}
package com.main;
public class BeanA extends MyBean{
public void show(){
System.out.println("I am in Bean A");
}
}
package com.main;
public class BeanB extends MyBean{
public void show(){
System.out.println("I am in Bean B");
}
}