Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以将依赖项注入到需要对象数据的对象中吗?_Java_Spring_Ioc Container - Fatal编程技术网

Java 我可以将依赖项注入到需要对象数据的对象中吗?

Java 我可以将依赖项注入到需要对象数据的对象中吗?,java,spring,ioc-container,Java,Spring,Ioc Container,我有一个用户工厂,它只从UserStore获取用户: public class UserFactory { @Autowired private UserStore userStore; public User getUserFromId(String Id) { User foundUser = this.userStore.findUser(Id); return foundUser; } } 这是我的用户对象:

我有一个用户工厂,它只从UserStore获取用户:

public class UserFactory
{
    @Autowired
    private UserStore userStore;

    public User getUserFromId(String Id)
    {
        User foundUser = this.userStore.findUser(Id);

        return foundUser;
    }
}
这是我的用户对象:

public class User implements Serializable
{
    @Autowired
    private List<Announcer> announcers;  //I want this injected

    private String twitterId;

    private String facebookId;

    public List<Announcer> getAnnouncers()
    {
        //lazy init?
        return this.announcers;
    }

}
公共类用户实现可序列化
{
@自动连线
私人名单播音员;//我要这个
私有字符串twitterId;
私有字符串facebookId;
公共广播员名单
{
//懒惰初始化?
把这个还给广播员;
}
}
announcers
中的每个实例都依赖于相应的服务ID,例如
twitterId
facebook ID
。。。我可以在getter中懒洋洋地构建它,但我很好奇是否有办法建立一个工厂来注入它。我还可以公开一个setter,并在UserFactory中设置它。。。但是试图限制曝光。

看看这个链接:

请参阅名为“注入Bean引用”的部分

我不知道有没有一种方法可以在没有xml配置的情况下执行此操作,在这些情况下,我所做的是在xml文件的列表中定义要注入的ITEN,然后将bean注入列表

比如:

 <bean id="an1" class="FBAnnouncer"/>
 <bean id="an2" class="TWAnnouncer"/>
 <bean id="user" class="User">
     <property name="announcers">
        <list>
           <ref bean="an1"/>
           <ref bean="an2"/>
        </list>
      </property>
 </bean>


这难道不会给每个用户提供相同的播音员吗?我需要根据每个用户注册的服务为他们提供自己的播音员您有要注入的用户列表吗?还是从数据库中获取?如果有用户列表和每个用户的注释列表,则可以使用此解决方案。您可以在用户工厂中注入用户列表,注入方式与将注释器注入用户相同。