Completed the simple
mvc application.
Changed it from
spring jdbctemplate to hibernate for Dao implementation.
Web.xml:-
<?xml version="1.0"
encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created
Web Application</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
</web-app>
Wasted lot of time in understanding how this “spring-mvc-servlet.xml”
getting loaded i.e. how spring is getting initiated when I am not specifying
which file to fetch nor the file path??
After so much of investigation I came to know that
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
Here server will take servlet-name as the standard
file name and appends -servlet at
the end like this:-
Loading XML bean definitions from
ServletContext resource [/WEB-INF/spring-mvc-servlet.xml
When you want to put your Servlet file in your custom location or with custom name, rather than the default naming convention
When you want to put your Servlet file in your custom location or with custom name, rather than the default naming convention
[servletname]-servlet.xml
and path under Web-INF/
,then you can use ContextLoaderListener
.
I ran simple java main class
to test the application functionality by placing the spring-mvc-servlet.xml
in resources folder.
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import jbr.springmvc.model.Login;
import jbr.springmvc.model.User;
import jbr.springmvc.service.UserService;
public class App {
public
static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("spring-mvc-servlet.xml");
UserService obj = (UserService)
context.getBean("userService");
Login login = new Login();
login.setUsername("admin");
login.setPassword("admin123");
User
user = obj.validateUser(login);
System.out.println(user.toString());
}
}
<build>
<finalName>SpringMvcUser</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
Use mvn jetty:run
SPRING:-
Spring framework provides flexibility to configure beans in
multiple ways such as XML, Annotations, and JavaConfig.
A technique where the instance is not directly passed but its specified how to create the instance, this helps in loose coupling and testing.
Types:-
- Setter injection
- Constructor injection
We prefer setter injection over constructor injection
because it helps in resolving cyclic dependency.
Scenario:- If A is dependent on B and vice-versa, then we
can neither create instance of A nor B because A requires instance of B passed
in the constructor and B requires instance of A be passed in the constructor,
This can be resolved by setter injection where the instance A is created first
then we can set the B’s instance.
No comments:
Post a Comment