我发现类似的问题解释了许多Web门户。但是我想这是独特的情况。我在Spring MVC应用程序中遇到错误。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController' defined in file [C:\Program Files (x86)\sts-bundle\pivotal-tc-server-developer-3.1.2.RELEASE\base-instance\wtpwebapps\ExpT1\WEB-INF\classes\com\expt\controller\TestController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.expt.repositories.CategoryRepository]: No qualifying bean of type [com.expt.repositories.CategoryRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.expt.repositories.CategoryRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
.....
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.expt.repositories.CategoryRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
代码如下。我认为已经复制了足够的代码。请帮助我了解缺少的内容以及如何解决此问题。任何帮助将是巨大的…!代码:Appitiializer:
package com.expt.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AppInitialzer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class[]{SpringAppContInit.class, SQLDevDataSource.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[]{WebMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[]{"/"};
}
}
SQLDEVConfig:
package com.expt.config;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@Profile("SQLDev")
@EnableJpaRepositories(basePackages={"com.expt.repositories"})
public class SQLDevDataSource extends AbstractJpaConfig {
@Override
public DataSource getDataSource() {
// TODO Auto-generated method stub
return createBasicDataSource("jdbc:jtds:sqlserver://LOCAL:1433/MYDB;", "net.sourceforge.jtds.jdbc.Driver", "UNMA", "PWD123");
}
public BasicDataSource createBasicDataSource(String jdbcUrl, String driverClass, String userName, String password) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(jdbcUrl);
dataSource.setDriverClassName(driverClass);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
}
SpringAPpContInit:
package com.expt.config;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class SpringAppContInit implements ApplicationContextInitializer<AnnotationConfigWebApplicationContext> {
@Override
public void initialize(AnnotationConfigWebApplicationContext applicationContext) {
// TODO Auto-generated method stub
ConfigurableEnvironment configEnv = applicationContext.getEnvironment();
configEnv.addActiveProfile("SQLDev");
}
}
AbstrackJpaConfig:
package com.expt.config;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import javax.sql.DataSource;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import com.expt.domain.Category;
public abstract class AbstractJpaConfig {
public abstract DataSource getDataSource();
@Bean(name="entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){
Map<String, String> properties = new HashMap<String, String>();
properties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, "validate");
properties.put(org.hibernate.cfg.Environment.DIALECT, SQLServer2008Dialect.class.getName());
properties.put(org.hibernate.cfg.Environment.SHOW_SQL, "true");
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
//em.setPackagesToScan(Expense.class.getPackage().getName());
em.setPackagesToScan(Category.class.getPackage().getName());
//em.setPersistenceProvider(new HibernatePersistenceProvider());
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.setJpaPropertyMap(properties);
return em;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
控制器:
package com.expt.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.expt.domain.Category;
import com.expt.repositories.CategoryRepository;
@RestController
@RequestMapping("/test")
public class TestController {
CategoryRepository catRepo;
@Autowired
public TestController(CategoryRepository catRepo) {
this.catRepo = catRepo;
}
/*private CategoryService catSvc;
@Autowired
public TestController(CategoryService catSvc) {
this.catSvc = catSvc;
}*/
@RequestMapping("/simple")
public String test(){
return "testing";
}
@RequestMapping("/json")
public Iterable<Category> testJ(){
return catRepo.findAll();
}
}
CategoryRepository:
package com.expt.repositories;
import java.math.BigDecimal;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.expt.domain.Category;
@Repository
public interface CategoryRepository extends JpaRepository<Category, BigDecimal> {
}
================================================== =======
编辑:(由于有很多评论添加到此,因此也想添加这些详细信息)
我指的是https://github.com/scottfrederick/spring-
music
。哪个public class AppInitializer implements WebApplicationInitializer
用于appinitializer。
完整代码在这里,这是我在旧项目中使用的代码。这确实手动注册了配置类,并且可以正常工作。但是当我们从扩展时并不需要AbstractAnnotationConfigDispatcherServletInitializer
。
public class AppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// TODO Auto-generated method stub
configureAppContextInitializers(servletContext, SpringAppContInit.class.getName());
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RepositoryConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
webContext.register(WebMvcConfig.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(webContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private void configureAppContextInitializers(ServletContext container, String... initClassNames) {
String initializerClasses = container.getInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM);
String delimitedClassNames = StringUtils.arrayToDelimitedString(initClassNames, " ");
if (StringUtils.hasText(initializerClasses)) {
initializerClasses += " " + delimitedClassNames;
}
else {
initializerClasses = delimitedClassNames;
}
container.setInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, initializerClasses);
}
}
在情况下AbstractAnnotationConfigDispatcherServletInitializer
,方法getRootConfigClasses
和getServletConfigClasses
做implicitly.Now我找我为什么上面的代码是不工作的原因登记。我有解决方法,例如可以通过恢复为XML或使其工作WebApplicationInitializer
。我正在寻找解决方案和原因,如果可能的话,使用更新的代码而不是API文档。我需要一个教程。API文档不会帮助我解决问题。
您自己的WebApplicationInitializer
和扩展的实现AbstractAnnotationConfigDispatcherServletInitializer
是不同的。它们的行为不同,那是因为您没有注册自定义ApplicationContextInitializer
。ApplicationContextInitializer
在javadoc中说明了如何注册的方法(请注意设置ApplicationContextInitializer
s
集合的2种方法)。
public class AppInitialzer extends AbstractAnnotationConfigDispatcherServletInitializer {
...
protected ApplicationContextInitializer<?>[] getRootApplicationContextInitializers() {
return new ApplicationContextInitializer[] { new SpringAppContInit() } ;
}
}
但是,这ApplicationContextInitializer
并不能真正添加仅通过设置环境或系统属性就无法完成的工作。
spring.profiles.active=SQLDev
当您这样做时,则不需要该init。
我也坚信您@EnableJpaRepositories
的课程是错误的,要启用这些功能的事实不应该取决于您的个人资料。
在运行该项目时,我出现了以下错误
我正在运行一个JUnit测试,嗯,我的Spring启动项目,我是这样写的: 找到依赖项[com.br.suppcomm.ocp.dao.logindao]得[com.br.suppcomm.ocp.dao.logindao]:需要至少1个具有此依赖项自动候选资格得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(
UnsatisfiedDependencyException:在文件[/home/x/workspace/botshop-backend-java/target/classes/far/botshop/backend/storage/filesystemStorageService.class]中创建名为“file SystemStorageService”的bean时出错:通过构造函数参数0表示
我通过使用Spring和Hibernate创建实体、服务和服务的JUnit测试开始了我的项目。所有这些都很管用。然后,我添加了spring-mvc来使用许多不同的分步教程来制作这个web应用程序,但是当我试图使用注释制作Controller时,我在部署期间从Glassfish得到错误。我想由于某种原因Spring没有看到我的服务,但经过多次尝试,我仍然无法处理它。 /webapp/web-inf/
我用SpringMVC4开发了一个web应用程序。对于DAO层,我添加了一个类GenericDAO:`public class GenericDAO{ ### 如下所示声明我的DAO会在启动我的应用程序时产生错误: 错误是: 在org。springframework。豆。工厂支持DefaultListableBeanFactory。raiseNoMatchingBeanFound(DefaultL