2017-08-11 70 views
0

我是新的春季mvc,我正在學習一本名爲spring-mvc-beginners-guide的書。但是項目出現了一些錯誤,我無法繼續。創建我的第一個彈簧mvc項目的錯誤

This is a photo of the project structure

DispatcherServletInitializer.java

package com.packt.webstore.config; 
@Configuration 
@EnableWebMvc 
@ComponentScan("com.packt.webstore") 
public class WebApplicationContextConfig extends WebMvcConfigurerAdapter { 
@Override 
    public void configureDefaultServletHandling (DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 
    @Bean 
    public InternalResourceViewResolver getInternalResourceViewResolver() { 
     InternalResourceViewResolver resolver = new 
     InternalResourceViewResolver(); 
     resolver.setViewClass(JstlView.class); 
     resolver.setPrefix("/WEB-INF/views/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 
} 

RootApplicationContextConfig.java

package com.packt.webstore.config; 

@Configuration 
@ComponentScan("com.packt.webstore.controller") 
public class RootApplicationContextConfig { 
    @Bean 
    public DataSource dataSource() { 
     EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 
     EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/create-table.sql") 
       .addScript("db/sql/insert-data.sql").build(); 
     return db; 
    } 

    @Bean 
    public NamedParameterJdbcTemplate getJdbcTemplate() { 
     return new NamedParameterJdbcTemplate(dataSource()); 
    } 
} 

WebApplicationContextConfig.java

package com.packt.webstore.config; 

@Configuration 
@EnableWebMvc 
@ComponentScan("com.packt.webstore") 
public class WebApplicationContextConfig extends WebMvcConfigurerAdapter { 
@Override 
    public void configureDefaultServletHandling (DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 
    @Bean 
    public InternalResourceViewResolver getInternalResourceViewResolver() { 
     InternalResourceViewResolver resolver = new 
     InternalResourceViewResolver(); 
     resolver.setViewClass(JstlView.class); 
     resolver.setPrefix("/WEB-INF/views/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 
} 

HomeController.java

package com.packt.webstore.controller; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
@Controller 
public class HomeController { 
    @RequestMapping("/") 
    public String welcome(Model model) { 
     model.addAttribute("greeting", "Welcome to Web Store!"); 
     model.addAttribute("tagline", "The one and only amazing web store"); 
     return "welcome"; 
    } 
} 

ProductController.java

package com.packt.webstore.controller; 


@Controller 
public class ProductController { 

    @Autowired 
    private ProductRepository productRepository; 

    @RequestMapping("/products") 
    public String list(Model model) { 
     model.addAttribute("products", productRepository.getAllProducts()); 
     return "products"; 
    } 
} 

Product.java

package com.packt.webstore.domain; 

import java.io.Serializable; 
import java.math.BigDecimal; 
public class Product implements Serializable { 
    private static final long serialVersionUID = 3678107792576131001L; 
    private String productId; 
    private String name; 
    private BigDecimal unitPrice; 
    private String description; 
    private String manufacturer; 
    private String category; 
    private long unitsInStock; 
    private long unitsInOrder; 
    private boolean discontinued; 
    private String condition; 

    public Product() { 
     super(); 
    } 
    public Product(String productId, String name, BigDecimal unitPrice) { 
     this.productId = productId; 
     this.name = name; 
     this.unitPrice = unitPrice; 
    } 

    // Here i deleted getters and setter for compact code 
    @Override 
    public boolean equals(Object obj) { 
    if (this == obj) 
     return true; 
    if (obj == null) 
     return false; 
    if (getClass() != obj.getClass()) 
     return false; 
    Product other = (Product) obj; 
    if (productId == null) { 
     if (other.productId != null) 
      return false; 
    } else if (!productId.equals(other.productId)) 
     return false; 
    return true; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((productId == null) ? 0 : productId.hashCode()); 
     return result; 
    } 
} 

ProductRepository.java

package com.packt.webstore.domain.repository; 

@ComponentScan 
public interface ProductRepository { 
    List<Product> getAllProducts(); 
} 

InMemoryProductRepository.java

package com.packt.webstore.domain.repository.impl; 

@Repository 
public class InMemoryProductRepository implements ProductRepository { 
    @Autowired 
    private NamedParameterJdbcTemplate jdbcTemplate; 

    @Override 
    public List<Product> getAllProducts() { 
     Map<String, Object> params = new HashMap<String, Object>(); 
     List<Product> result = jdbcTemplate.query("SELECT * FROM products", params, new ProductMapper()); 
     return result; 
    } 

    private static final class ProductMapper implements RowMapper<Product> { 
     public Product mapRow(ResultSet rs, int rowNum) throws SQLException { 
      Product product = new Product(); 
      product.setProductId(rs.getString("ID")); 
      product.setName(rs.getString("NAME")); 
      product.setDescription(rs.getString("DESCRIPTION")); 
      product.setUnitPrice(rs.getBigDecimal("UNIT_PRICE")); 
      product.setManufacturer(rs.getString("MANUFACTURER")); 
      product.setCategory(rs.getString("CATEGORY")); 
      product.setCondition(rs.getString("CONDITION")); 
      product.setUnitsInStock(rs.getLong("UNITS_IN_STOCK")); 
      product.setUnitsInOrder(rs.getLong("UNITS_IN_ORDER")); 
      product.setDiscontinued(rs.getBoolean("DISCONTINUED")); 
      return product; 
     } 
    } 
} 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <build> 
<plugins> 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-war-plugin</artifactId> 
<version>2.6</version> 
<configuration> 
<failOnMissingWebXml>false</failOnMissingWebXml> 
</configuration> 
</plugin> 
</plugins> 
</build> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.packt</groupId> 
    <artifactId>webstore</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <properties> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 
    <dependencies> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.3.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
    <groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-web</artifactId> 
    <version>4.0.3.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-config</artifactId> 
    <version>4.0.3.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-ldap</artifactId> 
    <version>4.0.3.RELEASE</version> 
</dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>4.3.0.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hsqldb</groupId> 
     <artifactId>hsqldb</artifactId> 
     <version>2.3.2</version> 
    </dependency> 
    </dependencies> 
</project> 

而stacktrace

GRAVE: Context initialization failed 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productRepository': No qualifying bean of type [com.packt.webstore.domain.repository.ProductRepository] found for dependency [com.packt.webstore.domain.repository.ProductRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.packt.webstore.domain.repository.ProductRepository] found for dependency [com.packt.webstore.domain.repository.ProductRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4743) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.packt.webstore.domain.repository.ProductRepository] found for dependency [com.packt.webstore.domain.repository.ProductRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570) 
    ... 24 more 

ago 11, 2017 8:32:51 AM org.apache.catalina.core.StandardContext listenerStart 
GRAVE: Excepción enviando evento inicializado de contexto a instancia de escuchador de clase [org.springframework.web.context.ContextLoaderListener] 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productRepository': No qualifying bean of type [com.packt.webstore.domain.repository.ProductRepository] found for dependency [com.packt.webstore.domain.repository.ProductRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.packt.webstore.domain.repository.ProductRepository] found for dependency [com.packt.webstore.domain.repository.ProductRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4743) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.packt.webstore.domain.repository.ProductRepository] found for dependency [com.packt.webstore.domain.repository.ProductRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570) 
    ... 24 more 

ago 11, 2017 8:32:51 AM org.apache.catalina.core.StandardContext startInternal 
GRAVE: One or more listeners failed to start. Full details will be found in the appropriate container log file 
ago 11, 2017 8:32:51 AM org.apache.catalina.core.StandardContext startInternal 
GRAVE: Falló en arranque del Contexto [/webstore] debido a errores previos 
ago 11, 2017 8:32:51 AM org.apache.catalina.core.ApplicationContext log 
INFORMACIÓN: Closing Spring root WebApplicationContext 
ago 11, 2017 8:32:51 AM org.apache.coyote.AbstractProtocol start 
INFORMACIÓN: Starting ProtocolHandler ["http-nio-8080"] 
ago 11, 2017 8:32:51 AM org.apache.coyote.AbstractProtocol start 
INFORMACIÓN: Starting ProtocolHandler ["ajp-nio-8009"] 
ago 11, 2017 8:32:51 AM org.apache.catalina.startup.Catalina start 
INFORMACIÓN: Server startup in 2681 ms 

PD:我已經刪除了一個緊湊代碼的導入。如果你需要他們,或者其他什麼,請告訴我。在此先感謝

+0

從中刪除'@ ComponentScan'不需要那裏'公共接口ProductRepository' – Jens

+0

謝謝,已經刪除但錯誤仍然相同 –

+0

您是否在Controller中導入了正確的ProductRepository? – Jens

回答

0

我建議你選擇學習春天啓動了「正確的方式」取代@ComponentScan。 有了「舊書」,你可能會得到錯誤的體驗,並因此花費太多時間。 春天有自己非常有用的guides它可以幫助你很快開始。例如:

Serving Web Content with Spring MVC

Handling Form Submission

Accessing Data with JPA

你也可以檢查CRUD的簡單example我與Spring MVC和Spring數據JPA。

+0

謝謝Cepr0,我要去試試那些指南 –

+0

@ V.Vallejo別忘了接受/ upvote答案,如果他們幫助你...) – Cepr0

1

更改@ComponentScan("com.packt.webstore.controller")以包括您的InMemoryProductRepository所在的軟件包。

您的掃描找不到存儲庫,因爲它只掃描控制器包。

+0

我認爲這是不正確的,因爲如果我進行了更改,告訴我堆棧跟蹤中還有更多錯誤。在這裏你可以看到當前的stacktrace http://textuploader.com/doyl3 –

+0

這意味着你需要HSQL DB驅動依賴。總的來說,你無論如何都需要修復所有的錯誤。 – StanislavL

+0

但我在pom.xml中有HSQL依賴項,並且我在maven的依賴項中添加了hsqldb-2.3.2.jar。那麼我應該怎麼做才能解決這個問題? –

1

與@Component從公共接口ProductRepository

+0

我已經嘗試過,但我得到的問題相同的錯誤。你能告訴我這個改變的原因嗎?用這種方式或用ComponentScan –

+0

「@ComponentScan」用於定義組件所在的軟件包。和用於將該類作爲組件的「@Component」。 –

+0

你有沒有試過這個包路徑「@ComponentScan(」com.packt.webstore「) public class RootApplicationContextConfig {}? –

0

它應該是:

@Component 
public interface ProductRepository { 
    List<Product> getAllProducts(); 
} 

@ComponentScan是不同的東西,它掃描指定的包,如果任何註釋中創建豆。

另請參考1好的答案here

+0

謝謝兄弟,這種解釋派上用場,因爲我仍然從春天開始 –

+0

你好@V.Vallejo,如果你對任何答案感到滿意,那麼請接受。 –

相關問題