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 ...
随机推荐
- 成语接龙(dfs)
成语接龙 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 92(17 users) Total Accepted: 23(14 user ...
- hadoop备战:一台x86计算机搭建hadoop的全分布式集群
主要的软硬件配置: x86台式机,window7 64位系统 vb虚拟机(x86的台式机至少是4G内存,才干开3台虚机) centos6.4操作系统 hadoop-1.1.2.tar.gz jdk- ...
- IOS 怎么修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色
self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; self.navigationControll ...
- Linux curses库使用
相信您在网路上一定用过如 tin,elm 等工具, 这些软体有项共同的特色, 即他们能利用上下左右等方向键来控制游标的位置. 除此之外, 这些程式的画面也较为美观. 对Programming 有兴趣 ...
- C/C++笔试准备(1)
题目:用递归的算法实现这样一个函数,计算一个字符串最大连续相同字符数,输入aaabbc,输出3:输入bbc,输出2 #include <iostream> using namespace ...
- 怎么修改placeholder字体的css样式
修改palceholder内文字的css样式 ::-webkit-input-placeholder{ color: red; font-size: 20px; line-height: 50px; ...
- JS如何判断IE和火狐与Chrome浏览器
var isIE=navigator.userAgent.toUpperCase().indexOf("MSIE")?true:false; 类似的可以写var isFirefox ...
- box-shadow实例1
实例一: 1.html <div class="paddingMiddle"> <div class="blank"></div& ...
- iOS方法类:CGAffineTransform的使用
CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放.旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式0. ...
- UIImage加载图片的区别和渲染模式
前言 关于本地图片UIImage的加载问题,还是需要注意的.不同的加载处理方式,在效率和性能上还是有差异的. 今天,我们来讲讲UIImage的加载应该选择什么样的API来加载! 两种API 这两种AP ...