Add component to the spring-context

90 views
Skip to first unread message

Robert van der Spek

unread,
Mar 24, 2017, 10:59:29 AM3/24/17
to Hippo Community
I want to add one of my components to  the spring context, so that properties can be injected via spring as well.

I have followed the instructions(1). However, the property which I have @Autowired does not get injected. What am I doing wrong?

package org.example.components;

{... imports ...}

@ParametersInfo(
 type
= VakantieserviceInfo.class
)
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Service("nl.ndcmediagroep.business.components.VakantieserviceComponent")
public class VakantieserviceComponent extends EssentialsDocumentComponent {

   
@Autowired
   
public FormProcessorFactory formProcessorFactory;

   
@Override
   
public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {
         
       
super.doBeforeRender(request, response);
       
       
FormProcessor formProcessor = formProcessorFactory.build(); // formProcessor is null
   
}
}

package org.example.utils;

{... imports ...}

@Component
public class FormProcessorFactory {

   
public FormProcessor build() {
       
return new FormProcessor();
   
}
}

site/src/main/resources/META-INF.hst-assembly.overrides/hst-spring-config.xml

For the question I have simplified my code to this example.


1. https://www.onehippo.org/library/concepts/web-application/spring-managed-hst-components.html

Marijan Milicevic

unread,
Mar 24, 2017, 11:06:44 AM3/24/17
to hippo-c...@googlegroups.com
Hi,

shouldn't above be:

/hst-assembly/overrides/ 
(2 sub-directories instead of .hst-assembly.overrides)

cheers
marijan







 

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-community@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-community+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.

Robert van der Spek

unread,
Mar 24, 2017, 11:10:36 AM3/24/17
to Hippo Community
This is the path (IntelliJ presents this a little bit differently): site/src/main/resources/META-INF/hst-assembly/overrides/hst-spring-config.xml

Op vrijdag 24 maart 2017 16:06:44 UTC+1 schreef marijan milicevic:
Hi,

To post to this group, send email to hippo-c...@googlegroups.com

RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.

Woonsan Ko

unread,
Mar 24, 2017, 11:11:01 AM3/24/17
to hippo-c...@googlegroups.com
Hi Robert,

First of all, you have to change the base-package to this:

<context:component-scan base-package="org.example,org.example.components" />

It doesn't scan all the subpackages automatically. You need to use comma separated string to list all the leaf package names.

Second, what's your configuration at @hst:componentclassname property of the hst component?
I think it should be "nl.ndcmediagroep.business.components.VakantieserviceComponent" in this case because you want to register the component in spring with that name by the @Service annotation.

Basically, HST component factory tries to find the component instance in this order:
(1) checks if it can find a bean from ComponentManager by the service annotation and it uses the instance of the prototype bean if found.
(2) Otherwise, it falls back to the old way: just create a new instance by the classname defined in @hst:componentclassname property.

But, your example shows a difference between @Service annotation value ("nl.ndcmediagroep.business.components.VakantieserviceComponent") and the real FQCN of the component class ("org.example.components.VakantieserviceComponent").
In this case, you can expect it to be working because HST will find your component by the step (1). If you remove spring annotation scan later for some reason, it wouldn't fall back to step (2) properly, which might be fine considering your intention.

However, there's one caveat if you use different names (in @Serivce and FQCN itself):
- The channel manager is not able to scan @ParametersInfo annotation properly for your component because it uses only FQCN to scan that @ParametersInfo annotation.
- Therefore, it's kind of best practice to use the same string for both @Service as FQCN of the component class to avoid this caveat.

HTH,

Woonsan


--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-community@googlegroups.com

RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-community+unsubscribe@googlegroups.com.



--
71 Summer Street, 2nd Floor, Boston, MA 02110
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466

Robert van der Spek

unread,
Mar 24, 2017, 11:19:20 AM3/24/17
to Hippo Community
Thank you Woonsan, this did solve my problem. I was in the presupposition that the sub packages are automatically scanned as well. 

The difference between the @Service string and the real FQCN was a mistake in rewriting the simplified example ;-).

Kind regards,

Robert
Reply all
Reply to author
Forward
0 new messages