【问题标题】:Using <context:component-scan base-package /> in annotation based configuration在基于注解的配置中使用 <context:component-scan base-package />
【发布时间】:2016-09-15 22:13:29
【问题描述】:

我已经使用了不同的资源,但我仍然没有完成我的工作。这是我基于 Spring 注释的配置:-

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class PersistenceJPAConfig {

@Autowired
private Environment env;

public PersistenceJPAConfig() {
    super();
}

// beans

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}

@Bean
public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

final Properties additionalProperties() {
    final Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers",
    // "true");
    return hibernateProperties;
}

}

我的控制器类在 org.baeldung.persistence.controller 包中,并在类的顶部表示为 @Controller。当我访问 URL /products 时,我仍然收到 404 错误。这是我的控制器类

@Controller
public class ProductViewController {
@RequestMapping(value = "/products", method = RequestMethod.POST)
public String create(@ModelAttribute("product") final Product product)     {
    final ProductServiceImpl productServiceImpl = new ProductServiceImpl();
    if (productServiceImpl.create(product)) {
        return "Product with product name : " + product.getProduct_name() + "Has been created";
    } else {
        return "Error while creating the product record";
    }

}

【问题讨论】:

  • 出现 404 的异常是什么?
  • 上面写着The requested resource is not available.
  • 我定义@ComponentScan({"org.baeldung.persistence" })的方法是否正确?
  • 是的,没错。
  • 所以我不知道哪里出了问题。你能帮我解决这个问题吗

标签: java spring spring-mvc spring-data spring-annotations


【解决方案1】:

由于你的控制器类在org.baeldung.persistence.controller包中,你必须扫描这个包中的组件

@ComponentScan({ "org.baeldung.persistence.controller" })

您还需要在 PersistenceJPAConfig 类中添加 @EnableWebMvc

@EnableWebMvc

【讨论】:

  • 我已经添加了这些 LOC,但我仍然得到同样的错误。我是否必须配置其他任何东西,如 web.xml 或调度程序 servlet 才能在浏览器中运行项目?我还没有为此创建任何东西
  • 是的,您需要创建将扩展 AbstractAnnotationConfigDispatcherServletInitializer 的 servlet3 初始化程序
  • 供您参考,看看这个tutorialsdesk.com/2016/02/…
猜你喜欢
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多