Spring中bean有三种装配机制,分he别是:

  1. 在xml中显式配置;
  2. 在java中显式配置;
  3. 隐式的bean发现机制和自动装配。

Spring的自动装配需要从两个角度来实现,或者说是两个操作:

  • 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
  • 自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;
  • 组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。

autowire

按名称autowire=”byName”
按类型autowire=”byType”

  1. <!--byName:会自动在容器上下文中查找,和自己对象set方法后面值对应的beanid-->
  2. <bean id="person" class="com.sy.pojo.Person" autowire="byName">
  3. <!--byName:会自动在容器上下文中查找,和自己对象属性类型相同的的bean-->
  4. <bean id="person" class="com.sy.pojo.Person" autowire="byType">
  5. <property name="name" value="御坂美琴"/>
  6. </bean>

使用注解

在spring配置文件中引入context文件头

<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
        http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/beans/spring-aop.xsd">

开启属性注解支持!



    <context:annotation-config/>

@Autowired

将User类中的set方法去掉,使用@Autowired注解

public class Person {



    @Autowired
    private Cat cat;
    @Autowired
    private Wife wife;

    private String name;

    public Cat getCat() {
        return cat;
    }

    public Wife getWife() {
        return wife;
    }

    public String getName() {
        return name;
    }

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

@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。
//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;

@Qualifier

@Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配

@Qualifier不能单独使用。
使用方法:

@Autowired
@Qualifier(value = "cat2")
private Cat cat;
@Autowired
@Qualifier(value = "dog2")
private Dog dog;

@Resource

  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
  • 其次再进行默认的byName方式进行装配;
  • 如果以上都不成功,则按byType的方式自动装配。
  • 都不成功,则报异常。 ```java public class Person {
@Resource
private Cat cat;
@Resource
private Wife wife;

private String name;

public Person(@Nullable String name) {
    this.name = name;
}

public Cat getCat() {
    return cat;
}

public Wife getWife() {
    return wife;
}

public String getName() {
    return name;
}

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

}


<a name="qR1qY"></a>
## @Component组件
等价于<bean id="user" class="com.sy.pojo.User"/>

1.  @Value("xxx")
1. _// 相当于配置文件中 <property name="name" value="xxx"/>_
<a name="g3fXg"></a>
#### @Component三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。<br />@Controller:web层<br />@Service:service层<br />@Repository:dao层

**写上这些注解,就相当于将这个类交给Spring管理装配了!**
<a name="pYjqN"></a>
## 作用域@scope

singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。

prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
```java
@Controller("user")
@Scope("prototype")
public class User {
    @Value("xxx")
    public String name;
}

XML与注解比较

XML可以适用任何场景 ,结构清晰,维护方便

注解不是自己提供的类使用不了,开发简单方便

xml与注解整合开发 :推荐最佳实践

xml管理Bean

注解完成属性注入

使用过程中, 可以不用扫描,扫描是为了类上的注解

作用:

<!--指定要扫描的包,这个包下注解就会生效-->
<context:component-scan base-package="com.sy.pojo "/>

进行注解驱动注册,从而使注解生效

用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册

如果不扫描包,就需要手动配置bean

如果不加注解驱动,则注入的值为null!

基于Java类进行配置

写一个pojo下的实体类

@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }
    @Value("定扯")
    public void setName(String name) {
        this.name = name;
    }

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

新建一个config配置包,编写一个MyConfig配置类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

//等同于Component 被Spring容器托管,注入到容器中
@Configuration
@ComponentScan("com.sy.pojo")//扫描包
@Import(MyConfig2.class)
public class MyConfig {

    // 注册一个bean,
    //这个方法名字相当于id
    //返回值相当于class属性
    @Bean
    public User getUser(){
        return new User();//返回要注入到bean的对象
    }

}

测试

public class MyTest {
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig上下文来获取容器,通过配置类的class 对象加载
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser.getName());;
    }
}

如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig上下文来获取容器,通过配置类的class 对象加载