Spring自动装配Bean及注解开发

Spring笔记三:

1、自动装配(AutoWired)

实体类:
Dog:

package com.itcast.pojo;

public class Dog {
    public void show(){
        System.out.println("这是一只狗");
    }
}

Cat:

package com.itcast.pojo;

public class Cat {
    public void show(){
        System.out.println("这是一只猫");
    }
}

Person:

package com.itcast.pojo;

public class Person {
    private String name;
    private Dog dog;
    private Cat cat;

    public Person() {
    }

    public Person(String name, Dog dog, Cat cat) {
        this.name = name;
        this.dog = dog;
        this.cat = cat;
    }

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

①byName,会自动在容器上下文查找和自己对象set后面的名字值对应的beanid!
*.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="dog" class="com.itcast.pojo.Dog"/>
    <bean name="cat" class="com.itcast.pojo.Cat"/>

    <bean name="person" class="com.itcast.pojo.Person" autowire="byName">
        <property name="name" value="unvi"/>
    </bean>
</beans>

测试:

 public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = context.getBean("person", Person.class);
        person.getDog().show();
        person.getCat().show();
    }

结果:
在这里插入图片描述

②byType,会自动在容器上下文查找和自己对象类型对应的beanid!
*.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="dog11" class="com.itcast.pojo.Dog"/>
    <bean name="cat22" class="com.itcast.pojo.Cat"/>

    <bean name="person" class="com.itcast.pojo.Person" autowire="byType">
        <property name="name" value="unvi"/>
    </bean>
</beans>

测试:

 public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = context.getBean("person", Person.class);
        person.getDog().show();
        person.getCat().show();
    }

结果:
在这里插入图片描述

2、注解开发(Annotation)

The introduction of annotation-based configuration raised the question of whether this approach is “better” than 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"
    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>

修改:
@Autowired 【通过byType】
@Autowired可以直接标注在属性名上,也可以标注在set方法上;
使用@Autowired还可以不写对应的set方法,前提这个属性在spring容器中有装配,符合命名规范

【@Qualifier(value = “cat111”)指定获取唯一beanid】
Person:

package com.itcast.pojo;

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

public class Person {
    private String name;
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;

    public Person() {
    }

    public Person(String name, Dog dog, Cat cat) {
        this.name = name;
        this.dog = dog;
        this.cat = cat;
    }

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

*.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"
       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/>

    <bean name="cat" class="com.itcast.pojo.Cat"/>
    <bean name="dog" class="com.itcast.pojo.Dog"/>
    <bean name="person" class="com.itcast.pojo.Person"/>
</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"
       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/>
<!--    扫描包-->
    <context:component-scan base-package="com.itcast.pojo"/>

</beans>

实体类:
Cat:

package com.itcast.pojo;

import org.springframework.stereotype.Component;

@Component
public class Cat {
    public void show(){
        System.out.println("这是一只猫");
    }
}

Dog:

package com.itcast.pojo;

import org.springframework.stereotype.Component;

@Component
public class Dog {
    public void show(){
        System.out.println("这是一只狗");
    }
}

Person:

package com.itcast.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class Person {
    @Value("unvi")
    private String name;
    @Autowired
    private Dog dog;
    @Autowired
//    @Qualifier(value = "cat111")
    private Cat cat;

    public Person() {
    }

    public Person(String name, Dog dog, Cat cat) {
        this.name = name;
        this.dog = dog;
        this.cat = cat;
    }

    public String getName() {
        return name;
    }

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

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

/**
@Component 相当于在xml中注册bean!

<bean name="xxx" class="com.itcast.pojo.xxx"/>

@Compoent 的衍生(功能都一样)
Dao:@Repository
Service:@Service
Controller:@Controller

*/
属性值基本类型的用@Value(“XXX”);
引用类型的用@AutoWired
测试:
使用这种方式:自动装配到spring容器中的id为该类的类名

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = context.getBean("person", Person.class);
        System.out.println(person.getName());
        person.getDog().show();
        person.getCat().show();
    }

结果:
在这里插入图片描述

注解开发和xml配置:
xml更加万能,适用于任何的场合,维护简单
注解不是自己的类用不了,维护复杂

【xml和注解的配合】
xml用来管理bean
注解负责属性的注入

使用注解!!一定要开始注解支持!!

 <context:annotation-config/>
 <context:component-scan base-package="com.itcast.pojo"/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值