1. Program: Implement a simple Spring bean configuration.
import org.springframework.context.annotation.Bean;
import org.springframework.context
.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
}
2. Program: Create a Spring bean with a dependency injected through constructor.
public class Employee {
private final String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
3. Program: Define a Spring bean with a dependency injected through setter.
public class Address {
private String city;
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
}
import org.springframework.context
.annotation.Bean;
import org.springframework.context
.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public Address address() {
Address address = new Address();
address.setCity("New York");
return address;
}
}
4. Program: Implement a simple Spring MVC controller.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind
.annotation.RequestMapping;
import org.springframework.web.bind
.annotation.ResponseBody;
@Controller
public class HelloWorldController {
@RequestMapping("/")
@ResponseBody
public String helloWorld() {
return "Hello, World!";
}
}
5. Program: Configure a simple RESTful web service in Spring.
import org.springframework.boot.SpringApplication;
import org.springframework.boot
.autoconfigure.SpringBootApplication;
import org.springframework.web.bind
.annotation.GetMapping;
import org.springframework.web.bind
.annotation.RestController;
@SpringBootApplication
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
6. Program: Implement a basic Aspect-Oriented Programming (AOP) aspect.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.example.*.*(..))")
public void beforeSampleCreation() {
System.out.println
("Logging before the method execution");
}
}
7. Program: Configure Spring Security for a simple web application.
import org.springframework.context
.annotation.Configuration;
import org.springframework.security.config
.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web
.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends
WebSecurityConfigurerAdapter {
// Add security configurations here
}
8. Program: Implement a simple Spring Boot application.
import org.springframework.boot
.SpringApplication;
import org.springframework.boot
.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
9. Program: Configure database access using Spring Data JPA.
import org.springframework.data.jpa.repository
.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends
JpaRepository {
// Add custom query methods here
}
10. Program: Implement a basic Spring Boot Actuator for monitoring the application.
import org.springframework.boot
.SpringApplication;
import org.springframework.boot.actuate.autoconfigure
.endpoint.web.WebEndpointProperties;
import org.springframework.boot
.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure
.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(exclude
= SecurityAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public WebEndpointProperties
webEndpointProperties() {
return new WebEndpointProperties();
}
}