spring使用注解实现自动装配

要使用注解之前

1.要配置文件要导入aop的约束  context约束

2.在配置文件中开启注解的支持

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<!--开启注解的支持-->
    <context:annotation-config/>
</beans>

2创建3个实体类  Student  Age   school

Student 中装配 age school

package com.huahua.pojo;

public class Student {
    private String name;
    private Age age;
    private school school;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }

    public Student() {
    }

    public Student(String name, Age age, com.huahua.pojo.school school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Age getAge() {
        return age;
    }

    public void setAge(Age age) {
        this.age = age;
    }

    public com.huahua.pojo.school getSchool() {
        return school;
    }

    public void setSchool(com.huahua.pojo.school school) {
        this.school = school;
    }
}
package com.huahua.pojo;

public class Age {
    private String age;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Age{" +
                "age='" + age + '\'' +
                '}';
    }
}

package com.huahua.pojo;

public class school {
    private String school;

    public void setSchool(String school) {
        this.school = school;
    }

    @Override
    public String toString() {
        return "school{" +
                "school='" + school + '\'' +
                '}';
    }
}

3.在配置文件中 配置各个实体类的Bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<!--开启注解的支持-->
    <context:annotation-config/>
<!--    Age实体类的bean-->
    <bean id="age1" class="com.huahua.pojo.Age">
        <property name="age" value="23"/>
    </bean>
<!--    school实体类的bean-->
    <bean id="school" class="com.huahua.pojo.school">
        <property name="school" value="第6小学"/>
     </bean>
<!--    student的实体类的bean-->
    <bean id="student" class="com.huahua.pojo.Student">
<!--        String类型的name进行set注入-->
        <property name="name" value="huahua"/>
    </bean>
</beans>

4.student类中注解设置

package com.huahua.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Student {
    private String name;
    //如果显示的定义了Autowired的属性required为false则表明这个属性可以为空
    @Autowired(required = false)
    @Qualifier(value = "age1") //也可以特定设置在配置文件中获取的id 当配置文件中设置Age类 bean 的id不为student类中的age时
    private Age age;

     private school school;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }

    public Student() {
    }

    public Student(String name, Age age, com.huahua.pojo.school school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Age getAge() {
        return age;
    }

    public void setAge(Age age) {
        this.age = age;
    }

    public com.huahua.pojo.school getSchool() {
        return school;
    }
    //也可以直接标注在set方法上
    @Autowired
    public void setSchool(com.huahua.pojo.school school) {
        this.school = school;
    }
}

测试类:

package com.huahua.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest {
    @Test
    public void mytest()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);

        System.out.println(student.toString());
    }
}

//运行结果:

Student{name='huahua', age=Age{age='23'}, school=school{school='第6小学'}}

@Autowired  是智能识别的  当注入ioc容器只有一个类型的时候,会按照byType进行查找,如何有2个或者2个以上一样的类型的时候会按照byName进行查找

@Resource  与@Autowired功能相同   不给@Resource  是现根据名字  在根据类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值