如何在没有组件扫描的情况下在xml中配置控制器?

编程入门 行业动态 更新时间:2024-10-25 03:31:47
本文介绍了如何在没有组件扫描的情况下在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="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xsi:schemaLocation="www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc.xsd www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/context www.springframework/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="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework/schema/beans www.springframework/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中配置控制器?

本文发布于:2023-11-27 19:41:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   组件   情况下   如何在   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!