如何在没有组件扫描的情况下在xml中配置控制器? [英] How to configure controller in spring without component scanning in xml?

查看:114
本文介绍了如何在没有组件扫描的情况下在xml中配置控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用spring mvc为银行设计一个非常大规模的项目。我已经选择使用XML配置。我担心的是限制服务器的启动时间。将有大约2000个控制器。

I have to design a very large scale project for a bank using spring mvc. I already choose to go with the XML configuration. My concern is to limit the start up time of the server. There will be approximately 2000 controllers.

我已经使用组件扫描来扫描 @Controller 。它工作正常。但是,问题是当我从XML中删除组件扫描并在XML中手动使用bean配置添加控制器bean时,它没有在IOC容器中创建控制器实例。并给我404未找到的错误。那么如何在没有XML扫描组件的情况下配置控制器。

I already use component scan for scanning the @Controller. It worked fine. But, the problem is when I remove the component scan from XML and add the controller bean using bean configuration manually in XML, it didn't create the instance of controller in IOC container. And gives me the 404 not found error. So how can I configure the controller without component scanning in XML.

以下是我的代码示例。有什么帮助吗?

Followings are my code samples. Any help?

servlet-context.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!--<context:component-scan base-package="" />-->
</beans>

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="dataContext/data-context.xml" />

    <bean id="contactSetupController" class="com.stl.afs.ci.cca.controller.ContactSetupController">
        <property name="contactSetupDao" ref="contactSetupDao" />
    </bean>

    <bean id="contactSetupDao" class="com.stl.afs.ci.cca.controller.ContactSetupDao" scope="prototype">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

ContactSetupController.java

package com.stl.afs.ci.cca.controller;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/contactsetup")
public class ContactSetupController {
    private static final Logger logger = LoggerFactory.getLogger(ContactSetupController.class);


    private ContactSetupDao contactSetupDao;

    public void setContactSetupDao(ContactSetupDao contactSetupDao) {
        this.contactSetupDao = contactSetupDao;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String index(ModelMap model) {
        contactSetupDao.showDepedency();

        model.addAttribute("message", "Hello world! Nice to see you in the planet");
        return "ci/contactsetup/index";
    }
}

ContactSetupDao.java

package com.stl.afs.ci.cca.controller;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by ARNAB on 1/8/2015.
 */
public class ContactSetupDao {
    public ContactSetupDao() {
        System.out.println("------DAO------");
    }

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@" + sessionFactory);
        this.sessionFactory = sessionFactory;
    }

    @Transactional(readOnly = true)
    public void showDepedency(){
        Query query = sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM customers");
        int i = 0;
        for (Object o : query.list()) {
            i++;
        }
        System.out.println(i);
    }

}


推荐答案

如何在没有XML扫描组件的情况下配置控制器?。您可以使用基于注释的配置来避免XML。

How can I configure the controller without component scanning in XML?. you can use annotation based configuration to avoid XML.

检查这个 EnableWebMvc 配置。我配置了一个没有xml配置的spring3.2项目。它完全基于注释。

check this EnableWebMvc configuration. I configured a spring3.2 project without xml configuration. it was totally annotation based.

覆盖Web应用程序的启动 Initializer

overwrite startup of web application Initializer :

public class Initializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException 
    {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebAppConfig.class);
        ctx.setServletContext(servletContext);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        servletContext.addListener(new ContextLoaderListener(ctx));        
    }
}

应用程序上下文的配置文件

Configuration file for application context

@Configuration
@ComponentScan("com.paul.nkp")     // set your root package, it will scan all sub-package
@EnableWebMvc
@EnableTransactionManagement
@PropertySource("classpath:com/paul/nkp/application.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    /**
     * configured for read property values using @Value attibutes
     *
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean(name = "multipartResolver")
    public static MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }
    @Bean
    public LocalSessionFactoryBean sessionFactory() throws PropertyVetoException, IOException {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        ......
        sessionFactoryBean.setHibernateProperties(getHibernateProperties());
        System.out.println("Session Factory Init");

        return sessionFactoryBean;
    }
}

现在一切都在代码中,没有xml操作。有一个 ComponentScan 注释在配置文件中,它从您的基础包扫描所有spring注释。

Now everything in code, no xml manipulation. There is a ComponentScan annotation in configuration file, which scan all spring annotation from you base package.

这篇关于如何在没有组件扫描的情况下在xml中配置控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆