JAVA-10516: Revisit spring-boot module inside spring-boot-modules folder (#12038)

This commit is contained in:
freelansam
2022-04-15 14:57:20 +05:30
committed by GitHub
parent bc979b2c0d
commit 20762cfeb9
127 changed files with 116 additions and 1874 deletions
@@ -0,0 +1,14 @@
package com.baeldung.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.setProperty("spring.config.name", "demo");
SpringApplication.run(DemoApplication.class, args);
}
}
@@ -0,0 +1,21 @@
package com.baeldung.demo.components;
import com.baeldung.demo.model.Foo;
import com.baeldung.demo.repository.FooRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FooService {
@Autowired
private FooRepository fooRepository;
public Foo getFooWithId(Integer id) throws Exception {
return fooRepository.findById(id).orElse(null);
}
public Foo getFooWithName(String name) {
return fooRepository.findByName(name);
}
}
@@ -0,0 +1,13 @@
package com.baeldung.demo.exceptions;
public class CommonException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 3080004140659213332L;
public CommonException(String message) {
super(message);
}
}
@@ -0,0 +1,13 @@
package com.baeldung.demo.exceptions;
public class FooNotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 9042200028456133589L;
public FooNotFoundException(String message) {
super(message);
}
}
@@ -0,0 +1,45 @@
package com.baeldung.demo.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private String name;
public Foo() {
}
public Foo(String name) {
this.name = name;
}
public Foo(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,8 @@
package com.baeldung.demo.repository;
import com.baeldung.demo.model.Foo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface FooRepository extends JpaRepository<Foo, Integer> {
public Foo findByName(String name);
}
@@ -0,0 +1,26 @@
package com.baeldung.demo.service;
import com.baeldung.demo.model.Foo;
import com.baeldung.demo.components.FooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FooController {
@Autowired
private FooService fooService;
@GetMapping("/{id}")
public Foo getFooWithId(@PathVariable Integer id) throws Exception {
return fooService.getFooWithId(id);
}
@GetMapping("/")
public Foo getFooWithName(@RequestParam String name) throws Exception {
return fooService.getFooWithName(name);
}
}
@@ -0,0 +1,16 @@
package com.baeldung.session.exception;
import com.baeldung.demo.model.Foo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@EntityScan(basePackageClasses = Foo.class)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
System.setProperty("spring.config.name", "exception");
System.setProperty("spring.profiles.active", "exception");
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,10 @@
package com.baeldung.session.exception.repository;
import com.baeldung.demo.model.Foo;
public interface FooRepository {
void save(Foo foo);
Foo get(Integer id);
}
@@ -0,0 +1,26 @@
package com.baeldung.session.exception.repository;
import javax.persistence.EntityManagerFactory;
import com.baeldung.demo.model.Foo;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
@Profile("exception")
@Repository
public class FooRepositoryImpl implements FooRepository {
@Autowired
private EntityManagerFactory emf;
@Override
public void save(Foo foo) {
emf.unwrap(SessionFactory.class).getCurrentSession().saveOrUpdate(foo);
}
@Override
public Foo get(Integer id) {
return emf.unwrap(SessionFactory.class).getCurrentSession().get(Foo.class, id);
}
}