如何在spring框架中使用注解初始化java bean?

发布于 2024-12-05 03:25:34 字数 663 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抽个烟儿 2024-12-12 03:25:34

我相信这是因为您在包 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 package com.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).

倾城花音 2024-12-12 03:25:34

您需要添加

<context:annotation-config />

使用此 XML,我的自动装配工作完美无缺:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.vanilla.example"></context:component-scan>
</beans>

You need to add

<context:annotation-config />

With this XML my Autowiring works flawless:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.vanilla.example"></context:component-scan>
</beans>
伴梦长久 2024-12-12 03:25:34

您不需要同时声明两者。

使用 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

我不吻晚风 2024-12-12 03:25:34
  1. 确保“User”类是该类的包或子包
    “com.myCompany.myProject”。

  2. 您不需要包含,它是
    包含在组件扫描中。

  3. 默认情况下,该 bean 的名称为“user”,除非您
    使用@Component("myBeanName")指定bean名称

一旦完成,您可以使用以下方法将 bean 自动装配到另一个 bean 中:

@Autowired
User user;

@Inject
User user;

注释:
@Inject 是一个 javax.inject 注释,不需要注入。
@Autowired是Spring注解,需要注入。
@Autowired 可以通过以下方式之一使用:

  1. 位于
  2. 接受 User bean
  3. setter 的成员变量构造函数上方,该 setter 接受 User bean。
  1. Be sure that the "User" class is the package or sub-package of the
    "com.myCompany.myProject".

  2. You do NOT need to include <context: annotation-config/>, it is
    included with component-scan.

  3. 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:

@Autowired
User user;

OR

@Inject
User user;

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:

  1. just above the member variable
  2. constructor that accepts a User bean
  3. setter that accepts a User bean.
别在捏我脸啦 2024-12-12 03:25:34

我已经用这种方式配置了我的xml文件,这将帮助你解决问题。
对于上下文组件扫描使用

<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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
 <context:component-scan base-package="com.java.Controllers"></context:component-scan>
 <context:component-scan base-package="com.java.dao"></context:component-scan>
 <context:component-scan base-package="com.java.bean"></context:component-scan>

或者您可以使用

<context:component-scan base-package="com.java.*"></context:component-scan>

next 进行注释驱动使用

<mvc:annotation-driven />

对于资源,请使用此

<mvc:resources location="/" mapping="/**"/>

I have configured my xml file in this way , this will help you to solve the problem.
For Context-componentscan use

<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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
 <context:component-scan base-package="com.java.Controllers"></context:component-scan>
 <context:component-scan base-package="com.java.dao"></context:component-scan>
 <context:component-scan base-package="com.java.bean"></context:component-scan>

Or else u can use

<context:component-scan base-package="com.java.*"></context:component-scan>

next for annotation driven use

<mvc:annotation-driven />

For resources use this

<mvc:resources location="/" mapping="/**"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文