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

  1. @Inject instead of Spring’s @Autowired to inject a bean.
  2. @Named instead of Spring’s @Component to 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.

pom.xml
Markup
	<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.

CustomerDAO.java
Java
package com.mkyong.customer.dao;

import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAO
{
public void save() {
System.out.println("CustomerDAO save method...");
}
}
CustomerService.java
Java
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.

CustomerDAO.java
Java
package com.mkyong.customer.dao;

import javax.inject.Named;

@Named
public class CustomerDAO
{ public void save() {
System.out.println("CustomerDAO save method...");
}
}
CustomerService.java
Java
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.

Spring-AutoScan.xml – Scan bean automatically
Markup
<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>
App.java – Run it
Java
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

Bash
CustomerService save method...
CustomerDAO save method...

4. JSR-330 Limitations

There are some limitations on JSR-330 if compare to Spring :

  1. @Inject has no “required” attribute to make sure the bean is injected successful.
  2. In Spring container, JSR-330 has scope singleton by default, but you can use Spring’s @Scope to define others.
  3. No equivalent to Spring’s @Value@Required or @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

Download it – Spring-JSR330-Example.zip (27kb)

References

javax inect的更多相关文章

  1. 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 ...

  2. 【原】tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig的解决

    现象: tomcat 7 启动报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig() ...

  3. 使用javax.servlet.http.Part类上传文件

    使用的是Servlet 3.0 新的特征标注(Annotaion)类描述部署,一些低版本的服务器需要使用标准依赖部署描述文件(web.xml)来部署,另外Part也是Java EE 6.0新增的类,P ...

  4. javax.mail 发送邮件异常

    一.运行过程抛出异常 1.Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/ ...

  5. javax.net.ssl.SSLHandshakeException(Cas导入证书)

    一.报错: javax.net.ssl.SSLHandshakeException二.原因分析:CAS部署时,常常要涉及到HTTPS的证书发布问题.由于在实验环境中,CAS和应用服务常常是共用一台PC ...

  6. 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 ...

  7. javax.crypto.BadPaddingException: Given final block not properly padded 解决方法

    下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误: javax.crypto.BadPaddingException: Given final block not properly p ...

  8. javax.el.PropertyNotFoundException 出错

    之所以是把他记下来,是因为这个低级错误 害的我找了老半天. 后台传了对象到页面,在页面中循环遍历获得对象某个属性值 如下: <c:forEach items="${resultMap. ...

  9. 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 ...

随机推荐

  1. [J2EE学习][post,get乱码处理]

    post乱码 通过flitter过滤(原理待未来好好学习) <!-- post乱码过虑器 --> <filter> <filter-name>CharacterEn ...

  2. Andriod布局之LinearLayout

    LinearLayout是安卓中的常见布局,即线性布局.(提示:在Andriod中要常用alt+/快捷键来补全代码 其中有一个重要的属性android:orientation,它是表示线性布局的方向问 ...

  3. HTTP协议之 简易浏览器(3)--转载

    简单的说,今天的全部工作就是 我的目的只有两个 1.加深对http协议的理解   2.深化对B/S结构的认识. 代码 1 /* 2 这个程序把主机地址写死了, 3 想更像的话,可以在加个输入.然后根据 ...

  4. appStore应用发布流程

    原文转自: http://blog.sina.com.cn/s/blog_68661bd801019uzd.html       首先确定帐号是否能发布, https://developer.appl ...

  5. POJ 1723 SOLDIERS (中位数)

    题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...

  6. QT5.5实现串口通信

    QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成 #include <QtSerialPort/QSerialPort> #include <QtSerial ...

  7. (转) Friendship and inheritance

    原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...

  8. Python codes

    Vertification of an assignment from Stochastic Processing. Using Brute Force and "itertools&quo ...

  9. [转载]OpenSUSE 13.2/13.1 安装搜狗拼音输入法

    1. 添加 M17N 源: 13.2: sudo zypper ar -f http://download.opensuse.org/repositories/M17N/openSUSE_13.2/ ...

  10. 转:Linux中find命令-path -prune用法详解

    在Windows中可以在某些路径中查找文件,也可以设定不在某些路径中查找文件,下面用Linux中的find的命令结合其-path -prune参数来看看在Linux中怎么实现此功能. 假如在当前目录下 ...