Spring - can't load bean information in service
I'd like to ask how should I load bean (defined in application xml) in my service class in Spring application. I've tried to load it using (and simillar solutions):
public class 开发者_如何学运维MyService {
public void myMethod() {
ApplicationContext context = new ClassPathXmlApplicationContext("security-config.xml");
LdapPersonDAO ldapPersonDAO = (LdapPersonDAO) context.getBean("ldapPersonDAO");
}
}
security-config.xml
<bean id="ldapPersonDAO" class="shdb.ldap_sync.dao.LdapPersonDAO">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
but no solution works for me. Is there any standard solution?
Thanks for any help,
Mateo
Try new ClassPathXmlApplicationContext("/security-config.xml"), though it would help if you provided more details on the errors you are getting.
Also, you should avoid lookups and MyService class should be also declared in app context and have ldapPersonDAO injected declarative. Then users of MyService would also have it injected, and so on up to the very main class of your application which you get from the app context as well.
精彩评论