gpt4 book ai didi

java - 使用<上下文:component-scan base-package/> in annotation based configuration

转载 作者:行者123 更新时间:2023-11-30 07:22:29 25 4
gpt4 key购买 nike

我已经使用了不同的资源,但仍然没有完成我的工作。这是我的基于 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;
}

}

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

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

}

最佳答案

由于您的 Controller 类位于 org.baeldung.persistence.controller 包中,因此您必须扫描此包中的组件

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

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

@EnableWebMvc

关于java - 使用<上下文:component-scan base-package/> in annotation based configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37316314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com