在基于注释的配置中使用<context:component-scan base-package =“”>

时间:2016-05-19 07:13:45

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

我经历过不同的资源,但我仍然没有完成我的工作。这是我基于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。我访问网址404时仍然出现/products错误。这是我的控制器类

@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";
    }

}

1 个答案:

答案 0 :(得分:1)

您的控制器类在org.baeldung.persistence.controller包中,您必须扫描此包中的组件

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

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

@EnableWebMvc