已经发布了几个关于依赖注入的具体问题的问题,例如何时使用它以及它有哪些框架.然而,
什么是依赖注入以及何时/为什么应该或不应该使用它?
language-agnostic design-patterns dependency-injection terminology
我正在阅读spring 3.0.x参考文档以了解Spring Autowired注释:
我无法理解下面的例子.我们是否需要在XML中执行某些操作才能使用它?
例1
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
例2
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
如何通过自动装配两个类来实现相同的接口并使用相同的类?
例:
class Red implements Color
class Blue implements Color
class myMainClass{
@Autowired
private Color color;
draw(){
color.design();
}
}
Run Code Online (Sandbox Code Playgroud)
将调用哪种设计方法?如何确保调用Red类的设计方法而不是Blue?
使用过滤器(DelegatingFilterProxy)安装SpringBoot Legacy(web.xml)WAR应用程序时,我收到以下错误/ stacktrace:
java.lang.IllegalStateException:org.springframework.boot.legacy.context.web.AnnotationConfigNonEmbeddedWebApplicationContext@fff88888尚未刷新
[ERROR] 2016-01-11 11:11:11,265 org.springframework.web.servlet.DispatcherServlet - Context initialization failed
java.lang.IllegalStateException: org.springframework.boot.legacy.context.web.AnnotationConfigNonEmbeddedWebApplicationContext@fff88888 has not been refreshed yet
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1041)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1059)
at org.springframework.web.servlet.DispatcherServlet.initMultipartResolver(DispatcherServlet.java:497)
at org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:479)
at org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:471)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:559)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:161)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.init(ServletWrapper.java:342)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.init(ServletWrapperImpl.java:168)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.load(ServletWrapper.java:1375)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1024)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3815)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:981)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:456)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:518)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:309)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:280)
at com.ibm.ws.ssl.channel.impl.SSLConnectionLink.determineNextChannel(SSLConnectionLink.java:1049)
at com.ibm.ws.ssl.channel.impl.SSLConnectionLink$MyReadCompletedCallback.complete(SSLConnectionLink.java:643)
at com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1818)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204) …Run Code Online (Sandbox Code Playgroud) java ×2
autowired ×1
spring ×1
spring-3 ×1
spring-boot ×1
terminology ×1
websphere ×1
websphere-8 ×1