Witam.
Uczę się springa i to od tej strony co jej nikt nie lubi czyli od XML :D
mam problem bo coś dział a ja nie wiem dlaczego:
package pl.tomek;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
public class BannerLoader {
Resource banner;
public void setBanner(Resource banner) {
this.banner = banner;
}
public void showBanner() throws IOException{
InputStream in = banner.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while(true) {
String line = reader.readLine();
if(line==null)
break;
System.out.println(line);
}
reader.close();
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="bannerLoader" class="pl.tomek.BannerLoader"
init-method="showBanner">
<property name="banner">
<value>classpath:banner.txt</value>
</property>
</bean>
..........
</beans>
nie mogę zrozumieć jednej rzeczy :
Resource banner;
Resource
jest interfejsem, w beanach nie posiadam żadnej implementacji Resource
, a mimo to program startuje, skąd spring wstrzykuje sobie implementacje do banner
jeśli nie mam nigdzie stworzonej implementacji? na jakichś wcześniejszych przykładach gdy potrzebowałem jakąś implementacje jakiegoś interfejsu to tworzyłem beana klasy która go implementuje i później podawałem ref czy to innymi metodami..., w beanie banerLoader
w property
podaje value
ze ścieżką i nie rozumiem czemu podanie samego classpath
w value
wystarcza.