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 ...
随机推荐
- boost::asio 的同、异步方式
转自:http://blog.csdn.net/zhuky/archive/2010/03/10/5364574.aspx Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C ...
- Git 推送分支
1.推送本地分支到远程: git push origin master 推送本地分支 master 到 远程的 origin 上面 git push origin test 将本地分支test推 ...
- EffectiveC#1--尽可能的使用属性(property),而不是数据成员(field)
1.属性可以进行数据绑定 2.可以做数据安全校验.在对数据检测时,如果发现数据不满足条件,最好以抛出异常的形式来解决 如下代码不可取 public string Name { get { if(thi ...
- 如何利用 Bootstrap 进行快速 Web 开发
原文出处: IBM developerworks 了解如何使用 Bootstrap 快速开发网站和 Web 应用程序(包括移动友好型应用程序).Bootstrap 以 LESS 项目为基础,由 Twi ...
- ASP.NET的Application简介1
ASP.NET中的Application 1. Application是用于保存所有用户共有的信息.在ASP时代,如果要保存的数据在应用程序生存期内不会或者很少改变,那么使用Application是理 ...
- ckplayer,超酷网页播放器,用于集成在网站中的播放器
自己在工作中做了一个教学网站,点击左边课程,右边播放视频,经过源代码分析,用的就是这个播放器 网址:http://www.ckplayer.com/ 具体使用播放器网站上说的比较明白 div id=& ...
- json的js和C#操作
C#端的WebService接口接收json格式数据,处理后以json格式返回result using System; using System.Collections.Generic; using ...
- Oracle相关的知识点
1. 如何在Oracle SQLPlus中执行SQL Script文件 以下面的格式在提示符中输入@{file name} SQL>@{file} 假设你要运行的文件的名字是script.sql ...
- Mybatis Generator最完整配置详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- C++学习笔录4
1.容器=数据结构+算法.相当于是为复杂的数据设计一种专门用于存放该数据的东西.用于开发中传递复杂的数据. 2.模版函数只能写在头文件中.不能单独做声明. 3.STL容器类分为三类: (1).顺序容器 ...