我必須爲使用spring mvc的銀行設計一個非常大型的項目。我已經選擇使用XML配置。我關心的是限制服務器的啓動時間。將會有大約2000個控制器。如何在沒有使用xml進行組件掃描的情況下配置控制器?
我已經使用組件掃描來掃描@Controller
。它運行良好。但問題是,當我從XML中刪除組件掃描並使用XML配置手動添加控制器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);
}
}
你的控制器應該在'servlet-context.xml'而不是'root-context.xml'中。你的dao也應該是一個單身而不是原型(沒有狀態,所以它的原型範圍是沒有意義的)。另外,當使用組件掃描(我真的建議你這樣做)時,你不應該使用''''掃描所有東西,你應該限制它到你自己的包和'servlet-context'來掃描僅適用於控制器。這將減少掃描類的數量並加快啓動時間。 –