|
@@ -19,6 +19,50 @@ public class TestBean {
|
|
|
System.out.println(user1);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testOther(){
|
|
|
+ ApplicationContext context = new ClassPathXmlApplicationContext("bean-other.xml");
|
|
|
+ // Object getBean(String name) throws BeansException;
|
|
|
+ Role role = (Role)context.getBean("role");
|
|
|
+ System.out.println(role);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testBean(){
|
|
|
+ ApplicationContext context = new ClassPathXmlApplicationContext("bean-other.xml");
|
|
|
+ // 通过id获取对象
|
|
|
+ Role role0 = (Role)context.getBean("role");
|
|
|
+ System.out.println(role0);
|
|
|
+
|
|
|
+ // <T> T getBean(Class<T> requiredType) throws BeansException;
|
|
|
+ // 因为通过传入类型 可以确定要返回的对象的类型 所以不需要再强制转化了
|
|
|
+ // 通过类别获取对象
|
|
|
+ Role role1 = context.getBean(Role.class);
|
|
|
+ System.out.println(role1);
|
|
|
+
|
|
|
+// ApplicationContext context1 = new ClassPathXmlApplicationContext("bean.xml");
|
|
|
+// User user = context1.getBean(User.class);
|
|
|
+// System.out.println(user);
|
|
|
+
|
|
|
+ // <T> T getBean(String name, Class<T> requiredType) throws BeansException;
|
|
|
+ // 通过id和类别获取对象
|
|
|
+ Role role2 = context.getBean("role",Role.class);
|
|
|
+ System.out.println(role2);
|
|
|
+
|
|
|
+ // A true true
|
|
|
+ // B true false
|
|
|
+ // C false false
|
|
|
+ // D false true
|
|
|
+ System.out.println(role0 == role1);
|
|
|
+ System.out.println(role1 == role2);
|
|
|
+
|
|
|
+ // 创建一个bean Student.java name major Grade.java num
|
|
|
+ // bean.xml 声明两个Student的bean标签 name="" major="" name="" major=""
|
|
|
+ // 一个Grade的bean标签 num=""
|
|
|
+ // 使用如上三种方式来获取对象 其中通过类型来获取的 使用Grade对象
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Test
|
|
|
public void test1(){
|
|
|
User user = new User();
|
|
@@ -28,4 +72,59 @@ public class TestBean {
|
|
|
|
|
|
User user2 = new User("lisi");
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testAll(){
|
|
|
+ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
|
|
|
+ Role role = context.getBean("role",Role.class);
|
|
|
+ System.out.println(role);
|
|
|
+// User user = context.getBean("role",User.class);
|
|
|
+// System.out.println(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testConst(){
|
|
|
+ // 这一行 可以一次性初始化所有配置的bean 直接创建出对象 存入容器中
|
|
|
+ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
|
|
|
+ // 只是从容器中取出 id="user1" && class="com.sf.User"
|
|
|
+ // 这里因为配置了构造器参数 而不使用无参构造器 使用对应的有参构造器
|
|
|
+ User user1 = context.getBean("user1",User.class);
|
|
|
+ System.out.println(user1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSpecial(){
|
|
|
+ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
|
|
|
+ // 如果配置了属性值为null 会调用对应的set方法 赋值为null
|
|
|
+ // 以避免对象在初始化时已经有值的情况
|
|
|
+ User user = context.getBean("user2",User.class);
|
|
|
+ System.out.println(user);
|
|
|
+
|
|
|
+// String str = "\"";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testRef(){
|
|
|
+ // <bean id="user" class="com.sf.User">
|
|
|
+ // <!-- 在bean标签下 有一个子标签 property 就是属性的意思 -->
|
|
|
+ // <!-- property标签 自己的标签属性有 name 和 value 对应变量名和变量的值 -->
|
|
|
+ // <!-- 本质上是调用属性的set方法 官网上叫做set注入-->
|
|
|
+ // <property name="name" value="zhangsan"></property>
|
|
|
+ // <property name="desc" value="something"></property>
|
|
|
+ // <property name="role" ref="role"></property>
|
|
|
+ // </bean>
|
|
|
+ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
|
|
|
+ User user = context.getBean("user",User.class);
|
|
|
+ System.out.println(user);
|
|
|
+ // 正常来讲 xml中的bean是按照先后顺序创建的
|
|
|
+ // 当容器创建 user对象时 发现还需要role对象 此时role对象还不存在(因为配置到了最后)
|
|
|
+ // 此时代表 user对象依赖role对象 两个类之间有“依赖关系” 也就是User类有一个属性 Role role;
|
|
|
+ // 容器会先创建一个role对象 然后按照配置中的顺序依次赋值 在setRole时就赋值给已经创建出来的user对象
|
|
|
+ // 在执行到 role对象配置的地方 会发现已经创建过了 就不会重复创建了
|
|
|
+ // 说明 容器默认创建的对象是唯一的
|
|
|
+// Role role = context.getBean("role",Role.class);
|
|
|
+// System.out.println(user.getRole() == role);
|
|
|
+ }
|
|
|
+
|
|
|
}
|