我有一个春季班。
@Service("dbManager")
@Repository
@Transactional
public class DatabaseManager {
GenericXmlApplicationContext context;
@PersistenceContext
private EntityManager em;
public DatabaseManager(GenericXmlApplicationContext context) {
this.context = context;
}
....
} //end of class DatabaseManager
我有 SpringUtil 类
public class SpringUtil {
public static GenericXmlApplicationContext loadSpringContext(String springXmlFile) {
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load(springXmlFile);
context.refresh();
return context;
} //end of loadSpringContext()
} //end of class SpringUtil
现在主要我正在使用类似的东西
public class Regulator {
public static void main( String[] args ) {
Test test = new Test;
test.start();
} //end of main()
} //end of class Regulator
这是测试类
public class Test {
public void start() {
String springXmlFile = "classpath:spring/plcb-app-context-xml.xml";
GenericXmlApplicationContext context = SpringUtil.loadSpringContext(springXmlFile);
} //end of reportStudent()
} //end of class Test
但我收到错误
Could not instantiate bean class [...DatabaseManager]: No default constructor
found; nested exception is java.lang.NoSuchMethodException:
...DatabaseManager.<init>()
我希望在DatabaseManager
创建类时,我创建的 spring 上下文SpringUtil.loadSpringContext(springXmlFile)
必须传递给它。我该怎么做 ?
谢谢
编辑 - - - - - - - - - -
public void switchDataSource(DatabaseType databaseType) {
DriverManagerDataSource dataSource = null;
if (databaseType == DatabaseType.LEGACY) {
dataSource = (DriverManagerDataSource)context.getBean("myLegacyDataSource");
} else if (databaseType == DatabaseType.LS360) {
dataSource = (DriverManagerDataSource)context.getBean("myLs360DataSource");
}
LocalContainerEntityManagerFactoryBean emf = context.getBean("myEmf", LocalContainerEntityManagerFactoryBean.class);
emf.setDataSource(dataSource);
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<Object> getResultList(String query, Class mappingClass) throws Exception {
Query emQuery = em.createNativeQuery(query, mappingClass);
return emQuery.getResultList();
} //end of findTraineeFromLegacy()
实际上,我的 DatabaseManager 类中有这两种方法。我正在设置上下文,所以我可以从switchDataSource()
方法中的上下文中获取 bean。
我可以做的一件事是删除实例文件并将方法更改为
public void switchDataSource(DatabaseType databaseType, GenericXmlApplicationContext context) {
....
}
这就是我这样做的原因?
谢谢