Spring学习(二)

25 篇文章 0 订阅
4 篇文章 0 订阅

Bean的自动装配

自动装配是Spring满足bean依赖的一种方式,Spring会在上下文(Context)中自动寻找,并自动给bean装配属性

三种方式

  • 在XML中显式配置
  • 在JAVA中显式配置
  • 隐式的自动装配Bean(重要)

在xml中设置进行自动装配(略去)

使用注解实现自动装配

  • 导入约束context
  • 配置注解的支持<context:annotation-config/>
  • 在pojo中添加注解@Autowired,可以在属性上使用,也可以在set方法上使用
  • 注意:首先byName和byType装配,冲突,则需要通过@Qualifier(value="")指定name进行装配
  • 也可以使用jdk自带的@Resource注解

pojo

Cat.java

package com.wangqi.pojo;

public class Cat {
    public void shout() {
        System.out.println("喵喵");
    }
}

Dog.java

package com.wangqi.pojo;

public class Dog {
    public void shout() {
        System.out.println("汪汪");
    }
}

People.java

package com.wangqi.pojo;

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

public class People {

    //required=false表示该自动自动装配为null也可以(即applicationContex.xml中没有合适的bean进行装配)
    @Autowired(required = false)
    private Cat cat;

    @Autowired
    @Qualifier(value = "dog1")
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

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

    public Dog getDog() {
        return dog;
    }

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

    public String getName() {
        return name;
    }

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

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

applicationContext.xml

applicationContext.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 id="cat" class="com.wangqi.pojo.Cat"/>
    <bean id="dog1" class="com.wangqi.pojo.Dog"/>
    <bean id="dog2" class="com.wangqi.pojo.Dog"/>
    <bean id="people" class="com.wangqi.pojo.People"/>
</beans>

Test.java

import com.wangqi.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
    }
}

Spring注解开发

概括

Spring注解开发包括如下几个部分(步骤)

  • bean
  • 属性注入
  • 衍生的注解
  • 自动装配装置
  • 作用域
  • 小结

配置扫描器

配置如下扫描器后就不用配置驱动了<context:annotation-config/>

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

配置bean

直接在User.java中添加如下注解

//等价于<bean id="user" class="com.wangqi.pojo.User"/>
@Component
public class User {
    public String name = "小明";
}

属性注入

也可以放在set方法上

//等价于<bean id="user" class="com.wangqi.pojo.User"/>
@Component
public class User {

    //相当于<property name="name" value="小明"/>,同时,还要添加set方法
    @Value("小明")
    public String name;
}

衍生的注解

@Component有几个衍生的注解,在web开发中,会按照mvc三层分层

  • dao :@Repository
  • service :@Service
  • controller :@Controller

这四个注解功能相同,都是代表将某个类注册到Spring容器中,装配Bean

作用域

默认是singleton

//等价于<bean id="user" class="com.wangqi.pojo.User"/>
@Component
@Scope("prototype")
public class User {

    //相当于<property name="name" value="小明"/>,同时,还要添加set方法
    @Value("小明")
    public String name;
}

小结

xml于注解相比:

  • 更加万能,适合于任何场合,维护简单方便
  • 注解:不是自己的类使用不了(比如引用其他类),维护相对复杂。

xml于注解的最佳实践:

  • xml用来管理Bean
  • 注解只用来完成属性的注入

使用Java的方式配置Spring(比较新)

现在可以完全不用Spring的XML配置,全权交给Java来做!JavaConfig是Spring的一个子项目,在Spring4之后,变成了一个核心功能!

配置类

package com.wangqi.config;

import com.wangqi.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

//理解这个类相当于xml文件是,比如这里也可以添加全局扫描@ComponentScan("com.wangqi.pojo")
// (就可以不在这个类里添加开bean,这时候就要在需要注入的类里添加@Component)
//也可以@import(xxx.clss)合并beans
@Configuration
public class WangConfig {

    //注册一个Bean,相当于之前的一个bean标签,id是方法名
    @Bean
    public User getUser() {
        return new User();
    }

}

bean

package com.wangqi.pojo;

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

@Component
public class User {

    @Value("小明")
    private String name;

    public String getName() {
        return name;
    }

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

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

Test

import com.wangqi.config.WangConfig;
import com.wangqi.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(WangConfig.class);
        User user = (User) context.getBean("getUser");
        User user2 = (User) context.getBean("getUser");
        System.out.println(user == user2);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值