关于Spring那些你不懂的事

1、注解实现自动装配

使用注解须知:

1、导入约束。context约束

2、配置注解的支持:context:annotation-config/

<?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

直接在属性行使用,也可以在set方式上使用。

使用Autowired不需要再写set方法,前提是你这个自动装配的属性在IOC容器中存在,且符合名字bytype。

如果使用@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value=“XXX”)去配合@Autowired使用,指定一个唯一的bean对象注入。

@Resource和**@Autowired**区别:

都是用来自动装配的,都可以放在属性字段上。

@Autowired通过bytype的方式实现。而且必须要求这个对象存在!【常用】

@Resource默认通过byname的方式实现,如果找不到名字,则通过bytype实现,如果两个都找不到的情况下,就报错。【常用】

执行顺序不同:@Autowired首先是通过bytype装配,找不到在通过byname进行装配。

​ @Resource首先是通过byname装配,找不到在通过bytype进行装配。

@Component :组件,放在类上,说明这个类被Spring进行管理,就是bean!

@Value

2、Spring注解开发

在Spring4之后,要使用注解开发,必须要保证aop的包导入。

使用注解需要导入

2.1、bean

2.2、属性如何注入

// 等价于<bean id="user" class="dao.Use/>
// @Component 组件
@Component
public class User {
    // 相当于<property name=“name" value="yangfan" />
    @Value("yangfan")
    public String name;
}

2.3、衍生注解

@Component有几个衍生注解,我们在web开发中,会按照MVC三层架构分层。

  • dao 【@Repository】
  • service 【@service】
  • controller 【@controller】
  • 这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配bean。

2.4、小结

XML与注解:

  • XML更加万能,适用于任何场合,维护简单方便。
  • 注解,不是自己类使用不了。维护相对复杂。

XML与注解最佳实践:

  • XML用来管理bean。
  • 注解只负责完成属性的注入。
  • 我们在使用的过程中,只需要注意一个问题,必须让注解生效,就需要开启注解的支持。

3、使用Java的方式配置Spring

完全不使用Spring的XML配置了,全部交给Java实现。

JavaConfig是Spring的一个子项目。在Spring4之后,

实体类

package pojo;

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

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 + '\'' +
                '}';
    }
}

配置文件

package config;

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

// 配置类,被Spring容器托管,注册到容器中,
@Configuration
@ComponentScan("pojo")
@Import(SpringConfig2.class)
public class SpringConfig {

    // 注册一个bean,就相当于之前写的bean标签
    // 这个方法的名字,就相当于bean标签中的id属性
    // 这个方法的返回值,就相当于bean标签中的class属性
    @Bean
    public User getUser() {
        return new User();// 就是返回bean实例化后的对象。
    }
}

测试类

import config.SpringConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import pojo.User;

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        User user = (User) context.getBean("getUser");
        System.out.println(user.getName());

    }
}

纯Java的配置在Springboot中随处可见。

关注我!不迷路!一起交流!一起学习!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值