Pergunta

I have a situation very similar to "Inject a file resource into Spring bean"

I have a controller that uses some .jasper compiled files and I am declaring them as such

//...
@Controller
public class InvoicingController {

private Resource quoteTemplate;
...//

And in my context configuration file

<bean id="invoicingController" class="x.x.InvoicingController">
    <property name="quoteTemplate" value="/WEB-INF/jasper/Quote.jasper" />
...

I set a breakpoint on the setQuoteTemplate() function and it is being called and the Resource object is being set properly when I initialize the container. However when I actually hit the controller quoteTemplate is null.

I am under the understanding that Controllers are singletons and unless there is a gap in my understanding I am not sure why the values that are set during the container's initialization become null when I hit a url that the controller handles.

EDIT:

Thanks @Sotirios Delimanolis

I ended up declaring beans as such:

    <bean id="quoteFile" class="java.io.File">
        <constructor-arg value="resources/jasper/Quote.jasper" />
    </bean>

    <bean id="quoteTemplate" class="org.springframework.core.io.FileSystemResource">
        <constructor-arg ref="quoteFile" />
    </bean>

And then @Autowireing the dependencies as such

@Autowired @Qualifier("quoteTemplate") private Resource quoteTemplate;

@Qualifier is used because I have multiple Resource implementation classes declared as beans and this makes sure the correct one gets used.

Foi útil?

Solução

You can't be using both the @Controller annotation and a <bean> declaration, unless you don't have a component-scan. You will end up with two bean definitions where the last one will overwrite the first one.

In this case, it seems like the component-scanned bean definition comes second and overwrites the bean you created with <bean>.

Choose which bean declaration method you want to use.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top