我知道这个问题已经被问了很多,我冒了一个双重(或三重或四重)的话题,但建议的解决方案似乎对我不起作用。
我遇到了很多可怕的无法自动发送错误的问题。第一次从头开始设置一个完整的Spring项目,所以我真的不知道问题是什么。
这是我目前的设置: ProjectRepo:
package be.italent.repo;
import be.italent.model.Project;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProjectRepo extends JpaRepository<Project, Integer> {
}
ProjectService:
package be.italent.services;
import be.italent.model.Project;
import be.italent.repo.ProjectRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProjectService {
@Autowired
private ProjectRepo projectRepo;
public List<Project> getAllProjects() {
return projectRepo.findAll();
}
}
ProjectRestController:
package be.italent.controllers;
import java.util.ArrayList;
import be.italent.services.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import be.italent.model.Project;
@RestController
@RequestMapping("/projects")
public class ProjectRestController {
@Autowired
private ProjectService projectService;
@RequestMapping(method = RequestMethod.GET, produces="application/json")
public ArrayList<Project> getProjects(){
ArrayList<Project> c = (ArrayList<Project>) projectService.getAllProjects();
return c;
}
}
弹簧mvc.xml
...
<context:component-scan base-package="be.italent"></context:component-scan>
...
错误:
SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常 org.springframework.beans.factory.BeanCreationException:使用名称&#39; projectRestController创建bean时出错&#39;:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private be.italent.services.ProjectService be.italent.controllers.ProjectRestController.projectService;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称&#39; projectService&#39;创建bean时出错:注册自动装配的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private be.italent.repo.ProjectRepo be.italent.services.ProjectService.projectRepo;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[be.italent.repo.ProjectRepo]类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:304) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584) 在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1572) 在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1562) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) 在java.lang.Thread.run(Thread.java:745) 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private be.italent.services.ProjectService be.italent.controllers.ProjectRestController.projectService;嵌套异常是org.springframework.beans.factory.BeanCreationException:使用名称&#39; projectService&#39;创建bean时出错:注册自动装配的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private be.italent.repo.ProjectRepo be.italent.services.ProjectService.projectRepo;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[be.italent.repo.ProjectRepo]类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ......还有22个 由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; projectService&#39;的注册:自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private be.italent.repo.ProjectRepo be.italent.services.ProjectService.projectRepo;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[be.italent.repo.ProjectRepo]类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:304) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ......还有24个 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private be.italent.repo.ProjectRepo be.italent.services.ProjectService.projectRepo;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[be.italent.repo.ProjectRepo]类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ......还有35个 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[be.italent.repo.ProjectRepo]类型的限定bean:期望至少有1个bean符合此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ......还有37个
有任何帮助吗?谢谢!
解决方案
将此添加到我的spring-mvc.xml:
<beans
...
jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
...
<jpa:repositories base-package="be.italent.repo" entity-manager-factory-ref="emf"/>
答案 0 :(得分:3)
当初始化应用程序上下文时出现问题时,Spring经常会给出这些长堆栈跟踪。通常,您可以通过查看堆栈跟踪的底部来找出最终原因。
在您的情况下,您会在底部看到此错误消息:
org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到[be.italent.repo.ProjectRepo]类型的限定bean
这意味着Spring找不到类型为public class FirstFragment extends Fragment {
EditText emailText;
EditText passText;
Button loginButton;
TextView registerView;
int counter=0;
public FirstFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
emailText = (EditText) view.findViewById(R.id.emailText);
passText = (EditText) view.findViewById(R.id.passwordText);
loginButton = (Button) view.findViewById(R.id.LoginButton);
registerView = (TextView) view.findViewById(R.id.RegisterView);
final TextInputLayout nameText = new TextInputLayout(getActivity());
final TextInputLayout DeptText = new TextInputLayout(getActivity());
final EditText phoneNumber = new EditText(getActivity());
final Button registerButton = new Button(getActivity());
final LinearLayout layout = (LinearLayout)view.findViewById(R.id.fragmentLayout);
phoneNumber.setInputType(InputType.TYPE_CLASS_PHONE);
LinearLayout.LayoutParams parameters1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) ;
nameText.setLayoutParams(parameters1);
DeptText.setLayoutParams(parameters1);
phoneNumber.setLayoutParams(parameters1);
LinearLayout.LayoutParams parameters2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) ;
parameters2.gravity = Gravity.CENTER;
registerButton.setLayoutParams(parameters2);
nameText.setHint("Enter Name");
DeptText.setHint("Enter Department");
phoneNumber.setHint("Enter Phone Number");
registerButton.setText("Register");
registerView.setTextColor(Color.BLUE);
registerView.setTextSize(17);
registerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
if(counter==1) {
layout.addView(nameText,1 );
layout.addView(DeptText, 2);
layout.addView(phoneNumber,3);
layout.removeView(loginButton);
layout.addView(registerButton,5);
registerView.setText("Already signed Up? login");
}
else{
layout.removeView(nameText);
layout.removeView(DeptText);
layout.removeView(phoneNumber);
layout.removeView(registerButton);
layout.addView(loginButton,2);
registerView.setText("New User? Sign Up");
counter=0;
}
}
});
return view;
}
}
的bean。
您的ProjectRepo
存储库接口位于包ProjectRepo
中,这是be.italent.repo
的子包,它是组件扫描的,所以问题不在于它是在错误的包中。
您是否忘记启用Spring Data JPA?
当您使用XML配置时(正如您现在所做的那样),您必须在配置文件中包含be.italent
XML标记:
repositories
使用JavaConfig时,可以使用注释:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- Tell Spring Data JPA where your repository interfaces are -->
<jpa:repositories base-package="be.italent.repo" />
<!-- ... -->
</beans>
答案 1 :(得分:-2)
问题在ProjectRestController.java中 您已将@Autowired用于ProjectService 1)
projectService
中获取和获取setter。ProjectService
设置Bean 现在运行您的应用程序。它对我有用。