2018-11-05 Addendum
In PHP's Laravel, getting an object globally via DI resolution from any class didn't cause any problems (which didn't happen to my knowledge), but in Spring it involves an annoying element of context, so Laravel It seems that it is not possible to have a DI solution with Hoi Hoi with a glue like this.
As usual, this is a story in a state where Spring Boot is not used, so it may not be the case when Spring Boot is introduced, but at this stage we have not been able to investigate that much.
Assigned to a Web system project using Java's Spring MVC. Spring Boot is not used because it seems to be a system made a little while ago. I'm still new to Java web systems, so it's very difficult to do.
Spring MVC seems to use annotations like "@Inject" for DI. Recently, I learned a little about domain-driven development, so I tried to make a simple domain object related to the repair part by thinking about making a domain object (an object that bears an easy-to-understand unit of work).
However, I learned that DI using annotations cannot be used in a class that does not follow the rules of Spring MVC, that is, in a self-made class. As a result of searching for an alternative method, I found it as it is, so I will briefly summarize it here.
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
(Omission)
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
ServletContext servletContext = httpServletRequest.getServletContext();
WebApplicationContext webApplicationContext =
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//Get instance by bean definition name
//Since it returns as Object type, cast it to the desired class
Target type variable name= (Cast to the desired mold) webApplicationContext.getBean("Bean definition name");
//Get bean definition name list getBean()You can check the character string that can be used as an argument of.
String[] beanDefinitionNames = webApplicationContext.getBeanDefinitionNames();
WebApplicationContextUtils.getRequiredWebApplicationContext (ServletContext instance)
, and "WebApplicationContext" can be obtained.getServletContext ()
.((ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()). getRequest ()
.can be obtained by
WebApplicationContext.getBeanDefinitionNames () `.package Specify the package name;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
*Domain object
*DI (DependencyInjectionSolver) solver
*In a self-made class not managed by Spring@DI using annotations such as Inject does not work, so
*Get the DI resolved instance via this class.
*
*When testing the process that uses this class, JUnit's Test class says "@Annotate "WebAppConfiguration".
*/
public class DependencyInjectionSolver {
private static final Logger logger = LoggerFactory.getLogger(DependencyInjectionSolver.class);
/**
*ApplicationContext for getting beans through this instance
*/
private WebApplicationContext webApplicationContext;
public DependencyInjectionSolver() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
ServletContext servletContext = httpServletRequest.getServletContext();
this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}
/**
*Receive the bean definition name and get the DI resolved instance.
*Since it returns as Object type, cast it on the acquisition side.
*
* @param string Bean definition name
* @return Object
*/
public Object getBeanByBeanName(String beanName) {
return this.webApplicationContext.getBean(beanName);
}
/**
*Get a list of defined Bean definition names.
*GetBeanByBeanName for the names in this list()It can be used as an argument of.
*
* @return List<string>List of bean definition names
*/
public List<String> gettableBeanNames() {
String[] beanDefinitionNames = this.webApplicationContext.getBeanDefinitionNames();
return Arrays.asList(beanDefinitionNames);
}
}
User side
DependencyInjectionSolver diSolver = new DependencyInjectionSolver();
Variable name such as interface name= (Interface name etc.) diSolver.getBeanByBeanName("Bean definition name");
Recommended Posts