我是新來的Spring和嘗試基於xml的配置來註釋基本。我讀了這個教程amd編碼。它適用於基於xml的配置。 MVC Spring CRUD TutorialSpring MVC基於xml的配置註釋
現在我將所有基於xml的配置轉換爲註釋,但我遇到了問題。我讀過幾乎所有我讀過的東西,但我沒有解決這個問題。
org.springframework.beans.factory.BeanCreationException:創建名爲'personController'的bean時出錯:注入自動裝配依賴失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝配方法:public void com.ulotrix.spring.controller.PersonController.setPersonService(com.ulotrix.spring.service.PersonService);嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合要求的[com.ulotrix.spring.service.PersonService]類型的符合條件bean:期望至少1個符合此依賴關係自動裝配候選資格的bean。依賴註解:{}
AppConfig.java
@EnableWebMvc
@Configuration
@ComponentScan({ "com.ulotrix.spring.controller" })
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("com.ulotrix.spring.model");
builder.addProperties(getHibernationProperties());
return builder.buildSessionFactory();
}
private Properties getHibernationProperties() {
Properties prop = new Properties();
prop.put("hibernate.format_sql", "true");
prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return prop;
}
@Bean(name = "dataSource")
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/deneme_db2");
ds.setUsername("root");
ds.setPassword("xxx");
return ds;
}
@Bean
public HibernateTransactionManager txManger() {
return new HibernateTransactionManager(sessionFactory());
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
SpringMVCInitializer.java
public class SpringMvcInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(AppConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
PersonController.java
@Controller
public class PersonController {
private PersonService personService;
@Autowired(required = true)
@Qualifier(value = "personService")
public void setPersonService(PersonService ps) {
this.personService = ps;
}
@RequestMapping(value = "/persons", method = RequestMethod.GET)
public String listPersons(Model model) {
model.addAttribute("person", new Person());
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
//For add and update person both
@RequestMapping(value = "/person/add", method = RequestMethod.POST)
public String addPerson(@ModelAttribute("person") Person p) {
if(p.getId() == 0) {
this.personService.addPerson(p);
}else {
this.personService.updatePerson(p);
}
return "redirect:/persons";
}
@RequestMapping(value = "/remove/{id}")
public String removePerson(@PathVariable("id") int id) {
this.personService.removePerson(id);
return "redirect:/persons";
}
@RequestMapping(value = "/edit/{id}")
public String editPerson(@PathVariable("id") int id, Model model) {
model.addAttribute("person", this.personService.getPersonById(id));
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
}
我改成'@ComponentScan({「com.ulotrix.spring」})',仍然是同樣的錯誤。我正在使用Intellij IDEA,這是[鏈接](http://i.imgur.com/f4OilNt.jpg)bean decleration截圖。 – fatiherdem
您的類'PersonServiceImpl'是否註明了@Service(「personService」)? – sol4me
它是'@ Service'註釋,我改爲'Service(「personService」)'。現在它可以工作。現在又收到另一個錯誤。我瀏覽到'http:// localhost:8080/persons',錯誤是'SEVERE:Servlet.service()在servlet [dispatcher]上下文中與path []拋出異常[Request processing failed;嵌套異常是帶有根本原因的java.lang.NullPointerException] – fatiherdem