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.
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);
}
}