我必须使用spring mvc为银行设计一个非常大规模的项目。我已经选择使用XML配置了。我担心的是限制服务器的启动时间。将有大约2000个控制器。
我已经使用组件扫描来扫描@Controller
。它工作正常。但是,问题是当我从XML中删除组件扫描并在XML中手动添加使用bean配置的控制器bean时,它没有在IOC容器中创建控制器实例。并给我404未找到的错误。那么如何在没有XML扫描的组件的情况下配置控制器。
以下是我的代码示例。有什么帮助吗?
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>
根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);
}
}
答案 0 :(得分:1)
您获得404 - Not Found
的原因仅限于使用标记<context:annotation-config/>
。
<context:annotation-config/>
仅激活已在您的应用程序上下文中注册的bean上的注释,并为您执行基本的自动装配。它不会自动检测和注册bean。
因此没有可用的Controllers
来处理请求。
为了配置控制器,你要么切换回context:component-scan
,无论如何它都适用于你(考虑在恢复时考虑M.Deinum建议)< strong>或通过HandlerMappings
手动将控制器配置到相应的网址,例如SimpleUrlHanslerMapping(由于涉及详细程度,不推荐)
P.S。:请阅读this fabulous post,讨论<context:annotation-config>
和<context:component-scan>
答案 1 :(得分:1)
How can I configure the controller without component scanning in XML?
。您可以使用基于注释的配置来避免XML。
检查此EnableWebMvc配置。我配置了一个没有xml配置的spring3.2项目。它完全基于注释。
覆盖网络应用 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
@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注释。