Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I finally think I got it! Sorry for the lack of information in my question. <strong>Ryan Stewart</strong> wrote "There's no reason your "stringList" and "validationList" can't be managed by a DI container and injected into your StringValidator.", and maybe he had this in mind. If you did, than that was the answer I was looking for, and your answer is correct, so thank you. I found it myself by experimenting in Spring.</p> <p>If I use the classes that contain the lists, than the resulting class cannot recive the lists. They are dynamically created, and I saw no way to bring them to StringValidator. Dynamically means - without the control of the container.</p> <p>The only way I could have injeted them was to inject them directly into StringValidator.</p> <p>But I forgot one thing. Spring is much more flexibile(based on my experience) - I frankly don't know how I could have solved this in Guice(haven't really tried, maybe I'll give it a go). </p> <p>Why not create the list dynamically, and use that list in the container life as a list that can be used to inject the required class?</p> <p><img src="https://i.stack.imgur.com/q6yMm.jpg" alt="enter image description here"></p> <p>The point being, when the container initializes the list:</p> <pre><code>package test; import org.springframework.stereotype.Component; import java.util.ArrayList; @Component public class StringList extends ArrayList&lt;String&gt; { } package test; import org.springframework.stereotype.Component; import java.util.ArrayList; @Component public class ValidationList extends ArrayList&lt;String&gt; { } </code></pre> <p>Or if you prefer the xml way(commented):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt; &lt;context:component-scan base-package="test"/&gt; &lt;!--&lt;bean id="validationList" class="java.util.ArrayList" scope="singleton"/&gt;--&gt; &lt;!--&lt;bean id="stringList" class="java.util.ArrayList" scope="singleton"/&gt;--&gt; &lt;/beans&gt; </code></pre> <p>That list can be used throught the life of the container, thus the application.</p> <pre><code>package test; import org.springframework.stereotype.Component; import javax.inject.Inject; import java.util.ArrayList; import java.util.List; @Component public class StringService implements Stringable { private List&lt;String&gt; stringList; @Inject public StringService(final ArrayList&lt;String&gt; stringList) { this.stringList = stringList; createList(); } //Simplified private void createList() { stringList.add("FILE1.txt"); stringList.add("FILE1.dat"); stringList.add("FILE1.pdf"); stringList.add("FILE1.rdf"); } @Override public List&lt;String&gt; getStringList() { return stringList; } } package test; import org.springframework.stereotype.Component; import javax.inject.Inject; import java.util.ArrayList; import java.util.List; @Component public class ValidationService implements Validateable { private List&lt;String&gt; validationList; @Inject public ValidationService(final ArrayList&lt;String&gt; validationList) { this.validationList = validationList; createList(); } //Simplified... private void createList() { validationList.add("FILE1.txt"); validationList.add("FILE2.txt"); validationList.add("FILE3.txt"); validationList.add("FILE4.txt"); } @Override public List&lt;String&gt; getValidationList() { return validationList; } } </code></pre> <p>And, I don't have to worry about the services, because the lists are now in the container, living their own lifecycle, thus available every time i request them.</p> <pre><code>package test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component public class StringValidator { private List&lt;String&gt; stringList; private List&lt;String&gt; validationList; private final List&lt;String&gt; validatedList = new ArrayList&lt;String&gt;(); @Autowired public StringValidator(final ArrayList&lt;String&gt; stringList, final ArrayList&lt;String&gt; validationList) { this.stringList = stringList; this.validationList = validationList; } public void validate() { for (String currentString : stringList) { for (String currentValidation : validationList) { if (currentString.equalsIgnoreCase(currentValidation)) { validatedList.add(currentString); } } } } public List&lt;String&gt; getValidatedList() { return validatedList; } } </code></pre> <p>The answer actually looks very simple, but it took me some time before I could get here.</p> <p>So, the Main class looks like this, and everything is handeled by the container.</p> <pre><code>package test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import java.util.List; @Component public class Main { @Autowired private StringValidator stringValidator; public void main() { stringValidator.validate(); final List&lt;String&gt; validatedList = stringValidator.getValidatedList(); for (String currentValid : validatedList) { System.out.println(currentValid); } } public static void main(String[] args) { ApplicationContext container = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml"); container.getBean(Main.class).main(); } } </code></pre> <p><strong>It seems possible. So to recap the answer - you could always have the dynamically created class in the container, and a pretty good decoupleing!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload