ApplicationContext bean. bean Spring @Autowired .., getBean. , bean getBean, bean . - :
@Component
public class Main
{
@Autowired
protected MyDependencyClass2 someClass1;
@Autowired
protected MyDependencyClass2 someClass2;
@PersistenceContext
protected EntityManager em;
protected void mainInternal(String[] args)
throws Exception
{
}
public static void main(String[] args)
throws Exception
{
final ApplicationContext context =
new ClassPathXmlApplicationContext("spring-context.xml");
context.getBean(Main.class).mainInternal(args);
}
}
. , getBean () getBean (String, Class), Class<T>.
new Main(), . Spring , new, , . Spring. , beans, .. , new.
, main mainInternal, . main, , .. , Spring, , Spring (, @Autowired), , , -.
, : beans, main. , mainInternal .
: . , Spring , , new.
SpringNoConextDataReaderClient client = new SpringNoConextDataReaderClient();
client Spring, . : , Spring ?
, . Spring usin . , . ( , HTTP- HTTP, .) , , .
, Spring , IReader. IReader , , , . :
singleton bean, IReader ,
public class TextFileReaderProvider {
public IReader createReader() { ... }
}
SpringNoConextDataReaderClient
public class SpringNoConextDataReaderClient {
@Autowired
protected TextFileReaderProvider readerProvider;
public SomeResult doMyComputation() {
IReader r = readerProvider.createReader();
try {
return theResult;
} finally {
r.close();
}
}
}
( @Autowired XML).
main Spring SpringNoConextDataReaderClient doMyComputation() .
, , concurrency.