案例1:当使用FileSystemXmlApplicationContext
时,我可以通过netbeans IDE运行应用程序,但是如果我创建一个可部署的jar,则会抛出文件未找到异常
案例2:当使用ClassPathXmlApplicationContext
时,我无法运行appplication thorugh IDE,甚至无法运行它所引发的可部署jar,org.springframework.beans.factory.NoSuchBeanDefinitionException
。
Utils类访问Spring Config:
package com.pts.utils;
import com.pts.action.login.LoginController;
import com.pts.dto.login.LoginDto;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author rpashank
*/
public class Utils {
private static Utils fxUtils;
private static ApplicationContext ctx;
public static Utils getInstance() {
if (fxUtils == null) {
fxUtils = new Utils();
}
return fxUtils;
}
private Utils() {
}
public Object getBean(String beanName) {
// ctx = new FileSystemXmlApplicationContext("/src/main/resources/spring-config.xml");
ctx = new ClassPathXmlApplicationContext("classpath*:/src/main/resources/spring-config.xml");
Object bean = ctx.getBean(beanName);
return bean;
}
}
Spring配置文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/PMA2014"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.springframework.transaction.support.TransactionTemplate" name="transactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean autowire="byName" class="com.pts.service.login.LoginServiceImpl" id="loginService" name="loginService"/>
<bean class="com.pts.dao.login.LoginDaoImpl" id="loginDao">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>