我正在创建一个基于弹簧的Web项目,我的文件如下 的applicationContext.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>
的web.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。请帮忙。也是为什么上下文加载器试图加载不正确的文件?
仅供参考,我使用的是春季4.1.6。请。这也是一个maven项目。
修改 因此,在ike建议将类路径添加到占位符文件之后,该问题得以修复。但是春天的背景还没有初始化。自动装配的属性仍为空。但是根据日志,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 class
@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 将@Component注释添加到泽西服务类解决了问题:)
感谢@ ike3的帮助。