Summary:
In Java, we create objects but in Spring, it will create objects. To create objects,
we define our class as @Component. Those objects get stored in IoC container.
To create container, we can configure using ApplicationContext. To communicate
between objects, we use dependency Injection called @Autowired.
Spring provides 3 types of configuration to create IoC container.
ClassPathXmlApplicationContext. It not only creates container but also
create object through xml file present in resources folder. Every bean represents
one object creation. Even though we have 2 references, it points to one object
because scope is singleton by default. To avoid this, we use prototype. We can
inject values through setter injection via property having value/ref. We can also
inject values through constructor injection via constructor-arg having value/ref….
If an interface has 2 implementations, then we define in bean as autowire –
byName/byType. If ByType gives error, then define primary – true to that
particular property.
To stop creating objects by-default, we use lazy-init – true. Once we call, object
gets created.
If laptop bean need to be used by only Alien, then write inner bean.
AnnotationConfigApplicationContext. It create object through JavaConfig file
where we define @Configuration and @Bean. As every bean represents one
object, to get multiple references, define @Scope(“prototype”) under bean
annotation. We use @Autowired in method parameters.
If an interface has 2 implementations, then define @Qualifier / primary.
By defining @Component on every class, it creates object. Inside JavaConfig file,
define @ComponentScan("[Link]") // define base package.
If we use Qualifier as laptop & Primary as desktop, then Qualifier will be
preferred.
By seeing @SpringBootApplication, it will search components under the same
package. To inject values into primary datatypes, use @Value(“25”)
Client → server (Controller → service → DAO/repository) →
database
The class that represents data is called Model.
Spring JDBC – Here we use JdbcTemplate (jdbc api). H2 is an in-memory
database. To connect with, create [Link] (table creation) & [Link] (insert
query) under resources folder.
MySQL is out-memory database. Comment h2 dependency and add mysql
dependency. Add configuration for mysql under application-properties. Spring
Web – used to build web applications. To run on tomcat server, create package as
war.
Spring Boot MVC – Create views under src/main/webapp and when we return
jsp from controller, it gives error. So, add dependency Tomcat Jasper.
In servlet way - public String add (HttpServletRequest req, HttpSession session)
{
[Link](“result”, result); ${result}
In Spring way – public String add (int num1, int num2, HttpSession session) {
To change parameter name, public String add(@RequestParam("num1") int
num
To transfer data between controller & view, we must use object which is called
Model object.
[Link]("result", result);
To remove .jsp extension in return, we have view resolver which we connect
through properties file by mentioning prefix & suffix.
We can put view name in object itself through ModelAndView as addObject,
setViewName.
or
If a model has 10 fields, then we can’t send parameters, then use
@ModelAttribute.
(@ModelAttribute("alien1") Alien alien)
(Alien alien)
<p>${alien1} </p>
<p>${alien}</p>
Spring MVC – Here we need to add dependency - spring web mvc. Once we run,
it gives 404 because tomcat will talk to dispatcher servlet to be defined in
[Link]. After run, it gives error to configure dispatcher servlet under test-
[Link] (WEB-INF).