|
@@ -0,0 +1,43 @@
|
|
|
+package com.lovecoding.beans;
|
|
|
+
|
|
|
+import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
|
+
|
|
|
+public class GetBean {
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ //获取Bean对象
|
|
|
+ //第一种通过Bean对象名字获取
|
|
|
+// ClassPathXmlApplicationContext context =
|
|
|
+// new ClassPathXmlApplicationContext("users.xml");
|
|
|
+// Person Person = (Person) context.getBean("Person");
|
|
|
+// Person.getUser().say();
|
|
|
+
|
|
|
+ //第二种获取bean对象方法 这种查询bean对象的方式是 后期常见的类型
|
|
|
+// ClassPathXmlApplicationContext context1 =
|
|
|
+// new ClassPathXmlApplicationContext("users.xml");
|
|
|
+// Person bean = context1.getBean(Person.class);
|
|
|
+// bean.getUser().say();
|
|
|
+
|
|
|
+ //第三种 通过类型 加 bean名字获取
|
|
|
+// ClassPathXmlApplicationContext context2 =
|
|
|
+// new ClassPathXmlApplicationContext("users.xml");
|
|
|
+// Person Person = context2.getBean("Person", Person.class);
|
|
|
+// Person.getUser().say();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 我们配置多个 同类型的 Bean对象, 然后通过 类型去查找Bean
|
|
|
+ * 会发生冲突, 这个时候 可以使用 Bean ID + 类型 查找对象
|
|
|
+ */
|
|
|
+// ClassPathXmlApplicationContext context3 =
|
|
|
+// new ClassPathXmlApplicationContext("users.xml");
|
|
|
+// User bean = context3.getBean( "user1", User.class);
|
|
|
+// System.out.println( bean );
|
|
|
+// ClassPathXmlApplicationContext context4 =
|
|
|
+// new ClassPathXmlApplicationContext("users.xml");
|
|
|
+// Persons bean = context4.getBean(Persons.class);
|
|
|
+// bean.say();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|