I have just started with Java Spring and am getting familiar with the framework.
Let's say I have a controller with two endpoints
"/remove_old"
"/remove_new"
They do the same job: controller layer -> service layer -> DAO except for databases which should be used in dao methods - those are different. As I understand, this can be nicely handled by Spring with no change in the service layer. How should I organize my beans to make it the most appropriate way? The only solution I can think of so far is to autowire everything and then expose Dao::setDatabase method which would be called at the controller layer.
Here is a spring-boot3, ARD-solution:
(starter-used)
Simple entity:
package com.example.routingds.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Data;
#Data
#Entity
class SomeEntity {
#Id
#GeneratedValue
Long id;
}
Plus repo:
package com.example.routingds.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface SomeRepository extends JpaRepository<SomeEntity, Long> {}
An enum (for all our data sources/tenants):
package com.example.routingds.demo;
public enum MyTenant {
OLD, NEW;
}
A thingy like (ref1, ref2, ref3...):
package com.example.routingds.demo;
public class MyTenantThreadLocalContextHolder {
private static ThreadLocal<MyTenant> threadLocal = new ThreadLocal<>();
public static void set(MyTenant tenant) {
threadLocal.set(tenant);
}
public static MyTenant get() {
return threadLocal.get();
}
}
...(we have plenty options here, but this is thread safe & easy to test/static!)
Then a (very) simple controller like:
package com.example.routingds.demo;
import static com.example.routingds.demo.MyTenant.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
#Controller
public class DemoController {
#Autowired
private SomeRepository someRepository;
#DeleteMapping("/remove_new/{id}")
public void removeNew(#PathVariable Long id) {
removeInternal(NEW, id);
}
#DeleteMapping("/remove_old/{id}")
public void removeOld(#PathVariable Long id) {
removeInternal(OLD, id);
}
private void removeInternal(MyTenant current, Long id) {
// set context ...
MyTenantThreadLocalContextHolder.set(current);
// and "just delete" (ard+context will choose correct DS):
someRepository.deleteById(id);
}
}
Lets go wire it:
application.properties:
fallback.datasource.url=jdbc:h2:./data/fallback
fallback.datasource.username=sa
fallback.datasource.password=
#fallback.datasource. ... more if you like/need
old.datasource.url=jdbc:h2:./data/old
old.datasource.username=sa
old.datasource.password=
# ...
new.datasource.url=jdbc:h2:./data/new
new.datasource.username=sa
new.datasource.password=
# ...
# assuming all dbs initialized , otherwise set these (+ un-comment main class):
#spring.sql.init.mode=always
#spring.sql.init.continue-on-error=false
# debug:
spring.jpa.show-sql=true
spring.h2.console.enabled=true
# https://github.com/spring-projects/spring-data-jpa/issues/2717:
spring.jpa.properties.jakarta.persistence.sharedCache.mode=UNSPECIFIED
App/Main/Config:
package com.example.routingds.demo;
import static com.example.routingds.demo.MyTenant.*;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
//import org.springframework.boot.autoconfigure.sql.init.SqlDataSourceScriptDatabaseInitializer;
//import org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
#SpringBootApplication
public class RoutingDsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RoutingDsDemoApplication.class, args);
}
// load the props:
#Bean
#Primary // one should be primary ...
#ConfigurationProperties("fallback.datasource")
public DataSourceProperties fallbackDSProps() {
return new DataSourceProperties();
}
#Bean
#ConfigurationProperties("old.datasource")
public DataSourceProperties oldDSProps() {
return new DataSourceProperties();
}
#Bean
#ConfigurationProperties("new.datasource")
public DataSourceProperties newDSProps() {
return new DataSourceProperties();
}
// the main (abstract routing) data source:
#Bean
#Primary
public DataSource dataSource(
#Qualifier("masterDS") DataSource masterDS,
#Qualifier("newDS") DataSource newDS,
#Qualifier("oldDS") DataSource oldDS) {
return new AbstractRoutingDataSource() {
{ // inline instance:
setTargetDataSources( // ! we operationally use only OLD, NEW:
Map.of(
// lookup key, data source:
OLD, oldDS,
NEW, newDS
)
);
//... but as a default/fallback/no-context:
setDefaultTargetDataSource(masterDS);
afterPropertiesSet();
}
// inline override:
#Override
protected Object determineCurrentLookupKey() {
return MyTenantThreadLocalContextHolder.get();
}
};
}
// the "slaves" / underlying / your DS's:
#Bean // default/master/backup/unused:
DataSource masterDS(DataSourceProperties props) {
return props.initializeDataSourceBuilder().build();
}
#Bean
DataSource oldDS(#Qualifier("oldDSProps") DataSourceProperties props) {
return props.initializeDataSourceBuilder().build();
}
#Bean
DataSource newDS(#Qualifier("newDSProps") DataSourceProperties props) {
return props.initializeDataSourceBuilder().build();
}
// for (script) db initialization, we might need this (+'sql.init.mode=always'):
// #Bean
// DataSourceScriptDatabaseInitializer initOld(#Qualifier("oldDS") DataSource oldDS, SqlInitializationProperties settings) {
// return new SqlDataSourceScriptDatabaseInitializer(oldDS, settings);
// }
//
// #Bean
// DataSourceScriptDatabaseInitializer initNew(#Qualifier("newDS") DataSource newDS, SqlInitializationProperties settings) {
// return new SqlDataSourceScriptDatabaseInitializer(newDS, settings);
// }
}
Init schema (src/main/resources/schema.sql):
create table some_entity (id bigint not null, primary key (id));
create sequence some_entity_seq start with 1 increment by 50;
TESTING TIME!! :))
package com.example.routingds.demo;
import static com.example.routingds.demo.MyTenant.*;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
#SpringBootTest
#AutoConfigureMockMvc // we will test the controller!
class RoutingDsDemoApplicationTests {
// tenant/DS dependent test IDs:
static final EnumMap<MyTenant, Set<Long>> TEST_IDS = new EnumMap<>(
Map.of(
OLD, new HashSet<>(),
NEW, new HashSet<>()
)
);
#TestConfiguration // some "test setup":
static class TestDataConfig {
#Bean
InitializingBean testData(SomeRepository repo) { // <- normal/autowired repo
return () -> {
// for OLD and NEW (tenant):
Arrays.stream(MyTenant.values())
.forEach((t) -> {
// set context/db:
MyTenantThreadLocalContextHolder.set(t);
// clean up (we shouldn't need this/DANGER):
// repo.deleteAll();
// save 100 SomeEntity's, and store ids to TEST_IDS:
IntStream.range(0, 100).forEach((i) -> {
TEST_IDS.get(t).add(
repo.save(new SomeEntity()).getId()
);
});
});
};
}
}
#Autowired
MockMvc mockMvc;
#Autowired
SomeRepository helper;
#Test
void testRemoveOld() {
// for each (known) OLD id:
TEST_IDS.get(OLD).stream().forEach((id) -> {
try {
mockMvc
.perform(delete("/remove_old/" + id))
.andExpect(status().isOk());
} catch (Exception ex) {
fail(ex);
}
});
// verify deleted:
MyTenantThreadLocalContextHolder.set(OLD);
TEST_IDS.get(OLD).stream().forEach((id) -> {
assertFalse(helper.existsById(id));
});
}
#Test
void testRemoveNew() {
// for each (known) NEW id:
TEST_IDS.get(NEW).stream().forEach((id) -> {
try {
mockMvc
.perform(delete("/remove_new/" + id))
.andExpect(status().isOk());
} catch (Exception ex) {
fail(ex);
}
});
// verify deleted:
MyTenantThreadLocalContextHolder.set(NEW);
TEST_IDS.get(NEW).stream().forEach((id) -> {
assertFalse(helper.existsById(id));
});
}
}
passes:
Results:
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
See also:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-access.configure-two-datasources
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization
Related
I have encountered an interesting yet a terrifying situation. My application has two features namely, OtpListener and CdmMonitor running on two separate threads. For now, only the OtpListener feature was required so I commented out CdmMonitor in the main class.
However, I still see the logs for the commented out class CdmMonitorService. This service was running (when it shouldn't as it was commented)
Do threads implemented from the Runnable class operate this way? I did not find anything of the sort in its documentation.
Main Class: OtpTrackerApp.java
package dev.xfoil.otpTracker;
import dev.xfoil.otpTracker.service.CdmMonitorService;
import dev.xfoil.otpTracker.service.OTPListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan({"dev.xfoil.otpTracker.*","dev.xfoil.shared.*", "dev.xfoil.shared.dal.repositories.springdata.*", "dev.xfoil.shared.dal.entities.*","dev.xfoil.shared.dal.cache.*"})
public class OtpTrackerApp implements CommandLineRunner {
#Autowired
public OTPListener otpListener;
// #Autowired
// public CdmMonitorService cdmMonitorService;
public static void main(String[] args){
SpringApplication.run(OtpTrackerApp.class, args);
}
#Override
public void run(String... args) throws Exception {
// Thread monitoringThread = new Thread(cdmMonitorService);
// monitoringThread.start();
otpListener.startListening();
}
}
CdmMonitorService.java
package dev.xfoil.otpTracker.service;
import com.google.common.flogger.FluentLogger;
import dev.xfoil.otpTracker.firebase.FirestoreManager;
import dev.xfoil.shared.dal.repositories.springdata.MachineMonitorRepo;
import dev.xfoil.shared.dal.repositories.springdata.MachineOperationsHistoryRepo;
import dev.xfoil.shared.dal.repositories.springdata.MachinesRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
#Service
public class CdmMonitorService implements Runnable {
FluentLogger logger = FluentLogger.forEnclosingClass();
#Autowired
MachineMonitorRepo machineMonitorRepo;
#Autowired
MachineOperationsHistoryRepo machineOperationsHistoryRepo;
#Autowired
MachinesRepo machinesRepo;
#Autowired
FirestoreManager firestoreManager;
public CdmMonitorService() {
}
#Override
#Scheduled(fixedDelay = 120000l)
public void run() {
try {
// code removed for brevity
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
logger.atWarning().log("Exception in CDM Monitor Thread " + e);
}
}
}
OtpListener.java
package dev.xfoil.otpTracker.service;
import com.google.common.flogger.FluentLogger;
import dev.xfoil.shared.dal.entities.AccountLedger;
import dev.xfoil.shared.dal.models.OTP;
import dev.xfoil.shared.dal.repositories.springdata.LedgerRepo;
import dev.xfoil.shared.dal.repositories.springdata.NoteCountRepo;
import dev.xfoil.shared.dal.repositories.springdata.UtilRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
#Service
public class OTPListener {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
#Autowired
UtilRepo utilRepo;
#Autowired
ThirdPartyApiCalls discordServer;
#Autowired
NoteCountRepo noteCountRepo;
private static LocalDateTime latestOTPTime;
#Value("#{${webhooks}}")
HashMap<String, String> discordLink = new HashMap<String, String>();
#PostConstruct
public LocalDateTime getLatestOTPTimestamp() {
latestOTPTime = utilRepo.getTimeOfLatestOTP();
return latestOTPTime;
}
public void startListening() throws InterruptedException {
logger.atInfo().log("Current in-mem links:");
try {
// code commented for brevity
Thread.sleep(5 * 60 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Have an openapi yml file with contains a simple get request with response, from this yml have the java files below been generated (among others).
How should these java files be used?
How to hook into the generated files?
Could simply copy the generated main and controller class into the main source tree, but does not seem to be the correct way.
--- edit ---
In the generated Controller class, how do I override the default responses that exist in the interface ExampleApi? Without having to modify the generated Controller class and have it in the VCS.
--- edit ---
build.gradle.kts
...
openApiGenerate {
generatorName.set("spring")
inputSpec.set("$rootDir/specs/api-example.yml")
outputDir.set("$buildDir/generated")
apiPackage.set("com.example.openapi.generated.api")
invokerPackage.set("com.example.openapi.generated.invoker")
modelPackage.set("com.example.openapi.generated.model")
configOptions.set(mapOf(
"dateLibrary" to "java8"
))
systemProperties.set(mapOf(
"modelDocs" to "false"
))
}
...
What should be done with these classes?
package com.example.openapi.generated.invoker;
import com.fasterxml.jackson.databind.Module;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
#SpringBootApplication
#ComponentScan(basePackages = {"com.example.openapi.generated.invoker", "com.example.openapi.generated.api" , "org.openapitools.configuration"})
public class OpenAPI2SpringBoot implements CommandLineRunner {
#Override
public void run(String... arg0) throws Exception {
if (arg0.length > 0 && arg0[0].equals("exitcode")) {
throw new ExitException();
}
}
public static void main(String[] args) throws Exception {
new SpringApplication(OpenAPI2SpringBoot.class).run(args);
}
static class ExitException extends RuntimeException implements ExitCodeGenerator {
private static final long serialVersionUID = 1L;
#Override
public int getExitCode() {
return 10;
}
}
#Bean
public WebMvcConfigurer webConfigurer() {
return new WebMvcConfigurer() {
/*#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("Content-Type");
}*/
};
}
#Bean
public Module jsonNullableModule() {
return new JsonNullableModule();
}
}
package com.example.openapi.generated.api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.NativeWebRequest;
import java.util.Optional;
#Controller
#RequestMapping("${openapi.basic.base-path:}")
public class ExampleApiController implements ExampleApi {
private final NativeWebRequest request;
#org.springframework.beans.factory.annotation.Autowired
public ExampleApiController(NativeWebRequest request) {
this.request = request;
}
#Override
public Optional<NativeWebRequest> getRequest() {
return Optional.ofNullable(request);
}
}
You need to tell gradle to compile the files, there is no need to copy them.
Add the path with the generated files to the sourceSets of your project. Something like this:
sourceSets {
main {
java {
srcDir("$buildDir/generated")
}
}
}
I am building a REST API to access a database and having trouble / consistently getting a whitepage error. Running in circles trying to find my error and/or my error in the flow or logic of the program.
Here is my application:
package com.skilldistillery.myRest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#SpringBootApplication
#ComponentScan(basePackages= {"com.skilldistillery.edgemarketing"})
#EntityScan("com.skilldistillery.edgemarketing")
#EnableJpaRepositories("com.skilldistillery.myRest.repositories")
public class MyRestApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyRestApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyRestApplication.class, args);
}
}
My controller:
package com.skilldistillery.myRest.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.skilldistillery.edgemarketing.entities.House;
import com.skilldistillery.myRest.services.HouseService;
#RestController
#RequestMapping("api")
#CrossOrigin({ "*", "http://localhost:4200" })
public class HouseController {
#Autowired
HouseService houseServ;
#GetMapping("index/{id}")
public House show(#PathVariable("id") Integer id) {
return houseServ.show(id);
}
}
My repo:
package com.skilldistillery.myRest.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.skilldistillery.edgemarketing.entities.House;
#Repository
public interface HouseRepo extends JpaRepository<House, Integer> {
}
My service:
package com.skilldistillery.myRest.services;
import java.util.List;
import org.springframework.stereotype.Service;
import com.skilldistillery.edgemarketing.entities.House;
#Service
public interface HouseService {
List<House> index();
House show(Integer id);
}
And my ServiceImpl:
package com.skilldistillery.myRest.services;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.skilldistillery.edgemarketing.entities.House;
import com.skilldistillery.myRest.repositories.HouseRepo;
#Service
public class HouseServiceImpl {
#Autowired
HouseRepo hRepo;
public House show(Integer id) {
Optional<House> opt = hRepo.findById(id);
House house = null;
if (opt.isPresent()) {
house = opt.get();
}
return house;
}
}
It compiles and launches but via postman and browser, I am getting whitepage errors. I've scoured the internets trying to understand where I'm going wrong but not finding it. Please advise.
You can use the following solution.
Change your main class to the following code
#SpringBootApplication
public class MyrestapplicationApplication {
public static void main(String[] args) {
SpringApplication.run(MyrestapplicationApplication.class, args);
}
}
Then create a separate class for your configurations.As well as running away from tight coupled architecture.
#Configuration
#EntityScan("com.skilldistillery.edgemarketing.entities")
#EnableJpaRepositories("com.skilldistillery.myRest.repositories")
public class BusinessConfig {
#Bean
public HouseService houseService(final HouseRepo houseRepo){
return new HouseServiceImpl(houseRepo);
}
}
Your controller will then change to the following.Utilising Dependency Injection
#RestController
#RequestMapping("api")
#CrossOrigin({ "*", "http://localhost:4200" })
public class HouseController {
private HouseService houseServ;
public HouseController(HouseService houseServ) {
this.houseServ = houseServ;
}
#GetMapping(value = "index/{id}",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
public House show(#PathVariable("id") Integer id) {
return houseServ.show(id);
}
}
HouseServiceImpl should also implement HouseService
public class HouseServiceImpl implements HouseService{
private HouseRepo hRepo;
public HouseServiceImpl(HouseRepo hRepo) {
this.hRepo = hRepo;
}
#Override
public List<House> index() {
return null;
}
public House show(Integer id) {
Optional<House> opt = hRepo.findById(id);
House house = new House();
if (opt.isPresent()) {
house = opt.get();
}
return house;
}
}
*NB - don't forget to remove the following configs #Autowired,#Repository as they are now handled within the BusinessConfig class.More Beans can be defined in the BusinessConfig Class
Below my code which is returning value as null.
ConfigurationFile.java
package config;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import com.mongodb.MongoClient;
#Configuration
public class ConfigurationFile {
private static MongoTemplate mongoTemplate;
public #Bean(name="mongoTemplate")
MongoTemplate mongoTemplate()throws Exception{
mongoTemplate = new MongoTemplate(new MongoClient("localhost",27017),"Test");
System.out.println("mongoTemplateValue1--> " + mongoTemplate);
return mongoTemplate;
}
public static MongoTemplate getMongoTemplate() {
System.out.println("mongoTemplateValue-->" + mongoTemplate);
return mongoTemplate;
}
}
Client.java
package client;
import java.net.UnknownHostException;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import config.ConfigurationFile;
import extraction.Extractor;
public class Client {
private MongoTemplate mongoTemplate;
public static void main(String[] args){
Client c = new Client();
c.sample();
}
private void sample(){
SetupMongoDb();
}
private void SetupMongoDb() {
if (mongoTemplate == null) {
System.out.println("insideSetup");
mongoTemplate = ConfigurationFile.getMongoTemplate();
}
}
}
I am unable to get the mongoTemplate value. Below the output
insideSetup
mongoTemplateValue-->null
Can anyone please help on this?
Your mongoTemplate() method is never being called because you are not actually creating Spring Context when starting your application with Client.main().
Need to learn how Spring Framework works, specifically how to create application context. Then you'll need to point your context to your configuration file and use autowiring to obtain your MongoTemplate.
#Configuration
public class ConfigurationFile {
#Bean(name="mongoTemplate")
public MongoTemplate mongoTemplate()throws Exception{
MongoTemplate mongoTemplate = new MongoTemplate(new MongoClient("localhost",27017),"Test");
return mongoTemplate;
}
}
Then just use the autowired field:
#Service
public class SomeService {
#Autowired
private MongoTemplate mongoTemplate;
public doSomething() {
//Use your mongoTemplate
}
}
I have a bean annoteded with JSR 303 annotations. I also added Spring aspect (#Around) for handling MethodConstraintViolationException. My problem is: if I execute methods with correct parameters - my aspect works (is executed - breakpoints added), but when I run methods with incorrect parameters then MethodConstraintViolationException is thrown and my aspect is not executed.
package noname.exception;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.validator.method.MethodConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import noname.service.exceptions.ValidationException;
import noname.utils.ValidationExceptionProcessor;
#Aspect
public class ExceptionAspect implements Ordered {
#Autowired
private ValidationExceptionProcessor processor;
#Pointcut(value = "execution(* noname.conversionstrategy.api.IDocumentConverter.*(..))")
public void aopDocumentConverterPointcut() {
}
#Pointcut(value = "execution(* noname.service.api.IMailMerger.*(..))")
public void aopMailMargeServicePointcut() {
}
#SuppressWarnings("deprecation")
#Around("aopDocumentConverterPointcut() || aopMailMargeServicePointcut()")
public Object exceptionsAspect(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
try {
return proceedingJoinPoint.proceed();
} catch ( Throwable e ) {
if (e instanceof MethodConstraintViolationException) {
ValidationException exp = processor.process((MethodConstraintViolationException) e);
throw exp;
} else {
throw e;
}
}
}
#Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
Intefaces (IMailMerger and IDocumentConverter) are similar:
package noname.conversionstrategy.api;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import noname.service.domain.DocumentActionInput;
import noname.service.domain.DocumentActionResult;
import noname.validator.ValidActionInput;
#Validated
public interface IDocumentConverter {
DocumentActionResult convertDocument(#NotNull(message = "DocumentActionInput must be provided") #ValidActionInput DocumentActionInput document);
List<DocumentActionResult> convertDocuments(#NotNull(message = "DocumentActionInput must be provided") #ValidActionInput List<DocumentActionInput> documents);
}
I suppose spring execute first bean validation (it is probably executed with aspect too (?) ). If this validation throws MethodConstraintViolationException then my aspect is not executed, because spring aop doesn't support catching exceptions from anoother aspect (need confirmation).
I also created test with proxy (everything with my aspect looks fine):
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:applicationContextTest.xml"})
public class ExceptionAspectSpringTest {
#Autowired
private IDocumentConverter documentConverter;
#Autowired
private ExceptionAspect exceptionAspect;
private IDocumentConverter proxy;
#Before
public void setUp() {
AspectJProxyFactory aspectJProxyFactory = new AspectJProxyFactory(documentConverter);
aspectJProxyFactory.addInterface(IDocumentConverter.class);
aspectJProxyFactory.addAspect(exceptionAspect);
proxy = aspectJProxyFactory.getProxy();
}
#Test( expected = ValidationException.class )
public void shouldThownValidationException() {
DocumentActionInput document = new DocumentActionInput();
proxy.convertDocument(document);
}
}
Any help appreciated