如何在spring框架中使用注解初始化java bean?
我有一个 Java bean:
public class User{
private Integer userid;
private String username;
private String password;
private boolean enable;
//getter and setter
}
我可以通过以下方式将其初始化为 context.xml 中的 spring bean:
<context:component-scan base-package="com.myCompany.myProject" />
但我不想在 xml 中初始化它。如何使用 sring 3 注释初始化它。我尝试了以下方法:
@Component
public class User{
private Integer userid;
private String username;
private String password;
private boolean enable;
//getter and setter
}
但上述方法对我不起作用。有什么想法吗?
I have a Java bean:
public class User{
private Integer userid;
private String username;
private String password;
private boolean enable;
//getter and setter
}
I am able to initialize it as a spring bean at context.xml via:
<context:component-scan base-package="com.myCompany.myProject" />
But I don't want initialize it in xml. How can I initialize it with sring 3 annotation. I tried with the following:
@Component
public class User{
private Integer userid;
private String username;
private String password;
private boolean enable;
//getter and setter
}
But the above did not work for me. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信这是因为您在包
com.myCompany.myProject
上启用了组件扫描,而不是在包com.myCompany.myProject.db
上启用组件扫描 将扫描定义更改为此:
(或者添加一个新的,如果您还需要其他包中的类)并且您可以从中删除 bean 定义XML 并让您的注释为您服务。虽然很愚蠢,但还是要确保
@Component
注释是 Spring 的注释。我有时会遇到这个愚蠢的问题,定义一个实际上不是来自所需库的注释(由于注释的名称相同,由我的类路径中的不同库)。I believe that is because you have enable component scan on the package
com.myCompany.myProject
and not on the packagecom.myCompany.myProject.db
Change your scan definition to this :
<context:component-scan base-package="com.myCompany.myProject.db" />
(or add a new one, if you want classes from the other package as well) and you can remove the bean definition from XML and have your annotation work for you.Silly, but still, ensure that the
@Component
annotation is that of Spring's. I've at times faced this silly issue of defining an annotation that is actually not from the desired library (owing to the same name of the annotation, by different libraries in my classpath).您需要添加
使用此 XML,我的自动装配工作完美无缺:
You need to add
With this XML my Autowiring works flawless:
您不需要同时声明两者。
使用 context:annotation-config 允许通过注释等自动装配 beans。
使用 context:component-scan 提供 context:annotation-config 的所有内容,但允许自动发现 beans。您在 context:component-scan 中提供的包将扫描该包以及所有子包。
希望这有帮助
you shouldn't need to have both declared.
Use of context:annotation-config allows for autowiring of beans via annotations etc
Use of context:component-scan provides everything that context:annotation-config, but allows for autodiscovery of beans. The package you supply in context:component-scan will scan that package plus all sub packages.
Hope this helps
确保“User”类是该类的包或子包
“com.myCompany.myProject”。
您不需要包含
,它是包含在组件扫描中。
默认情况下,该 bean 的名称为“user”,除非您
使用@Component("myBeanName")指定bean名称
一旦完成,您可以使用以下方法将 bean 自动装配到另一个 bean 中:
或
注释:
@Inject 是一个 javax.inject 注释,不需要注入。
@Autowired是Spring注解,需要注入。
@Autowired 可以通过以下方式之一使用:
Be sure that the "User" class is the package or sub-package of the
"com.myCompany.myProject".
You do NOT need to include
<context: annotation-config/>
, it isincluded with component-scan.
The bean is available with the name "user" by default, unless you
specify the bean name with @Component("myBeanName")
Once that is done, you can autowire the bean into another with:
OR
NOTES:
@Inject is a javax.inject annotation the injection is NOT required.
@Autowired is a Spring annotation and the injection is required.
@Autowired can be used in one of the following ways:
我已经用这种方式配置了我的xml文件,这将帮助你解决问题。
对于上下文组件扫描使用
或者您可以使用
next 进行注释驱动使用
对于资源,请使用此
I have configured my xml file in this way , this will help you to solve the problem.
For Context-componentscan use
Or else u can use
next for annotation driven use
For resources use this