You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by kristian_widjaja <kr...@yahoo.com> on 2012/06/14 08:44:25 UTC

@Resource not working when using Spring 3.x AnnotationConfigWebApplicationContext

Hi Everyone,

I am having some issue here with @Resource in a Click Page, it seems like
the bean is not searchable nor injectable by Spring context. I suspect that
maybe it is because I am using the AnnotationConfigWebApplicationContext
instead of plain Spring XML context. 

The NullPointerException was thrown when the page get instantiated. I have
attached the full exception and below are my codes that related to the
initialization of web application context.

--web.xml

        ... [truncated for clarity]
	<context-param>
		<description>SpringFramework Context Annotation-based Config
ClassLoader</description>
		<param-name>contextClass</param-name>
	
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
	</context-param>
	<context-param>
		<description>SpringFramework Context Annotation-based Config Class
location</description>
		<param-name>contextConfigLocation</param-name>
		<param-value>in.hitme.web.context.AppConfigurationContext</param-value>
	</context-param>
	<listener>
		<description>Built-in SpringFramework Listener</description>
	
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
	</listener>
	... [truncated for clarity]
	... [truncated for clarity]
	
	<servlet>
		<servlet-name>springClickServlet</servlet-name>
	
<servlet-class>org.apache.click.extras.spring.SpringClickServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
		<init-param>
			<param-name>inject-page-beans</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springClickServlet</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>
	... [truncated for clarity]
</web-app>

--Annotation Application Context
@Configuration
@ComponentScan(basePackages = { "in.hitme.web.page" }, scopeResolver =
PageScopeResolver.class)
@PropertySource(value = { "classpath:/config/application.properties" })
@Import(value = { WebConfigurationContext.class,
PersistenceConfigurationContext.class })
public class AppConfigurationContext {

    @Bean
    public AppConfig appConfig() {
	return new AppConfigLoader();
    }

   ... [truncated for clarity]

-- Home Page
@Component
public class HomePage extends BasePage {

    private static final long serialVersionUID = -5999383948884446789L;

    @Resource(name = "appConfig")
    private AppConfig appConfig;
    
    public String title = PageConstants.TITLE_PAGE_HOME;
    
    public HomePage() {
	log.debug("AppConfig instance : " + appConfig);
	title = appConfig.get(PageConstants.TITLE_PAGE_HOME);
	log.debug("\n\nTitle : " + title + "\n\n");
    }

   ... [truncated for clarity]

For anyone who have ever been experienced the same issue, and able to
resolve it, kindly share the resolution. Thank you.

Regards,
Kristian Widjaja
http://click.1134972.n2.nabble.com/file/n7578133/click-exception.txt
click-exception.txt 

--
View this message in context: http://click.1134972.n2.nabble.com/Resource-not-working-when-using-Spring-3-x-AnnotationConfigWebApplicationContext-tp7578133.html
Sent from the click-user mailing list archive at Nabble.com.

Re: @Resource not working when using Spring 3.x AnnotationConfigWebApplicationContext

Posted by Naoki Takezoe <ta...@gmail.com>.
Hi Kristian,

Click always calls onInit() in the page events processing
except following cases:

- Page#onSecurityCheck() returns false
- Ajax request (invocation of page action)

If your case is either of these,I expect the reason is not in Click.
For example, LogoutPage is not mapped to the redirect path correctly.

Regards,

2012/6/14 kristian_widjaja <kr...@yahoo.com>:
> Hi Naoki,
>
> Thanks for your great advice. It's worked. Actually, the reason why I put it
> in the constructor is because
> when I logout from the app, spring-security redirect the page to LogoutPage,
> and the "onInit()" method of LogoutPage is not even executed. I have a small
> logic that need the AppConfig instance to get some property to be used
> before showing the index page. Any idea why is the "onInit()" is not
> executed?
>
> Thanks again for your help.
>
> Regards,
> Kristian.
>
> --
> View this message in context: http://click.1134972.n2.nabble.com/Resource-not-working-when-using-Spring-3-x-AnnotationConfigWebApplicationContext-tp7578133p7578135.html
> Sent from the click-user mailing list archive at Nabble.com.



-- 
Naoki Takezoe

Re: @Resource not working when using Spring 3.x AnnotationConfigWebApplicationContext

Posted by kristian_widjaja <kr...@yahoo.com>.
Hi Naoki,

Thanks for your great advice. It's worked. Actually, the reason why I put it
in the constructor is because
when I logout from the app, spring-security redirect the page to LogoutPage,
and the "onInit()" method of LogoutPage is not even executed. I have a small
logic that need the AppConfig instance to get some property to be used
before showing the index page. Any idea why is the "onInit()" is not
executed?

Thanks again for your help.

Regards,
Kristian.

--
View this message in context: http://click.1134972.n2.nabble.com/Resource-not-working-when-using-Spring-3-x-AnnotationConfigWebApplicationContext-tp7578133p7578135.html
Sent from the click-user mailing list archive at Nabble.com.

Re: @Resource not working when using Spring 3.x AnnotationConfigWebApplicationContext

Posted by Naoki Takezoe <ta...@gmail.com>.
Hi Kristian,

I think it can not touch AppConfig instance in the constructor of HomePage
because Spring injects AppConfig into a field after create HomePage instance.

Could you try to move code in the constructor to HomePage#onInit()?

Regards,

2012/6/14 kristian_widjaja <kr...@yahoo.com>:
> Hi Everyone,
>
> I am having some issue here with @Resource in a Click Page, it seems like
> the bean is not searchable nor injectable by Spring context. I suspect that
> maybe it is because I am using the AnnotationConfigWebApplicationContext
> instead of plain Spring XML context.
>
> The NullPointerException was thrown when the page get instantiated. I have
> attached the full exception and below are my codes that related to the
> initialization of web application context.
>
> --web.xml
>
>        ... [truncated for clarity]
>        <context-param>
>                <description>SpringFramework Context Annotation-based Config
> ClassLoader</description>
>                <param-name>contextClass</param-name>
>
> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
>        </context-param>
>        <context-param>
>                <description>SpringFramework Context Annotation-based Config Class
> location</description>
>                <param-name>contextConfigLocation</param-name>
>                <param-value>in.hitme.web.context.AppConfigurationContext</param-value>
>        </context-param>
>        <listener>
>                <description>Built-in SpringFramework Listener</description>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>
> <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
>        </listener>
>        ... [truncated for clarity]
>        ... [truncated for clarity]
>
>        <servlet>
>                <servlet-name>springClickServlet</servlet-name>
>
> <servlet-class>org.apache.click.extras.spring.SpringClickServlet</servlet-class>
>                <load-on-startup>0</load-on-startup>
>                <init-param>
>                        <param-name>inject-page-beans</param-name>
>                        <param-value>true</param-value>
>                </init-param>
>        </servlet>
>        <servlet-mapping>
>                <servlet-name>springClickServlet</servlet-name>
>                <url-pattern>*.htm</url-pattern>
>        </servlet-mapping>
>        ... [truncated for clarity]
> </web-app>
>
> --Annotation Application Context
> @Configuration
> @ComponentScan(basePackages = { "in.hitme.web.page" }, scopeResolver =
> PageScopeResolver.class)
> @PropertySource(value = { "classpath:/config/application.properties" })
> @Import(value = { WebConfigurationContext.class,
> PersistenceConfigurationContext.class })
> public class AppConfigurationContext {
>
>    @Bean
>    public AppConfig appConfig() {
>        return new AppConfigLoader();
>    }
>
>   ... [truncated for clarity]
>
> -- Home Page
> @Component
> public class HomePage extends BasePage {
>
>    private static final long serialVersionUID = -5999383948884446789L;
>
>    @Resource(name = "appConfig")
>    private AppConfig appConfig;
>
>    public String title = PageConstants.TITLE_PAGE_HOME;
>
>    public HomePage() {
>        log.debug("AppConfig instance : " + appConfig);
>        title = appConfig.get(PageConstants.TITLE_PAGE_HOME);
>        log.debug("\n\nTitle : " + title + "\n\n");
>    }
>
>   ... [truncated for clarity]
>
> For anyone who have ever been experienced the same issue, and able to
> resolve it, kindly share the resolution. Thank you.
>
> Regards,
> Kristian Widjaja
> http://click.1134972.n2.nabble.com/file/n7578133/click-exception.txt
> click-exception.txt
>
> --
> View this message in context: http://click.1134972.n2.nabble.com/Resource-not-working-when-using-Spring-3-x-AnnotationConfigWebApplicationContext-tp7578133.html
> Sent from the click-user mailing list archive at Nabble.com.



-- 
Naoki Takezoe