MVC Spring Application Setup:: Dependencies
MVC Spring Application Setup:: Dependencies
xml
Web.xml
SpringSample-servlet.xml
o And finally update the Jar file, it’s 1.7 and we want to
to 12. So in the pom.xml file we will add it before the
</properties> line.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
And in the web xml file exactly before the line </web-app>
o We well write servlet-name (auto writing serv..), and add the name of the servlet that we
want
o And the most important to add the DespatcherServlet that we will need to handle our
application.
o So in the Maven Dependencies spring-webmvc-5.3.2.jar
org.springframework.web.servlet DispatcherServlet.class DispatcherServlet
in this form right click and select Copy qualify name and paste it in <servlet-class> in
the web.xml
<servlet>
<servlet-name>Spring Servlet</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
https://studyeasy.org/spring/spring-mvc-minimal/
If u don’t have the web.xml file then..
you can do it by Dynamic Web Project –> RightClick –> Java EE Tools –> Generate Deployment
Descriptor Stub.
The web.xml
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringSample-
servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringSample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The SpringSample-servlet.xml file inside web-inf
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Adding Support for Component Scan -->
<context:component-scan base-package="org.studyeasy" />
<!-- Configure View Resolver -->
https://studyeasy.org/spring/spring-mvc-minimal/
https://dzone.com/articles/spring-spring-boot-and-component-scan
And after the MainDirver bean is created , Spring will then inject
other beans into its field if that field is annotated with @Autowird.
So in this case , Environment , ApplicationContext, and
ConfigurableEnvironment are all injected this MainDirver bean.
AnnotationConfigApplicationContext context =
new
AnnotationConfigApplicationContext(AppContext.class);
//this is reference to Bean factory,
//Bean => java object created by spring.
//Bean factory is the place were spring keeps all
the object it creates.
The @Configuration annotation indicates that the class is a source of bean definitions. Also, we can
add it to multiple configuration classes.
The @Autowired annotation is a great way of making the need to inject a dependency in
Spring explicit. And although it's useful, there are use cases for which this annotation alone
isn't enough for Spring to understand which bean to inject.
If more than one bean of the same type is available in the container, the framework will
throw NoUniqueBeanDefinitionException, indicating that more than one bean is available
for autowiring.
If we try to load FooService into our context, the Spring framework will throw a
NoUniqueBeanDefinitionException. This is because Spring doesn't know which bean to inject. To
avoid this problem, there are several solutions. The @Qualifier annotation is one of them.
By using the @Qualifier annotation, we can eliminate the issue of which bean needs to be injected.
How it work in few word..
3. AppConfig.java @ComponentScan(“org.studyeasy”)
https://docs.spring.io/spring-framework/docs/5.0.0.RC2/spring-framework-
reference/web.html#spring-web
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue annotation specifies the generation strategies for the values of primary
keys.
@Column annotation is for the name of the db table for this variable .
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
transaction -annotation
This interface allows us to pass all the information required by Spring MVC in one return:
Dynamic Web Project –> RightClick –> Java EE Tools –> Generate Deployment Descriptor Stub.
Take it as rule: if the content is static does not change keep it in the view file, if it is dynamic
content controller file and pass the information to the view file