IOC创建对象方式

 

New module

6、IOC创建对象方式

 

通过无参构造方法来创建

1、User.java

public class User {

   private String name;

   public User() {

       System.out.println("user无参构造方法");

  }

   public void setName(String name) {

       this.name = name;

  }

   public void show(){

       System.out.println("name="+ name );

  }

}

6、IOC创建对象方式

 

注:

配置文件上下文

6、IOC创建对象方式

注end

 

2、beans.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"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="user" class="com.kuang.pojo.User">

       <property name="name" value="kuangshen"/>

   </bean>

</beans>

6、IOC创建对象方式

 

注:

注册完之后这里就会有叶子

6、IOC创建对象方式

注end

 

3、测试类

@Test

public void test(){

   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

   //在执行getBean的时候, user已经创建好了 , 通过无参构造

   User user = (Usercontext.getBean("user");

   //调用对象的方法 .

   user.show();

}

6、IOC创建对象方式

 

6、IOC创建对象方式

 

结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!

 

 

通过有参构造方法来创建

1、UserT . java

public class UserT {

   private String name;

   public UserT(String name) {

       this.name = name;

  }

   public void setName(String name) {

       this.name = name;

  }

   public void show(){

       System.out.println("name="+ name );

  }

}

6、IOC创建对象方式

 

2、beans.xml 有三种方式编写

<!-- 第一种根据index参数下标设置 -->

<bean id="userT" class="com.kuang.pojo.UserT">

   <!-- index指构造方法 , 下标从0开始 -->

   <constructor-arg index="0" value="kuangshen2"/>

</bean>

6、IOC创建对象方式

 

<!-- 第二种根据参数名字设置 -->

<bean id="userT" class="com.kuang.pojo.UserT">

   <!-- name指参数名 -->

   <constructor-arg name="name" value="kuangshen2"/>

</bean>

6、IOC创建对象方式

 

<!-- 第三种根据参数类型设置 -->

<bean id="userT" class="com.kuang.pojo.UserT">

   <constructor-arg type="java.lang.String" value="kuangshen2"/>

</bean>

6、IOC创建对象方式

 

3、测试

@Test

public void testT(){

   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

   UserT user = (UserTcontext.getBean("userT");

   user.show();

}

结论:在配置文件加载的时候。其中管理的对象都已经初始化了!

 

 

再创建一个无参构造

6、IOC创建对象方式

 

6、IOC创建对象方式

 

6、IOC创建对象方式

结论:在配置文件加载的时候。其中管理的对象都已经初始化了!

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2022-01-10
  • 2021-11-30
  • 2021-07-20
  • 2021-08-02
  • 2022-01-29
猜你喜欢
  • 2022-12-23
  • 2021-10-27
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
相关资源
相似解决方案