gpt4 book ai didi

java - (Spring MVC + Hibernate 4 + 测试 4) Autowiring DAO 返回 NULL

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:14 25 4
gpt4 key购买 nike

Maven 依赖项

<!-- SPRING MVC -->
<spring-version>4.0.3.RELEASE</spring-version>
<spring-test-version>4.2.1.RELEASE</spring-test-version>
<!-- TESTS -->
<junit-Version>4.11</junit-Version>
<!-- DATA BASE -->
<hibernate.version>4.1.5.Final</hibernate.version>
<hibernate-validator>4.2.0.Final</hibernate-validator>
<mysql.connector.version>5.1.32</mysql.connector.version>

AccountDao.java

@Repository
@Transactional
public class AccountDao {

private final SessionFactory sessionFactory;

@Inject
public AccountDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
[...]
}

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringMVCInitializer.class}, loader=AnnotationConfigContextLoader.class)
public class LoadingTests extends TestMachine {

@Autowired
private AccountDao accountDao;

AppConfig.java

 @Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = "company")
@EnableTransactionManagement
public class AppConfig {

@Bean
public AccountDao accountDao() {
return new AccountDao();
}

@Bean
public PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();

/*LOCAL*/
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/compamny");
dataSource.setUsername("root");
dataSource.setPassword("");

return dataSource;
}

@Bean(name = "sessionFactory")
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setPackagesToScan("insynctive.model");
sessionFactoryBean.setHibernateProperties(hibProperties());
return sessionFactoryBean;
}

@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}

private Properties hibProperties() {
Properties properties = new Properties();
properties.put(Environment.HBM2DDL_AUTO, "create");
properties.put(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect");
properties.put(Environment.SHOW_SQL, true);
return properties;
}

}

SpringMVCConfiguration.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="company.controller")
public class SpringMVCConfiguration extends WebMvcConfigurerAdapter {

public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolverJSP = new InternalResourceViewResolver();
viewResolverJSP.setOrder(1);
viewResolverJSP.setViewClass(JstlView.class);
viewResolverJSP.setPrefix("views/jsp/");
viewResolverJSP.setSuffix(".jsp");

return viewResolverJSP;
}
}

SpringMVCInitializer.java

public class SpringMVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {AppConfig.class, SpringMVCConfiguration.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}

@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}

我的 Autowiring 的 AccountDao 返回 null为什么?但在我的网络应用程序中一切正常。

我尝试了很多事情,例如:

  • 创建一个新的 sessionFactory 但不起作用。
  • 使用:classes ={AppConfig.class,SpringMVCConfig.class}。
  • 使用:@ContextConfiguration(initializers = {SpringMVCInitializer.class}, loader=AnnotationConfigContextLoader.class) throw 类型不匹配:无法从类转换为类>
  • 使用:@SpringApplicationConfiguration(classes = {SpringMVCInitializer.class})

最佳答案

我认为您需要在 LoadingTests 类上设置初始值设定项而不是类。

@ContextConfiguration(initializers = {SpringMVCInitializer.class}, loader=AnnotationConfigContextLoader.class)

此外,请考虑在您的 DAO 上使用构造函数注入(inject)。

关于java - (Spring MVC + Hibernate 4 + 测试 4) Autowiring DAO 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32874697/

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