javax inect
Spring 3 and JSR-330 @Inject and @Named example
Since Spring 3.0, Spring supports for the standard JSR 330: Dependency Injection for Java. In Spring 3 application, you can uses standard
@Inject
instead of Spring’s@Autowired
to inject a bean.@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.
<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 :
@Inject
has 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
@Scope
to define others. - 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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~