IOC创建对象方式+set注入+构造器注入

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 );
  }

}

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>

3、测试类

@Test
public void test(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   //在执行getBean的时候, user已经创建好了 , 通过无参构造
   User user = (User) context.getBean("user");
   //调用对象的方法 .
   user.show();
}

通过有参构造方法来创建

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 );
  }

}

beans.xml 有三种方式编写

<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <!-- index指构造方法 , 下标从0开始 -->
   <constructor-arg index="0" value="kuangshen2"/>
</bean>
<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <!-- name指参数名 -->
   <constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>

3、测试

@Test
public void testT(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   UserT user = (UserT) context.getBean("userT");
   user.show();
}

Spring配置

别名

<alias name="userT" alias="userNew"/>

Bean的配置

<!--bean就是java对象,由Spring创建和管理-->

<!--
   id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
   如果配置id,又配置了name,那么name是别名
   name可以设置多个别名,可以用逗号,分号,空格隔开
   如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
   class是bean的全限定名=包名+类名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
   <property name="name" value="Spring"/>
</bean>

import

这个import, -般用于团队开发使用,它可以将多个配置文件,导入合并为一个

<import resource="{path}/beans.xml"/>

依赖注入

构造器注入

同有参构造创建对象

<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <!-- index指构造方法 , 下标从0开始 -->
   <constructor-arg index="0" value="kuangshen2"/>
</bean>
<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <!-- name指参数名 -->
   <constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="com.kuang.pojo.UserT">
   <constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>

set注入

pojo类

@Setter
@ToString
public class Student {
    private String name;
    private Address address;
    private String[] book;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;
}

@Setter
@ToString
public class Address {
    private String address;
}

*.xml

<bean id="address" class="com.chao.pojo.Address"/>
<bean id="student" class="com.chao.pojo.Student">
    <!--常量注入-->
    <property name="name" value="吴梓超"/>
    <!--Bean注入-->
    <property name="address" ref="address"/>
    <!--数组注入-->
    <property name="book">
        <array>
            <value>红楼梦</value>
            <value>西游记</value>
            <value>水浒传</value>
            <value>三国演义</value>
        </array>
    </property>
    <!--list注入-->
    <property name="hobbies">
        <list>
            <value>跑步</value>
            <value>听歌</value>
            <value>看电影</value>
        </list>
    </property>
    <!--map注入-->
    <property name="card">
        <map>
            <entry key="建行" value="1233123313"/>
            <entry key="工行" value="1233456413"/>
        </map>
    </property>
    <!--set注入-->
    <property name="games">
        <set>
            <value>LOL</value>
            <value>CF</value>
            <value>DNF</value>
        </set>
    </property>
    <!--NULL注入-->
    <property name="wife"><null></null></property>
    <!--properties注入-->
    <property name="info">
        <props>
            <prop key="姓名">吴梓超</prop>
            <prop key="身份证号">12312312</prop>
            <prop key="性别">男</prop>
        </props>
    </property>
</bean>

测试

public class MyTest {
    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }

    /**
     * 结果
     * Student(
     * name=吴梓超,
     * address=Address{address='地球'},
     * book=[红楼梦, 西游记, 水浒传, 三国演义],
     * hobbies=[跑步, 听歌, 看电影],
     * card={建行=1233123313, 工行=1233456413},
     * games=[LOL, CF, DNF],
     * info={性别=男, 姓名=吴梓超, 身份证号=12312312},
     * wife=null)
     */
}

P命名空间

用的是无参构造

@Setter
@ToString
public class User {
    private String name;
    private int age;
}
在头文件中加入约束   
xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.chao.pojo.User" p:name="吴梓超" p:age="24"/>
@Test
public void test1(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
    User user = (User) context.getBean("user");
    System.out.println(user.toString());
}

C命名空间注入

用的是有参构造

@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private int age;
}
在头文件中加入约束
xmlns:c="http://www.springframework.org/schema/c"
 <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.chao.pojo.User" c:name="吴梓超" c:age="24"/>
@Test
public void test1(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
    User user = (User) context.getBean("user2");
    System.out.println(user.toString());
}

posted @ 2020-06-02 20:32  我不爱吃鱼  阅读(507)  评论(1编辑  收藏  举报
Live2D