javax inect
Spring 3 and JSR-330 @Inject and @Named example
By mkyong | September 16, 2012 | Viewed : 86,399 times
Since Spring 3.0, Spring supports for the standard JSR 330: Dependency Injection for Java. In Spring 3 application, you can uses standard
@Injectinstead of Spring’s@Autowiredto inject a bean.@Namedinstead of Spring’s@Componentto declare a bean.
Those JSR-330 standard annotations are scanned and retrieved the same way as Spring annotations, the integration just happened automatically, as long as the following jar in your classpath.
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
1. Spring Annotations
Let see a normal Spring’s annotation example – @Autowired and @Component
P.S @Component, @Repository and @Service are same, just declares a bean in Spring Ioc context.
package com.mkyong.customer.dao;
import org.springframework.stereotype.Repository;
@Repository
public class CustomerDAO
{
public void save() {
System.out.println("CustomerDAO save method...");
}
}
package com.mkyong.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mkyong.customer.dao.CustomerDAO;
@Service
public class CustomerService
{
@Autowired
CustomerDAO customerDAO;
public void save() {
System.out.println("CustomerService save method...");
customerDAO.save();
}
}
2. JSR-330 Annotations
Basically, it works the same, just with different annotations – @Inject and @Named.
package com.mkyong.customer.dao;
import javax.inject.Named;
@Named
public class CustomerDAO
{
public void save() {
System.out.println("CustomerDAO save method...");
}
}
package com.mkyong.customer.services;
import javax.inject.Inject;
import javax.inject.Named;
import com.mkyong.customer.dao.CustomerDAO;
@Named
public class CustomerService
{
@Inject
CustomerDAO customerDAO;
public void save() {
System.out.println("CustomerService save method...");
customerDAO.save();
}
}
3. Run it
Both Spring and JSR330 annotations need component scan to works.
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.mkyong.customer" />
</beans>
package com.mkyong;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.customer.services.CustomerService;
public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});
CustomerService cust = (CustomerService)context.getBean("customerService");
cust.save();
}
}
Above two examples are generated the same output
CustomerService save method...
CustomerDAO save method...
4. JSR-330 Limitations
There are some limitations on JSR-330 if compare to Spring :
@Injecthas no “required” attribute to make sure the bean is injected successful.- In Spring container, JSR-330 has scope singleton by default, but you can use Spring’s
@Scopeto define others. - No equivalent to Spring’s
@Value,@Requiredor@Lazy.
Check out this Spring references.
5. Go for JSR-330
In fact, Spring’s annotations are more powerful, but only available on Spring framework. The JSR-330 is a standard spec, and it’s supported on all J2ee environment that follow the JSR-330 spec.
For new or migration project, it’s always recommended to use JSR-330 annotations, and remember, it works on Spring 3 as well.
Download Source Code
References
javax inect的更多相关文章
- The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files
看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...
- 【原】tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决
现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...
- 使用javax.servlet.http.Part类上传文件
使用的是Servlet 3.0 新的特征标注(Annotaion)类描述部署,一些低版本的服务器需要使用标准依赖部署描述文件(web.xml)来部署,另外Part也是Java EE 6.0新增的类,P ...
- javax.mail 发送邮件异常
一.运行过程抛出异常 1.Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/ ...
- javax.net.ssl.SSLHandshakeException(Cas导入证书)
一.报错: javax.net.ssl.SSLHandshakeException二.原因分析:CAS部署时,常常要涉及到HTTPS的证书发布问题.由于在实验环境中,CAS和应用服务常常是共用一台PC ...
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path解决方案
0.环境: win7系统,Tomcat9配置无误. 1.错误: 项目中某一.jps页面忽然出现错误,鼠标点上去为:The superclass "javax.servlet.http.Htt ...
- javax.crypto.BadPaddingException: Given final block not properly padded 解决方法
下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误: javax.crypto.BadPaddingException: Given final block not properly p ...
- javax.el.PropertyNotFoundException 出错
之所以是把他记下来,是因为这个低级错误 害的我找了老半天. 后台传了对象到页面,在页面中循环遍历获得对象某个属性值 如下: <c:forEach items="${resultMap. ...
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path。问题
JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.HttpServlet" was not found on the Ja ...
随机推荐
- UIView属性clipsTobounds的应用
view添加view,并剪边(UIView属性clipsTobounds的应用) 如题,有两个view: view1,view2 view1添加view2到其中,如果view2大于view1,或者vi ...
- UIImage图片处理
#pragma mark - #pragma mark - 缩放处理 + (UIImage *)scaleImage:(UIImage *)image withScale:(float)scale { ...
- 利用jquery来隐藏input type="file"
<li> <input type="text" name="token" value = "<?php ech$_SESSIO ...
- (转)实例详解CSS中position的fixed属性使用
关于fixed属性,在什么情况下需要用,怎么用,首先,我们应该先了解下fixed属性的说明:fixed总是以body为定位时的对象,总是根据浏览器的窗口来进行元素的定位,通过"left&qu ...
- Android--Toast时间
/** * * 显示toast,自己定义显示长短. * param1:activity 传入context * param2:word 我们需要显示的toast的内容 * param3:time le ...
- Android向Rest服务Post数据遇到的Date类型数据问题
今天在Android端向Rest服务Post数据时,总是不成功,查了很多资料,才知道Rest端将json串反序列化时,需要的时间格式必须是UTC类型,及Date(12345678+0800)格式. A ...
- nginx配置文件(反向代理+集群+动静分离)
1.nginx纯反向代理配置(nginx.conf): #user nobody;worker_processes 4;error_log logs/error.log info;pid logs/n ...
- 两个string数组对应比较
最近做的array string类型对比.这个可能比较复杂,用的是linq 是请教别人的,我在这里记录一下 jquery 方法里面的数组 function arrtxt() { var arrt= [ ...
- dede调用指定栏目的标签
{dede:type typeid='1'} <a href="[field:typelink /]">[field:typename /]</a> {/d ...
- 函数stripslashes去除转义 shopnc 搜索框过滤特殊字符 输入单斜杆会自动转义
如何php是如何处理和过滤特殊字符的呢? 搜索%_显示所有商品:搜索\会在搜索框内叠加\\ 查了一下 magic_quotes_sybase 项开启,反斜线将被去除,但是两个反斜线将会被替换成一个. ...