【问题标题】:Not able to load servlet context in Spring无法在 Spring 中加载 servlet 上下文
【发布时间】:2017-12-04 09:43:06
【问题描述】:

我正在尝试在服务器启动时加载属性数据。为此,我创建了一个实现ServletContextListener 的类。这将加载所有属性。

在我的 DaoImpl 类中,我试图获取属性数据并初始化为一些字符串。但它会抛出异常

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

这是我的配置类

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);
}
}

DAOImpl 类是

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. 

这里有什么问题?

任何想法将不胜感激。

【问题讨论】:

  • 没错,怎么可能是其他方式。 1) 你创建对象 => 构造函数调用 2) 因为你有 ServletContextAware 你得到了 setServletContext 调用的上下文。基本上你不能在没有对象实例的情况下设置上下文,所以构造函数调用总是首先出现
  • @Repository 注释放在你的 BBDaoImpl 类上。
  • @SandipSolanki,它抛出 org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.qiib.dao.BBDAo' available: expected single matching bean but found 2: BBDaoImpl,BBDAO .. 我已经有
  • @varren,如何解决这个问题?
  • 为什么?为什么要将 DB 层与 Web 层联系起来(这就是您正在有效地做的事情)。而是使用 Spring 使用 @PropertySource 加载属性文件并使用 @ValueEnvironment 类来访问属性。

标签: java spring servlets servletcontextlistener


【解决方案1】:

如果您查看您的异常,您会发现您的服务类中有嵌套异常,它试图自动装配仍未初始化的 dao bean。

首先看看你的配置:- 1. 你在扫描你的 dao Repo。 2. 如果是,则在您的 Dao 类中放置任何原型(@Component)。

请查看这些,您会在异常本身中找到解决方案。

【讨论】:

【解决方案2】:

注意到你只是想获取一些属性并初始化你的类,为什么不以弹簧的方式实现这一点

  1. 把你的属性放到spring属性文件中,使BBDaoImpl实现EnvironmentAware接口和InitializingBean接口,然后可以在afterPropertiesSet方法中初始化BBDaoImplInitializingBean中声明的方法。关键过程是实现EnvironmentAware ,这样你就可以得到环境对象的引用,通过它你可以得到你的属性
  2. 或者你可以实现ServletConfigAware,和上面类似

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 2020-03-08
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2013-01-17
    • 2015-02-19
    • 2014-12-30
    相关资源
    最近更新 更多