Spring 如何在不创建一个bean id的情况下访问同一个类实例

Spring 如何在不创建一个bean id的情况下访问同一个类实例,spring,inversion-of-control,Spring,Inversion Of Control,我是Spring的新手,我目前参与的项目正在使用Spring IOC。 这是我的应用程序的当前设置,我试图将其表示为独立的 下面的这个类是在服务器启动期间作为进程启动的 <bean id="streamingthread" class="com.StreamingThread" scope="singleton" > </bean> </beans> **This is StreamingThread** p

我是Spring的新手,我目前参与的项目正在使用Spring IOC。 这是我的应用程序的当前设置,我试图将其表示为独立的 下面的这个类是在服务器启动期间作为进程启动的

  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }
Client.java

public final class Client {
    public static void main(String[] args) {
        try {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
            String[] beans = ctx.getBeanDefinitionNames();
            for (String string : beans) {
                System.out.println(beans);
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

context.xml





      <!-- <context:component-scan base-package="com.tradeking" /> -->
       <context:annotation-config />



    <bean id="stream-core" class="com.StreamHandler" scope="singleton" init-method="init">
          <constructor-arg>
             <ref bean="streamingthread"/>
          </constructor-arg>
           </bean>

          <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
           </bean>

         </beans>
  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }
StreamingThread.java

   package com;
    import java.util.HashSet;
    import java.util.Set;
    public class StreamingThread extends Thread {
        private Set<String> streamSet = new HashSet();
        public void run() {
            while (true) {
                for (int i = 0; i < 12; i++) {
                    streamSet.add("Test" + 1);
                    System.out.println("Run Called");
                }
            }
        }
    }
  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }
这是我的context.xml

  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }

  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }

  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }

**这是StreamingThread**
包装组件;
导入java.util.Set;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.context.ApplicationContext;
导入org.springframework.context.support.ClassPathXmlApplicationContext;
公共类客户端{
@自动连线
@限定符(“streamingthread”)
私有StreamingThread StreamingThread;
公开募捐
{
while(true)
{
}
}
公共静态void main(字符串[]args){
试一试{
ApplicationContext ctx=新类路径XmlApplicationContext(“context.xml”);
字符串[]bean=ctx.getBeanDefinitionNames();
for(字符串:bean){
}
客户机c=新客户机();
c、 anotherMethod();
}捕获(可丢弃的t){
t、 printStackTrace();
}
}
公共无效无热法()
{
Set=streamingthread.getData();
System.out.println(set.size());
}
}
这是我的StreaminThread类

  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }
   package com;
    import java.util.HashSet;
    import java.util.Set;
    public class StreamingThread extends Thread {
        int i=0;
        StreamingThread()
        {
            System.out.println("Hi Streamed Thread Called"+i);
        }

        private  Set<String> streamSet = new HashSet();


        public Set getData()
        {
            return streamSet;
        }

        public void run() {
            /*while (true) {
                for (int i = 0; i < 12; i++) {
                    streamSet.add("Test" + 1);
                //s System.out.println("Run Called"+i);
                }
            }*/
        }
    }
package-com;
导入java.util.HashSet;
导入java.util.Set;
公共类StreamingThread扩展线程{
int i=0;
StreamingThread()
{
System.out.println(“称为“+i”的高速流线程);
}
私有集streamSet=newhashset();
公共集getData()
{
返回流集;
}
公开募捐{
/*while(true){
对于(int i=0;i<12;i++){
streamSet.添加(“测试”+1);
//s System.out.println(“称为“+i”的运行);
}
}*/
}
}

如果希望注入相同的对象,则不能定义其他bean,而是使用相同的定义:

  <bean id="streamingthread" class="com.StreamingThread" scope="singleton" >
   </bean>

 </beans>



    **This is StreamingThread**     




   package com;

    import java.util.Set;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public  class Client{
        @Autowired
        @Qualifier("streamingthread")
        private StreamingThread streamingthread;
        public void run()
        {
            while(true)
            {
            }
        }
        public static void main(String[] args) {
            try {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
                String[] beans = ctx.getBeanDefinitionNames();
                for (String string : beans) {
                }
                Client c = new Client();
                c.anotherMethod();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        public void anotherMethod()
        {
            Set set = streamingthread.getData();
            System.out.println(set.size());
        }
    }
<beans>
    <bean id="stream-core" class="com.StreamHandler" scope="singleton" init-method="init">
        <constructor-arg><ref bean="streamingthread"/></constructor-arg>
    </bean>

    <bean id="streamingthread" class="com.StreamingThread" scope="singleton" />
</beans>

您好,谢谢您的回复,您的意思是我需要在StreamingThread类中添加一个方法,该类返回Set know??private字段,该字段不能从外部访问。这是基本的面向对象的东西。如果您想使其可访问,您可以添加一个公共方法,该方法允许访问字段(或其数据),是的。非常感谢您的帮助。我是春天的新手,所以我不确定,这就是为什么再次询问和感谢。我尝试了建议的方式,但我得到了NPE,你能帮助我吗,我已经在编辑部分编辑了我的问题,解决了问题,感谢师父帮助我这样的傻瓜。