Ich versuche, die Eigenschaften Daten beim Serverstart zu laden. Dafür habe ich eine Klasse erstellt, die ServletContextListener
implementiert. Dies lädt alle Eigenschaften.Nicht in der Lage, Servlet-Kontext in Spring zu laden
Und in meiner DaoImpl-Klasse versuche ich, die Eigenschaften Daten zu erhalten und zu einigen der Zeichenfolgen initialisiert werden. Aber es wirft die Ausnahme
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'bbService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bbService': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BBDAO' defined in ServletContext resource [/WEB-INF/spring-web-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.bb.dao.BBDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
Hier ist meine Config-Klasse
public class Config implements ServletContextListener {
private static final String ATTRIBUTE_NAME = "config";
private Properties config = new Properties();
@Override
public void contextInitialized(ServletContextEvent event){
try {
Resource resource = new ClassPathResource("/uat.properties");
config = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
try {
throw new QiibException("Loading config failed");
} catch (QiibException e1) {
e1.printStackTrace();
}
}
event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
public static Config getInstance(ServletContext context) {
return (Config) context.getAttribute(ATTRIBUTE_NAME);
}
public String getProperty(String key) {
return config.getProperty(key);
}
}
Die DAOImpl Klasse ist
public class BBDaoImpl extends JdbcDaoSupport implements BBDao, ServletContextAware {
Properties properties = null;
@Autowired
private ServletContext ctx;
public void setServletContext(ServletContext servletContext) {
this.ctx = servletContext;
}
public BBDaoImpl() throws IOException {
super();
Config config = Config.getInstance(ctx); --> ctx is null here.
Was ist hier falsch?
Alle Ideen würden sehr geschätzt werden.
das ist richtig, wie es anders sein kann. 1) Sie erstellen Objekt => Konstruktor Aufruf 2) weil Sie 'ServletContextAware' haben, erhalten Sie Kontext mit' setServletContext' Aufruf. Im Grunde können Sie keinen Kontext ohne eine Instanz eines Objekts setzen, so dass der Konstruktoraufruf immer zuerst kommt. – varren
Setzen Sie die '@ Repository' Annotation in Ihre' BBDaoImpl' Klasse. –
@SandipSolanki, es wirft org.springframework.beans.factory.NoUniqueBeanDefinitionException: Keine qualifizierende Bean vom Typ 'com.qiib.dao.BBDao' verfügbar: erwartet einzelne übereinstimmende Bean aber gefunden 2: BBDaoImpl, BBDAO .. Ich habe bereits \t \t \t –
Yakhoob