Project Presentation Question and Answers JAVA
Project Presentation Question and Answers JAVA
Framework vs Library
<dependency>
<groupId>org.springframework</grou
pId>
<artifactId>spring-webmvc</artifactId
>
<version>${spring.version}</version>
</dependency>
If you don't use pom.xml then while
transfering project you will have to
transfer all jar files over the network
many times.
Maven give re-usability of jar files in
multiple project. You need not to have
deep copy of all jar files in
WEB-INF/lib folder.
EDIT 1
7)What is Spring?
Ans:- Spring is a framework. Spring
Framework is a Java platform that
provides comprehensive infrastructure
support for developing Java applications.
Spring handles the infrastructure so you
can focus on your application.
Spring enables you to build applications
from “plain old Java objects” (POJOs) and
to apply enterprise services non-invasively
to POJOs. This capability applies to the
Java SE programming model and to full
and partial Java EE.
Examples of how you, as an application
developer, can use the Spring platform
advantage:
Make a Java method execute in a
database transaction without having to
deal with transaction APIs.
Make a local Java method a remote
procedure without having to deal with
remote APIs.
Make a local Java method a
management operation without having
to deal with JMX APIs.
Make a local Java method a message
handler without having to deal with
JMS APIs.
8) What Are the Benefits of Using
Spring?
Spring targets to make Jakarta EE
development easier, so let’s look at the
advantages:
Lightweight – There is a slight overhead of
using the framework in development.
Inversion of Control (IoC) – Spring
container takes care of wiring
dependencies of various objects instead of
creating or looking for dependent objects.
Aspect-Oriented Programming (AOP) –
Spring supports AOP to separate business
logic from system services.
IoC container – manages Spring Bean life
cycle and project-specific configurations
MVC framework – used to create web
applications or RESTful web services,
capable of returning XML/JSON responses
Transaction management – reduces the
amount of boilerplate code in JDBC
operations, file uploading, etc., either by
using Java annotations or by Spring Bean
XML configuration file
Exception Handling – Spring provides a
convenient API for translating technology-
specific exceptions into unchecked
exceptions.
9)Name some of the Spring modules.
Ans:- Core – a key module that provides
fundamental parts of the framework, such as IoC
or DI
JDBC – enables a JDBC-abstraction layer that
removes the need to do JDBC coding for specific
vendor databases
ORM integration – provides integration layers for
popular object-relational mapping APIs, such as
JPA, JDO and Hibernate
Web – a web-oriented integration module that
provides multipart file upload, Servlet listeners and
web-oriented application context functionalities
MVC framework – a web module implementing
the Model View Controller design pattern
AOP module – aspect-oriented programming
implementation allowing the definition of clean
method-interceptors and pointcuts
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</groupId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</groupId>
</dependency>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
24) Why Spring Boot is called as
opinionated?
Ans:-
Spring boot automatically configures a lot of
dependencies just by its availability in the
classpath. For example, it can auto configure
tomcat if the server container is not available. This
is why spring boot is opinionated because it auto-
configure many dependencies if it is not needed
and we can override auto-configuration settings as
needed.
Auto configuration is disabled by default and use
one of the
annotations @SpringBootApplication or @Enable
AutoConfiguration on the Main class to enable the
feature.
25) What is the difference between
RestController and Controller?
Ans:- @Controller is supposed to return a
response to the client through View ( either JSP or
Thymeleaf). Whereas @RestController
( @Controller + @ResponseBody ) is supposed to
return the response directly to the client in the
form of JSON or text or html or xml. There is no
View in case of RestController.
26) What embedded containers does Spring
Boot support?
Ans:- Spring Boot includes support for embedded
Tomcat, Jetty, and Undertow servers.
By default the embedded server will listen for
HTTP requests on port 8080.
27) What is the difference between
embedded container and a war?
Ans:- The main difference between an embedded
container and a war is that you can run Spring boot
application as a jar from commad prompt without
setting up a web server.
But to run a war file , you need to first set up a
web container like Tomcat and then deploy war file
there.
28) Explain JPA Repository in Spring.
Ans:- JPA is an abbreviation for JAVA
Persistence API (Application program
interface). As the name suggests, JPA helps in
persisting the java objects in relational
databases.
In the Java Spring framework, JPA-based
repositories are commonly used. These
repositories are crucial in managing database
operations and are a part of the Spring Data
JPA module. Repositories define a new
elegant method of storing, updating, and
extracting the data stored from JAVA
applications in the backend. All of the CRUD
(Create, read, update, and delete) operations
can be implemented with the help of a
repository interface.
@Transactional
public Long registerUser(User user) {
// execute some SQL that e.g.
// inserts the user into the db and retrieves
the autogenerated id
// userDao.save(user);
return id;
}
}
connection.commit(); // (1)
} catch (SQLException e) {
connection.rollback(); // (1)
}
}
}
This is what Spring’s transactional annotation does
for you automatically, without you having to write
it explicitly.
@Autowired
Private CustomerService service;
e.g.
@Component
@Component
public class Student
{
default constructor
other stuff
}
@Component
public class Course
{
@Autowired
private Student student;
}
@Bean
public class Student
{
parameterized constructor accepting rollno
other stuff
}
@Configuration
public class MyConfiguration
{
@Bean
public Student getStudent()
{
// some logic to create Student instance
Student s1=new Student(random
number);
return s1;
}
}
@Component
public class Course
{
@Autowired
private Student student;
}
What is origin?
origin consists of
"protocol" ,"domain" and "port"
e.g. http://myserver.com:8000
here "http" - protocol
"myserver.com" - domain name
8000 - port
@CrossOrigin("http://localhost:4200"
)
here "http://localhost:4200" is
assumed to be a different origin from
where you would like to allow access
to your Rest Service/s.