【发布时间】:2014-03-19 15:26:46
【问题描述】:
我有一个相当幼稚的问题。我们可以像使用 Spring 框架注入一样使用核心 java 注入依赖项吗?
现在,我做这样的事情:
在 web.xml 中:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring applicationcontext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="mybean" class="com.test.app.MyService" />
</beans>
我将使用注入 bean 的类:
public class MyResource {
@Autowired
private MyService mybean;
public MyResponse doService(MyRequest req) {
mybean.doBusiness(req);
}
}
}
那么,有没有一种方法可以使用核心 java 进行这种依赖注入?我一直在阅读有关 CDI 的内容,但不太了解。另外,它也感觉它不是 Spring 所做的直接替代。
如果我错了,请帮助并纠正我。
【问题讨论】:
-
“核心Java”是什么意思?这不是一个普遍认可的术语。
-
我改了标题。是不是更合适?
-
这样比较容易理解。
标签: java spring dependency-injection