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. Qt快速入门系列教程目录

    Qt快速入门系列教程目录

  2. .net DataTable 正确排序姿势

    关于dataTable中根据列排序正确姿势做个随笔,方便查阅 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Ad ...

  3. linux基础内容学习一:linux下的分区及安装

    linux看系统版本信息 uname -a 如果显示为i386,i686则为32位系统,如果为x86_64则为64位 一块硬盘最多可以有四个主分区其中一个主分区可以用一个扩展分区替换,在这个扩展分区中 ...

  4. React-Native OpenGL体验一

    昨天初体验了一把SVG一个并不是多么复杂的动画,我在iOS模拟器上体验的是流畅的,但是在Android真机上体验,还是比较卡的. 下面来介绍一个OpenGL的第三方库: 下面是我运行的里面Demo的效 ...

  5. lucene.net 3.0.3、结合盘古分词进行搜索的小例子(转)

    lucene.net 3.0.3.结合盘古分词进行搜索的小例子(分页功能)   添加:2013-12-25 更新:2013-12-26 新增分页功能. 更新:2013-12-27 新增按分类查询功能, ...

  6. 1228.1——计算器(未使用MVC设计模式)

    #import "ViewController.h"typedef enum{    kStausNum,    kStausOperation}kStaus; typedef e ...

  7. 【.Net Remoting-1】

    [.NetRemoting]2015.09.16 [分布式应用程序] 应用程序分布在不同计算机上,通过网络来共同完成一项任务 C/S架构[模式] [互操作性,Interoperability]又称[互 ...

  8. (转) 学习C++ -> 指针初步

    学习C++ -> 指针初步 一.指针    1. 什么是指针?        我们知道, 计算机的内存是由一个个独立的存储单元组成, 并且系统会对每一个存储单元分配一个唯一的号码, 称为这个存储 ...

  9. Hibernate中HQL的日期差值计算,可计算相差多少秒

    最近有个业务需求就是计算订单创建时间离现在超过 4 小时的订单都查找出来! 那么就需要用到日期函数了. 网上找了一下总共的日期函数有一下几个: CURRENT_DATE() 返回数据库当前日期 时间函 ...

  10. css布局学习笔记之max-width

    设置块级元素的 width 可以阻止它从左到右撑满容器.然后你就可以设置左右外边距为 auto 来使其水平居中.元素会占据你所指定的宽度,然后剩余的宽度会一分为二成为左右外边距. div{ width ...