我一直在关注一个简单的Spring MVC项目:
/Users/serdartugcu/Documents/apache-tomcat-7.0.65/bin/catalina.sh run
[2016-01-11 12:02:48,934] Artifact Offers:war exploded: Server is not connected. Deploy is not available.
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.65
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Oct 9 2015 08:36:58 UTC
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.65.0
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Mac OS X
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.11.2
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86_64
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.7.0_75-b13
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /Users/serdartugcu/Library/Caches/IntelliJIdea14/tomcat/Tomcat_7_0_65_Offers
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /Users/serdartugcu/Documents/apache-tomcat-7.0.65
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.config.file=/Users/serdartugcu/Library/Caches/IntelliJIdea14/tomcat/Tomcat_7_0_65_Offers/conf/logging.properties
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote=
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.port=1099
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.ssl=false
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.rmi.server.hostname=127.0.0.1
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/serdartugcu/Documents/apache-tomcat-7.0.65/endorsed
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/serdartugcu/Library/Caches/IntelliJIdea14/tomcat/Tomcat_7_0_65_Offers
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/serdartugcu/Documents/apache-tomcat-7.0.65
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=/Users/serdartugcu/Documents/apache-tomcat-7.0.65/temp
Jan 11, 2016 12:02:49 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/serdartugcu/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Jan 11, 2016 12:02:49 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 11, 2016 12:02:49 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 430 ms
Jan 11, 2016 12:02:49 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 11, 2016 12:02:49 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.65
Jan 11, 2016 12:02:49 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 11, 2016 12:02:49 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 11, 2016 12:02:49 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 30 ms
Connected to server
[2016-01-11 12:02:49,965] Artifact Offers:war exploded: Artifact is being deployed, please wait...
Jan 11, 2016 12:02:50 AM org.apache.catalina.loader.WebappClassLoaderBase validateJarFile
INFO: validateJarFile(/Users/serdartugcu/Projects/SpringTutorial/MVCBasics/Offers/target/Offers/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
Jan 11, 2016 12:02:50 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jan 11, 2016 12:02:50 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Jan 11, 2016 12:02:50 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Mon Jan 11 00:02:50 EET 2016]; root of context hierarchy
Jan 11, 2016 12:02:51 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 115 ms
Jan 11, 2016 12:02:51 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'offers': initialization started
Jan 11, 2016 12:02:51 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'offers-servlet': startup date [Mon Jan 11 00:02:51 EET 2016]; parent: Root WebApplicationContext
Jan 11, 2016 12:02:51 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/offers-servlet.xml]
Jan 11, 2016 12:02:51 AM org.springframework.web.context.support.XmlWebApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'offersController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.springapp.mvc.controller.OffersController.setOffersService(com.springapp.mvc.service.OffersService); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springapp.mvc.service.OffersService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5253)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5543)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1839)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$2.run(Transport.java:202)
at sun.rmi.transport.Transport$2.run(Transport.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:198)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.springapp.mvc.controller.OffersController.setOffersService(com.springapp.mvc.service.OffersService); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springapp.mvc.service.OffersService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 67 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.springapp.mvc.service.OffersService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:600)
... 69 more
Jan 11, 2016 12:02:51 AM org.springframework.web.servlet.DispatcherServlet initServletBean
...
这些是我的文件:
的web.xml:
<web-app 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">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>offers</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<description>Spring Database</description>
<resource-ref>
<res-ref-name>jdbc/spring</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:com/springapp/mvc/resources/service-context.xml
classpath*:com/springapp/mvc/resources/dao-context.xml
</param-value>
</context-param>
</web-app>
报价-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.springapp.mvc.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
服务上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.springapp.mvc.service">
</context:component-scan>
</beans>
道context.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<context:component-scan
base-package="com.springapp.mvc.dao">
</context:component-scan>
<context:annotation-config></context:annotation-config>
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/spring"
expected-type="javax.sql.DataSource">
</jee:jndi-lookup>
</beans>
OffersController.java:
package com.springapp.mvc.controller;
import com.springapp.mvc.models.Offer;
import com.springapp.mvc.service.OffersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Created by serdartugcu on 08/01/16.
*/
@Controller
public class OffersController {
private OffersService offersService;
@Autowired
public void setOffersService(OffersService offersService) {
this.offersService = offersService;
}
@RequestMapping("/")
public String showHome(ModelMap model){
model.addAttribute("message", "Hi There!");
return "hello";
}
@RequestMapping("/offer")
public String offerPageHome(ModelMap model){
model.addAttribute("message", "Offers Page!");
return "hello";
}
@RequestMapping("/sesexample")
public String sesexample(HttpSession session) {
session.setAttribute("name", "Serdar");
return "sesexample";
}
@RequestMapping("/modelexample")
public ModelAndView modelExample() {
ModelAndView mv = new ModelAndView("modelexample");
Map<String, Object> model = mv.getModel();
model.put("name", "Serdar TUGCU");
return mv;
}
@RequestMapping("/modelexample2")
public String modelexample2(Model model) {
model.addAttribute("name", "Model Example <b>2</b>");
return "modelexample";
}
@RequestMapping("/jspexample")
public String jspexample(Model model) {
model.addAttribute("name", "JSP JSTL example page");
return "jspexample";
}
@RequestMapping("/firstdbexample")
public String firstDbExample(Model model) {
System.out.println("firstDbExample metodu!!!!!!!!!!!");
List<Offer> offers = offersService.getCurrent();
model.addAttribute("offers", offers);
return "firstdbexample";
}
}
OffersDao.java:
package com.springapp.mvc.dao;
import com.springapp.mvc.models.Offer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* Created by serdartugcu on 08/01/16.
*/
@Component("offersDao")
public class OffersDao {
private NamedParameterJdbcTemplate jdbc;
@Autowired
public void setDataSource(DataSource jdbc) {
this.jdbc = new NamedParameterJdbcTemplate(jdbc);
}
public List<Offer> getOffers() {
return jdbc.query("select * from offers", new RowMapper<Offer>() {
public Offer mapRow(ResultSet rs, int rowNum) throws SQLException {
Offer offer = new Offer();
offer.setId(rs.getInt("id"));
offer.setName(rs.getString("name"));
offer.setText(rs.getString("text"));
offer.setEmail(rs.getString("email"));
return offer;
}
});
}
public boolean update(Offer offer) {
BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(offer);
return jdbc.update("update offers set name=:name, text=:text, email=:email where id=:id", params) == 1;
}
public boolean create(Offer offer) {
BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(offer);
return jdbc.update("insert into offers (name, text, email) values (:name, :text, :email)", params) == 1;
}
@Transactional
public int[] create(List<Offer> offers) {
SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(offers.toArray());
return jdbc.batchUpdate("insert into offers (id, name, text, email) values (:id, :name, :text, :email)", params);
}
public boolean delete(int id) {
MapSqlParameterSource params = new MapSqlParameterSource("id", id);
return jdbc.update("delete from offers where id=:id", params) == 1;
}
public Offer getOffer(int id) {
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("id", id);
return jdbc.queryForObject("select * from offers where id=:id", params,
new RowMapper<Offer>() {
public Offer mapRow(ResultSet rs, int rowNum)
throws SQLException {
Offer offer = new Offer();
offer.setId(rs.getInt("id"));
offer.setName(rs.getString("name"));
offer.setText(rs.getString("text"));
offer.setEmail(rs.getString("email"));
return offer;
}
});
}
}
OffersService.java:
package com.springapp.mvc.service;
import com.springapp.mvc.dao.OffersDao;
import com.springapp.mvc.models.Offer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by serdartugcu on 10/01/16.
*/
@Component
public class OffersService {
private OffersDao offersDao;
@Autowired
public void setOffersDao(OffersDao offersDao) {
this.offersDao = offersDao;
}
public List<Offer> getCurrent() {
return offersDao.getOffers();
}
}
感谢您的帮助。
答案 0 :(得分:0)
您在web.xml中错过了1个config xml文件。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:com/springapp/mvc/resources/offers-servlet.xml
classpath*:com/springapp/mvc/resources/service-context.xml
classpath*:com/springapp/mvc/resources/dao-context.xml
</param-value>
</context-param>