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. 解决使用Touch ID API在回调时界面“长时间卡住”的问题

    Touch ID是iOS8上新公开的API,关于详细介绍和用法可以看CocoaChina的这两篇文章:上 和 下,在此篇文章中不再赘述. 我在app中需要的效果是如果touch id验证通过,则页面p ...

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

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

  3. String类中几个简单的常用方法

    这里我们就把 info 这个字符串 通过 “ ” 这个分隔符 分割成几部分 并吧没部分添加到 s 数组里面 注意:只有字符串才能分割 分隔符必须是 char 类型 而且是 字符串 里面存在的, 例如我 ...

  4. SQL Server 模式和名称解析

    模式实际上是名称空间,因此在SQL Serve中调用数据库对象时,一定要设置对象引用的环境.每个用户都被赋予了一个默认模式,在用户登录SQL Server并凋用数据库对象时,这个默认模式就是对象引用方 ...

  5. C#总结(2)

    有输出,当然有输入.这样才会有人机交互. using System; using System.Collections.Generic; using System.Linq; using System ...

  6. JavaScript---while和do while的区别

    JavaScript,while 和do while的区别: 场景一:小盒子身上有100元,用while输出能吃多少次米线,一碗米线12元,最终还剩下多少钱. var money = 100; whi ...

  7. [poj2449]Remmarguts' Date(spfa+A*)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Remmarguts' Date Time Limit: 4000MS   Mem ...

  8. jquery之stop()的用法

    // 为了看效果,随意写的动画 $('#animater').animate({ 'right':-800 }, 3000).animate({'font-size':'16px'},'normal' ...

  9. pymssql文档

    原文地址 http://pymssql.org/en/latest/ref/_mssql.html _mssql module reference pymssql模块类,方法和属性的完整文档. Com ...

  10. 伪静态规则写法RewriteRule-htaccess详细语法使用

    一.正则表达式教程伪静态规则写法RewriteRule-htaccess详细语法使用教程分享简单说下:伪静态实际上是利用PHP把当前地址解析成另外一种方法进行访问网站!要学伪静态规则的写法,你必须得懂 ...