spring上下文未加载上下文文件

eulz3vhy  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(449)

我正在创建一个基于spring的web项目,我的文件如下。
应用程序上下文.xml

<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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:annotation-config />
<context:component-scan base-package="<package>" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="packagesToScan">
        <list>
            <value><package name></value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">update</prop>
        </props>
    </property>
</bean>
<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:<filename>.properties</value>
        </property>
    </bean>

网站.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这是tomcat启动时出现的错误,

INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
May 10, 2016 6:40:19 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/<filename>.properties]

由于applicationcontext.xml没有初始化,spring没有初始化bean,因此我无法使用这些bean。请帮忙。为什么上下文加载器试图加载不正确的文件?
仅供参考,我使用的是spring 4.1.6.release。这也是一个maven项目。
编辑,以便在ike建议将类路径添加到占位符文件后修复该问题。但spring上下文尚未初始化。自动连线属性仍为空。但根据日志,spring试图使用上下文文件中提到的sessionfactory连接mysql。
tomcat部署日志。

INFO: validateJarFile(E:\Development\Eclipse\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\tripmidas\WEB-INF\lib\javax.servlet-api-3.1.0.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
May 10, 2016 7:14:24 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
May 10, 2016 7:14:24 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Tue May 10 19:14:27 IST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Tue May 10 19:14:29 IST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Tue May 10 19:14:30 IST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
May 10, 2016 7:14:32 PM com.sun.jersey.api.core.PackagesResourceConfig init

编辑2服务类

@Path("/cities")
public class CityServices {

    @Autowired
    CityDAO cityDAO;

    @POST
    @Path("/search")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response searchCities(String inputJsonString)
    {
        if ( inputJsonString != null )
        {
            JSONObject inputJson = new JSONObject();
            try {
                inputJson = new JSONObject(inputJsonString);
                if ( !inputJson.has("keyword"))
                {
                    return Response.status(Status.BAD_REQUEST).entity("Bad request").build();
                }
            } catch (Exception e) {
                return Response.status(Status.BAD_REQUEST).entity("Bad request").build();
            }

            String searchQuery = inputJson.getString("keyword");

            List<City> cities = cityDAO.searchCities(searchQuery);

            return Response.status(Status.OK).entity(cities).build();               
        }
        else {
            return Response.status(Status.BAD_REQUEST).entity("Bad request").build();
        }
    }
}

dao接口

public interface CityDAO {
    public List<City> searchCities(String searchQuery);
}

dao impl类

@Repository

    public class CityDAOImpl implements CityDAO {

        @Autowired
        private SessionFactory sessionFactory;

        @SuppressWarnings("unchecked")
        @Override
        @Transactional
        public List<City> searchCities(String searchQuery) {
            Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery("from "+ City.class.getName() + " where "
                    + "city_name like '%"+searchQuery+"%' OR city_airline_code like '%"+searchQuery+"%'");
            return query.list();
        }
    }

上下文基包标记-

<context:component-scan base-package="com._._.dao" />

com…dao是citydao所在的包。正在获取空指针异常 List<City> cities = cityDAO.searchCities(searchQuery); 这意味着自动布线没有发生。
编辑3向jersey服务类添加@component注解解决了以下问题:)
谢谢@ike3的帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题