当前位置: 首页 > 知识库问答 >
问题:

基于Spring MVC xml的配置到注释

许明朗
2023-03-14

我是Spring的新手,尝试将基于xml的配置转换为注释basic。我读了这个教程。它与基于xml的配置完美结合。MVCSpring积垢教程

现在我将所有基于xml的配置转换为注释,但我有一个问题。我几乎把我读到的东西都读了一遍,但我没有解决这个问题。

组织。springframework。豆。工厂BeanCreationException:创建名为“personController”的bean时出错:自动关联依赖项的注入失败;嵌套的异常是org。springframework。豆。工厂BeanCreationException:无法自动连线方法:public void com。乌洛特里克斯。Spring控制器。人事控制员。setPersonService(com.ulotrix.spring.service.PersonService);嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:找不到依赖项类型为[com.ulotrix.spring.service.PersonService]的符合条件的bean:至少需要1个符合此依赖项autowire候选项条件的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;
}

}

SpringMVC初始化器。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("/");

}

}

人事控制员。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";
}
}

共有1个答案

孟昆
2023-03-14

PersonService在包com中定义。乌洛特里克斯。Spring服务,因此将@ComponentScan({“com.ulotrix.spring.controller”})更改为@ComponentScan({“com.ulotrix.spring”}),因为spring可以发现包spring中定义的所有bean。

 类似资料:
  • 问题内容: 在最近我从事的一些大型项目中,选择其中一种(XML或注释)似乎变得越来越重要。随着项目的发展,一致性对于可维护性非常重要。 我的问题是:与基于注释的配置相比,基于XML的配置有哪些优势?与基于XML的配置相比,基于注释的配置有哪些优势? 问题答案: 注释有其用途,但它们不是杀死XML配置的灵丹妙药。我建议将两者混合! 例如,如果使用Spring,则将XML用于应用程序的依赖注入部分是完

  • 我最近的目标是构建一个Spring Boot应用程序,但不需要任何XML配置文件(或尽可能少),因此我希望避免使用一些XML文件(即web.XML),特别是对于一些bean定义部分。 更难的部分来了。 我想使用@AutoWired注释将一个SessionFactory bean注入到类中,但每次尝试启动应用程序时,我都得到: unsatisfiedDependencyException:创建名为“

  • Spring Data Elasticsearch repository可以通过XML配置,也可以通过JavaConfig的注解配置。 Example 35. Spring Data Elasticsearch repositories using JavaConfig(使用JavaConfig) @Configuration @EnableElasticsearchRepositories(ba

  • 我最近在使用作为Spring Boot applications(v2.2)开发的微服务,在我的公司,我们使用Keycloak作为授权服务器。我们之所以选择它,是因为我们需要复杂的策略、角色和组,我们还需要用户托管授权(UMA)来在用户之间共享资源。 有没有一种方法可以在控制器级别使用某种注释?类似于下面的伪代码:

  • 从Spring 2.5开始,可以使用annotations配置依赖注入。 因此,不是使用XML来描述bean连接,而是可以通过在相关的类,方法或字段声明上使用注释将bean配置移动到组件类本身。 在注入XML之前执行注释注入。 因此,对于通过两种方法连接的属性,后一种配置将覆盖前者。 默认情况下,Spring容器中未打开注释接线。 因此,在我们使用基于注释的布线之前,我们需要在Spring配置文件

  • 当我使用Spring framework时,我经常看到2个术语基于Java和基于注释的配置/自动生成。 如果它们不一样,你能告诉我它们之间有什么不同吗?