我正在尝试使用教程和一些东西来设置 Spring 配置。似乎一切都很好,但是当我用 @Resource 调用 Bean 的构造函数时,一切都崩溃了。
我也在尝试 Apache Click 用一块石头杀死两只鸟。
拜托,谁能告诉我这里发生了什么,我该如何解决这个问题?
谢谢你。
错误:
Caused by: java.lang.RuntimeException: No Context available on ThreadLocal Context Stack
at org.apache.click.Context$ContextStack.peek(Context.java:934)
at org.apache.click.Context$ContextStack.access$000(Context.java:885)
at org.apache.click.Context.getThreadLocalContext(Context.java:168)
at org.apache.click.extras.control.MenuFactory.loadFromMenuXml(MenuFactory.java:495)
at org.apache.click.extras.control.MenuFactory.getRootMenu(MenuFactory.java:302)
at org.apache.click.extras.control.MenuFactory.getRootMenu(MenuFactory.java:255)
at org.apache.click.extras.control.MenuFactory.getRootMenu(MenuFactory.java:197)
at org.test.pages.BasePage.<init>(BasePage.java:15)
at org.test.pages.HomePage.<init>(HomePage.java:24)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
... 30 more
这是我的 applicationContext.xml:
<context:annotation-config />
<context:component-scan base-package="org.test" />
<tx:annotation-driven />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.0.10:1521:xe" />
<property name="user" value="HR" />
<property name="password" value="hr"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="ctest" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
这是我的 web.xml:
<display-name>CTest</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ClickServlet</servlet-name>
<servlet-class>org.apache.click.extras.spring.SpringClickServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ClickServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
编辑:我按照建议更改了代码,但我的 dao 仍然为空。同样在 appContext 我放了:
<context:component-scan base-package="org.test.pages" scope-resolver="org.apache.click.extras.spring.PageScopeResolver"/>
好的,我尝试在我的 IndexPage 中注入我的 Dao,但在构造函数中 cTestDao 为空。
我究竟做错了什么?
谢谢
IndexPage 类代码:
@Component @Scope("prototype")
public class IndexPage extends Page {
@Resource
protected CTestDao<Employee> cTestDao;
public IndexPage(){
super();
List<Employee> list = cTestDao.getBeans(Employee.class);
for(Employee e:list){
String s = String.format("Name:%1 Last Name:%2 Salary%3€",e.getFirstName(),e.getLastName(),e.getSalary());
System.out.println(s);
}
}
}