2. Bootstrapping the application
Spring Boot starter dependencies are special in that they typically don’t
have any library code themselves, but instead transitively pull in other
libraries.
These starter dependencies offer three primary benefits:
Your build file will be significantly smaller and easier to manage because
you won’t need to declare a dependency on every library you might need.
You’re able to think of your dependencies in terms of what capabilities
they provide, rather than in terms of library names
You’re freed from the burden of worry about library versions. You can
trust that for a given version of Spring Boot, the versions of the libraries
brought in transitively will be compatible.
You only need to worry about which version of Spring Boot you’re
using.
3. Bootstrapping the application
Setting the dependencies within the pom.xml file
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>6.0.5</version>
</dependency>
4. Bootstrapping the application
Create a new classes “Car” , “Bike” and an interface
package com.maven.lab1;
import org.springframework.stereotype.Component;
@Component
public class Car implements Vehicle{
public void drive() {
System.out.println(“This is the car class");
}
}
5. Bootstrapping the application
@ Component is responsible for some operations.
Spring framework provides three other specific annotations to be used when
marking a class as Component.
Service: Denotes that the class provides some services. Our utility classes can
be marked as Service classes.
Repository: This annotation indicates that the class deals with CRUD
operations, usually it’s used with DAO implementations that deal with database
tables.
Controller: Mostly used with web applications or REST web services to specify
that the class is a front controller and responsible to handle user request and
return appropriate response.
Note that all these four annotations are in package org.springframework.stereotype
and part of spring-context jar.
6. Bootstrapping the application
Create a new object of the class Car in the main App.java
package com.maven.lab1;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
Car car = new Car();
car.drive();
}
}
7. Bootstrapping the application
Instead of creating an object from each class and using a controller to control the whole system using a
beanFactory
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>6.0.5</version>
</dependency>
And the App.java file will be updated as follows:
package com.maven.lab1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext context = new
ClassPathXmlApplicationContext("spring.xml");
Vehicle obj = (Vehicle) context.getBean("vehicle");
obj.drive();}}
8. Bootstrapping the application
ClassPathXmlApplicationContext("spring.xml");
A new xml file that would be used to control the whole application through the use of beans
The contents of this xml file :
<?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:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.maven.lab1"></context:component-
scan>
<bean id="vehicle" class="com.maven.lab1.Car"></bean>
</bean>
</beans>
9. Task
Using the previous information, Can you control and change the main application to print
the contents of the Bike class instead of Car class?
10. Getting started
To start our first Aspect project we need:
1. Dependent class files
2. A new xml file to remove the dependency
Bean dependencies:
In a new xml file add the beans
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/co
ntext"
xsi:schemaLocation="http://www.springframework.org/sche
ma/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">