spring的配置及依赖注入(详细)

1、spring的配置

1.1、起别名

alias设置别名,为bean设置别名,可以设置多个别名

<!--设置别名:在获取Bean的时候可以使用别名获取--> 
<alias name="user" alias="user2"/>

1.2、bean的配置

<!--注册一个javabean到spring容器中,这里name属性也可以设置多个别名,-->
<bean id="user" class="com.lili.entity.User" name="user3 user4 user5">
    <property name="age" value="12"/>
    <property name="name" value="张三" />
</bean>

1.3、import

可以导入其他配置文件

<import resource="beans.xml"/>

2、spring的依赖注入(DI)

  • 依赖:Bean对象的创建依赖于容器
  • 注入:Bean对象所依赖的资源,由容器来设置和装配

2.1、构造器注入

   <!--方式一(根据参数下标设置)-->
    <bean id="user1" class="com.lili.entity.User">
        <constructor-arg index="0" value="12"/>
        <constructor-arg index="1" value="张三"/>
    </bean>
 <!--方式二(根据参数名字设置)-->
    <bean id="user2" class="com.lili.entity.User">
        <constructor-arg name="age" value="12"/>
        <constructor-arg name="name" value="张三"/>
    </bean>
 <!--方式三(根据参数类型设置)-->
    <bean id="user3" class="com.lili.entity.User">
        <constructor-arg type="int" value="12"/>
        <constructor-arg type="java.lang.String" value="张三"/>
    </bean>

2.2、Set注入【重点】

1.编写复杂类型实体类

Address.java

public class Address {
    private String address;
    // get、set、toString
}

Student.java

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private Properties properties;
    private String wife;
    // get、set、toString
}

2.开始注入

1、常量注入

<bean id="student" class="com.lili.entity.Student">
     <property name="name" value="齐菁菁"/>
</bean>

2、bean注入

<bean id="student" class="com.lili.entity.Student">
    <property name="address" ref="address"/>
</bean>
<bean id="address" class="com.lili.entity.Address">
    <property name="address" value="china"/>
</bean>

3、数组注入

<property name="books">
    <array>
        <value>自律</value>
        <value>美学</value>
        <value>杂念</value>
     </array>
</property>

4、map注入

<property name="card">
    <map>
        <entry key="奥特曼" value="迪迦"/>
        <entry key="弼马温" value="孙悟空"/>
    </map>
</property>

5、set注入

<property name="games">
    <set>
        <value>王者荣耀</value>
        <value>天天酷跑</value>
        <value>天天飞车</value>
     </set>
</property>

6、list注入

<property name="hobbies">
    <list>
        <value>羽毛球</value>
        <value>乒乓球</value>
    </list>
</property>

8、properties注入

<property name="properties">
    <props>
        <prop key="学号">124</prop>
        <prop key="性别"></prop>
    </props>
</property>

9、Null注入

<property name="wife">
    <null/>
</property>

10、测试:

@Test
public void test3() {
    // 加载配置文件获取上下文对象
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
    System.out.println("------------");
    // 从容器里获取对象
    Student student = (Student) context.getBean("student");
    System.out.println(student);
}

11、运行如下:

Student{name='齐菁菁', address=Address{address='china'}, books=[自律, 美学, 杂念], hobbies=[羽毛球, 乒乓球], card={奥特曼=迪迦, 弼马温=孙悟空}, games=[王者荣耀, 天天酷跑, 天天飞车], properties={学号=124, 性别=男}, wife='null'}

2.3、扩展依赖注入实现

实体类:

public class User {
    /**
     * 年龄
     */
    private int age;
    /**
     * 名字
     */
    private String name;

    public User(){
    }
    public User(int age,String name){
        this.age = age;
        this.name = name;
    }
    // get,set,toString
}

p命名空间注入:需要在头文件加入约束文件(set注入方式)

导入约束 : xmlns:p="http://www.springframework.org/schema/p"
    <!--P(属性命名空间),set方法依然要有-->
<bean id="user1" class="com.lili.entity.User" p:age="12" p:name="qijingjing"/>

c:命名空间注入:需要在头文件加入约束文件(构造器注入方式)

导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造器命名空间)-->
<bean id="user2" class="com.lili.entity.User" c:age="13" c:name="qijingjing" />

测试代码:

@Test
public void test4() {
    // 加载配置文件获取上下文对象
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans4.xml");
    System.out.println("------------");
    // 从容器里获取对象
    User user1 = (User) context.getBean("user1");
    User user2 = (User) context.getBean("user2");
    System.out.println(user1);
    System.out.println(user2);
}

结果如下:

User{age=12, name='qijingjing'}
User{age=13, name='qijingjing'}
posted @   JamieChyi  阅读(29)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示