Inject bean in a plugin MyBatis has a null value - java

I am using CDI (Weld ) in a web application developed in JSF and MyBatis on a Tomcat application server.
When I inject a bean into a plugin of MyBatis, the injected bean always has a null value, instead of injecting the correct instance of the bean.
What am I doing wrong?
Regards
import es.metadata.metaqualitas.web.SessionBean;
import java.io.Serializable;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
#Intercepts({
#Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
#Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
public class ExamplePlugin implements Interceptor, Serializable {
//log has a ApplicationScope
#Inject
private Logger log;
//sessionBean has a SessionScoped
#Inject
private SessionBean sessionBean;
#Override
public Object intercept(Invocation invocation) throws Throwable {
//log and sessionBean are always worth null and launch NullPointerException
log.info(sessionBean.getUsuario());
return invocation.proceed();
}
#Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
#Override
public void setProperties(Properties properties) {
}
}
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
#ApplicationScoped
#Named
public class LoggerProducer {
#Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
-------------------------------------------
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#Named
#SessionScoped
public class SessionBean implements Serializable {
private String usuario;
public String getUsuario(){
return usuario;
}
public void setUsuario(String usuario){
this.usuario = usuario;
}
#PostConstruct
public void init() {
usuario = "Pedro";
}
}

Related

Spring Boot, Parameter 1 of constructor in ...Service required a bean of type '...Mapper' that could not be found

I'm getting bean not found error on Mapper. It was working for just user entity, then i added food entity (all same as user) now it shows error.
Description:
Parameter 1 of constructor in com.example.springmysqlelastic.service.impl.FoodService required a bean of type 'com.example.springmysqlelastic.mapper.UserAndFoodMapper' that could not be found.
Action:
Consider defining a bean of type 'com.example.springmysqlelastic.mapper.UserAndFoodMapper' in your configuration.
UserAndFoodMapper.java
package com.example.springmysqlelastic.mapper;
import com.example.springmysqlelastic.model.Food;
import com.example.springmysqlelastic.model.FoodModel;
import com.example.springmysqlelastic.model.User;
import com.example.springmysqlelastic.model.UserModel;
import com.example.springmysqlelastic.model.dto.FoodDTO;
import com.example.springmysqlelastic.model.dto.UserDTO;
import org.mapstruct.Mapper;
import java.util.List;
#Mapper(componentModel = "spring")
public interface UserAndFoodMapper {
UserDTO toUserDTO(User user);
List<UserDTO> toUserDtos(List<User> users);
User toUser(UserDTO userDTO);
List<User> toUsers(List<UserDTO> userDTOS);
UserModel toUserModel(User user);
FoodDTO toFoodDTO(Food food);
List<FoodDTO> toFoodDtos(List<Food> foods);
Food toFood(FoodDTO foodDTO);
List<Food> toFoods(List<FoodDTO> foodDTOS);
FoodModel toFoodModel(Food food);
}
FoodService.java
package com.example.springmysqlelastic.service.impl;
import com.example.springmysqlelastic.mapper.FoodMapper;
import com.example.springmysqlelastic.mapper.UserAndFoodMapper;
import com.example.springmysqlelastic.model.Food;
import com.example.springmysqlelastic.model.dto.FoodDTO;
import com.example.springmysqlelastic.repo.IFoodDAO;
import com.example.springmysqlelastic.service.IFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
#Service
public class FoodService implements IFoodService {
private IFoodDAO foodDAO;
private UserAndFoodMapper foodMapper;
//private final FoodMapper foodMapper;
#Autowired
public FoodService(IFoodDAO foodDAO, UserAndFoodMapper foodMapper) {
this.foodDAO = foodDAO;
this.foodMapper = foodMapper;
}
#Override
public FoodDTO save(FoodDTO foodDTO) {
Food food = this.foodDAO.save(this.foodMapper.toFood(foodDTO));
return this.foodMapper.toFoodDTO(food);
}
#Override
public FoodDTO findById(Long id) {
return this.foodMapper.toFoodDTO(this.foodDAO.findById(id).orElse(null));
}
#Override
public List<FoodDTO> findAll() {
return this.foodMapper.toFoodDtos(this.foodDAO.findAll());
}
}

How to test Spring's Cacheable on a method?

Recently in my project I was asked to use Spring's #Cacheable annotation for one of the method which returns static referenceData from the database. I followed this blog https://medium.com/#d.lopez.j/configuring-multiple-ttl-caches-in-spring-boot-dinamically-75f4aa6809f3 which guides to have caches created dynamically. I am trying to test this implementation by following this SO answer How to test Spring's declarative caching support on Spring Data repositories? and I am facing issues. I am not able to load the properties from the application-test.properties into my test class.
My CachingConfig Class
package org.vinodh.testing;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.Data;
#Configuration
#ConfigurationProperties(prefix = "caching")
#Data
public class CachingConfig {
private Map<String, CacheSpec> specs;
#Data
public static class CacheSpec {
private int minutesToExpire;
private int maximumSize;
public int getMaximumSize() {
return maximumSize;
}
public void setMaximumSize(int maximumSize) {
this.maximumSize = maximumSize;
}
public int getMinutesToExpire() {
return minutesToExpire;
}
public void setMinutesToExpire(int minutesToExpire) {
this.minutesToExpire = minutesToExpire;
}
}
public Map<String, CacheSpec> getSpecs() {
return specs;
}
#Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
if (specs != null) {
List<CaffeineCache> caches = specs.entrySet().stream()
.map(entry -> buildCache(entry.getKey(), entry.getValue())).collect(Collectors.toList());
cacheManager.setCaches(caches);
}
return cacheManager;
}
private CaffeineCache buildCache(String name, CacheSpec specs) {
Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder()
.expireAfterWrite(specs.getMinutesToExpire(), TimeUnit.MINUTES).maximumSize(specs.getMaximumSize());
return new CaffeineCache(name, caffeineBuilder.build());
}
}
My Test Class
package org.vinodh.testing;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
#ConfigurationProperties(prefix = "caching")
#ActiveProfiles("test")
#Profile("test")
public class CachingConfigTest {
#Configuration
#EnableCaching
static class NestedCacheConfiguration {
static class CacheSpec {
#SuppressWarnings("unused")
private int minutesToExpire;
#SuppressWarnings("unused")
private int maximumSize;
}
private Map<String, CacheSpec> specs;
public Map<String, CacheSpec> getSpecs() {
return specs;
}
#Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
System.out.println(getSpecs()); // Is null
return cacheManager;
}
}
#Test
public void test() {
System.out.println("Inside Test");
}
}
application-test.properties
caching.specs.test.minutesToExpire=10
caching.specs.test.maximumSize=10

Parameter 0 of constructor in Thomas.ChapterController required a bean of type 'Thomas.ChapterRepository' that could not be found

I'm new to spring boot and I can't get an example from my spring boot book to work. Here is the code
Description:
Parameter 0 of constructor in Thomas.ChapterController required a bean of >type 'Thomas.ChapterRepository' that could not be found.
Action:
Consider defining a bean of type 'Thomas.ChapterRepository' in your configuration.
Chapter.java
package Thomas;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Data
#Document
public class Chapter {
#Id /*tells mongodb that this will be the primary key for Mongo Document */
private String Id;
private String name;
public Chapter(String name) {
this.name = name;
}
}
ChapterRepository.java
package Thomas;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String> {
}
LoadDatabase.Java
package Thomas;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import reactor.core.publisher.Flux;
import org.springframework.context.annotation.Configuration;
#Configuration /* Marks this class as a source of beans */
public class LoadDatabase {
#Bean /* Indicates that the return value of init is a Spring Bean */
CommandLineRunner init(ChapterRepository repository) {
return args -> {
Flux.just (
new Chapter("Quick Start With Java"),
new Chapter("Reactive Web With Spring Boot"),
new Chapter("...and More!"))
.flatMap(repository::save)
.subscribe(System.out::println);
};
}
}
ChapterController.java
package Thomas;
import reactor.core.publisher.Flux;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class ChapterController {
private final ChapterRepository repository;
public ChapterController(ChapterRepository repository)
{
this.repository = repository;
}
#GetMapping("/chapters")
public Flux<Chapter> listing() {
return repository.findAll();
}
}
ThomasSpringApplication.java
package Thomas;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ThomasSpringApplication {
public static void main(String [] args) {
SpringApplication.run(ThomasSpringApplication.class, args);
}
}
Figured out i was pulling in the wrong dependencies in my pom.xml

Extend class level RequestMapping with custom annotation

I would like to create a custom annotation in my Spring Boot application which always adds a prefix to my class level RequestMapping path.
My Controller:
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;
#MyApi("/users")
public class UserController {
#GetMapping("/stackoverflow")
public String get() {
return "Best users";
}
}
My custom annotation
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#RestController
#RequestMapping(path = "/api")
public #interface MyApi {
#AliasFor(annotation = RequestMapping.class)
String value();
}
GOAL: a mapping like this in the end: /api/users/stackoverflow
Notes:
server.servlet.context-path is not an option because I want to create
several of these
I'm using Spring Boot version 2.0.4
I was not able to find an elegant solution for the issue. However, this worked:
Slightly modified annotation, because altering behavior of value turned out to be more difficult.
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#RestController
#RequestMapping
public #interface MyApi {
#AliasFor(annotation = RequestMapping.class, attribute = "path")
String apiPath();
}
Bean Annotation Processor
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
#Component
public class MyApiProcessor implements BeanPostProcessor {
private static final String ANNOTATIONS = "annotations";
private static final String ANNOTATION_DATA = "annotationData";
public Object postProcessBeforeInitialization(#NonNull final Object bean, String beanName) throws BeansException {
MyApi myApi = bean.getClass().getAnnotation(MyApi.class);
if (myApi != null) {
MyApi alteredMyApi = new MyApi() {
#Override
public Class<? extends Annotation> annotationType() {
return MyApi.class;
}
#Override
public String apiPath() {
return "/api" + myApi.apiPath();
}
};
alterAnnotationOn(bean.getClass(), MyApi.class, alteredMyApi);
}
return bean;
}
#Override
public Object postProcessAfterInitialization(#NonNull Object bean, String beanName) throws BeansException {
return bean;
}
#SuppressWarnings("unchecked")
private static void alterAnnotationOn(Class clazzToLookFor, Class<? extends Annotation> annotationToAlter, Annotation annotationValue) {
try {
// In JDK8 Class has a private method called annotationData().
// We first need to invoke it to obtain a reference to AnnotationData class which is a private class
Method method = Class.class.getDeclaredMethod(ANNOTATION_DATA, null);
method.setAccessible(true);
// Since AnnotationData is a private class we cannot create a direct reference to it. We will have to manage with just Object
Object annotationData = method.invoke(clazzToLookFor);
// We now look for the map called "annotations" within AnnotationData object.
Field annotations = annotationData.getClass().getDeclaredField(ANNOTATIONS);
annotations.setAccessible(true);
Map<Class<? extends Annotation>, Annotation> map = (Map<Class<? extends Annotation>, Annotation>) annotations.get(annotationData);
map.put(annotationToAlter, annotationValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Controller:
import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;
#MyApi(apiPath = "/users")
public class UserController {
#GetMapping("/stackoverflow")
public String get() {
return "Best users";
}
}

Spring StandardMultipartHttpServletRequest validation

Is there any possibility to validate StandardMultipartHttpServletRequest using standard #Valid annotation and custom Validator?
I've implemented such validator, annotated method param in controller the validator is not invoked.
I've figured it out myself. To make it work you need a DTO:
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
#Getter
#Setter
public class NewOrderFilesDTO {
List<MultipartFile> files;
}
Then, a validator:
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static org.springframework.util.CollectionUtils.isEmpty;
#Component
public class NewOrderFilesValidator implements Validator {
private static final String MIME_TYPE_PDF = "application/pdf";
private static final long ALLOWED_SIZE = 3 * 1024 * 1024;
#Override
public void validate(Object target, Errors errors) {
if (target == null) {
return;
}
NewOrderFilesDTO newOrderFilesDTO = (NewOrderFilesDTO) target;
List<MultipartFile> newOrderFiles = newOrderFilesDTO.getFiles();
if (isEmpty(newOrderFiles)) {
return;
}
for (MultipartFile file : newOrderFiles) {
if (!MIME_TYPE_PDF.equals(file.getContentType())) {
errors.rejectValue(file.getName(), file.getName(), "'application/pdf' files allowed only!");
}
if (file.getSize() > ALLOWED_SIZE) {
errors.rejectValue(file.getName(), file.getName(), "File size allowed up to 3MB!");
}
}
}
#Override
public boolean supports(Class<?> cls) {
return NewOrderFilesDTO.class.equals(cls);
}
}
And finally a controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.validation.Valid;
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
#Controller
class OrderController {
private final NewOrderFilesValidator newOrderFilesValidator;
#Autowired
OrderController(NewOrderFilesValidator newOrderFilesValidator) {
this.newOrderFilesValidator = newOrderFilesValidator;
}
#InitBinder("newOrderFiles")
void initOrderFilesBinder(WebDataBinder binder) {
binder.addValidators(newOrderFilesValidator);
}
#ResponseStatus(NO_CONTENT)
#RequestMapping(value = ORDERS_PATH, method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
void createOrder(
#Valid #ModelAttribute NewOrderFilesDTO newOrderFiles
) {
}
}
With the configuration above the DTO will be validated automatically by spring.

Categories