Ara*_*tta 6 java spring dependency-injection
我有一个相当天真的问题.我们可以使用核心java注入依赖关系,就像我们使用Spring框架注入一样吗?
现在,我做这样的事情:
在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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我将使用注入的bean的类:
public class MyResource {
@Autowired
private MyService mybean;
public MyResponse doService(MyRequest req) {
mybean.doBusiness(req);
}
}
Run Code Online (Sandbox Code Playgroud)
}
那么,有没有办法可以使用核心java进行这种依赖注入?我一直在读CDI,但不太懂.此外,它也感觉它不是直接替代Spring所做的事情.
如果我错了,请帮助并纠正我.
也许您想看看javax.inject。该 API 可作为单独的 jar 提供,并且存在一些依赖于并使用该 jar 的依赖项注入框架。
你的@Autowired这里将使用@Injectjavax.inject 语言;上面的两个框架允许纯Java注入(也许其他框架,不知道,因为不感兴趣)。
希望这可以帮助...
| 归档时间: |
|
| 查看次数: |
8388 次 |
| 最近记录: |