spring学习记录(五)——使用注解开发

8、使用注解开发

8.1、说明

在spring4之后,使用注解形式开发,必须要引入aop的包,引入webmvc的包会自动引入aop的包

在这里插入图片描述

8.2、Bean的实现

我们之前都是在xml中使用bean标签注入,但是在实际开发中,我们大多数使用注解的形式开发

配置注解扫描包

 <context:component-scan base-package="com.zhangjiangbo.cn.bean"/>

创建实体类加上注解Component使这个类能够被spring扫描到

package com.zhangjiangbo.cn.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@Data
@Component("user")
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private String name;

    private int age;
}

创建测试类

package com.zhangjiangbo.cn;

import com.zhangjiangbo.cn.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    @org.junit.Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
    }
}

输出
在这里插入图片描述
可以看到,我们创建的实体类已经被打印了出来

8.3、基于注解的属性注入

使用注解注入属性
1、可以不提供set方法,直接在字段名上添加@Value(“值”)

@Data
@Component("user")
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Value("张江博")
    private String name;

    @Value("24")
    private int age;
}

打印
在这里插入图片描述
2、 如果提供了set方法,也可以在set方法上添加@Value(“值”)

@Component("user")
public class User {

    private String name;

    private int age;

    @Value("小迷你")
    public void setName(String name) {
        this.name = name;
    }

    @Value("24")
    public void setAge(int age) {
        this.age = age;
    }

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

打印
在这里插入图片描述
可以看到,通过两种方法都可以给属性赋值(查看打印结果时需要有toString方法)

使用注解是为了更快更便捷的编写代码
@Component三个衍生注解

  • @Contrller : web层
  • @Service:业务逻辑层
  • @Repository : 持久层
    写上这些注解,就相当于将这个类交给spring管理装配了

8.4、使用注解的作用域

@Scope作用域

在实体类上加上@Scope注解可以指定作用域

package com.zhangjiangbo.cn.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Scope("singleton")
@Component("user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    @Value("小迷你")
    private String name;
    
    @Value("24")
    private int age;

}

测试

 @org.junit.Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        User user = context.getBean("user", User.class);
        User user1 = context.getBean("user", User.class);
        System.out.println(user==user1);
    }

打印
在这里插入图片描述

将作用域修改为propertype

@Scope("prototype")
@Component("user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Value("小迷你")
    private String name;

    @Value("24")
    private int age;

}

打印
在这里插入图片描述

小结

xml与注解比较

  • xml可以使用任何场景,结构清晰,维护方便
  • 注解不是自己提供的类使用不了,开发简单方便

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

  • xm管理bean
  • 注解完成属性注入
  • 使用过程中,可以不用扫描,扫描是为了类上的注解
<context:annotation-config/>

作用:

  • 进行注解驱动注册,从而使注解生效
  • 用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向spring注册
  • 如果不扫描包,就需要手动配置bean
  • 如果不加注解驱动,则注入的值为null

8.5、基于Java的类进行配置

创建实体类

package com.zhangjiangbo.cn.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Dog {

    @Value("小狗")
    private String name;
}

创建配置

package com.zhangjiangbo.cn.config;

import com.zhangjiangbo.cn.bean.Dog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// Configuration表示这是一个配置类
@Configuration
public class MyConfig {

    @Bean
    public Dog dog(){
        return new Dog();
    }
}

测试类

    @org.junit.Test
    public void test02(){
        //如果完全使用了配置类,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        Dog dog = context.getBean("dog", Dog.class);
        System.out.println(dog);
    }

打印
在这里插入图片描述
可以看到我们已经是完全注解式开发,没有使用spring的配置文件

使用配置注解也可以导入其他的配置,使用@Improt注解

例如,我们要导入MyConfig2的配置

package com.zhangjiangbo.cn.config;

import com.zhangjiangbo.cn.bean.Dog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

// Configuration表示这是一个配置类
@Configuration
@Import(MyConfig2.class)
public class MyConfig {

    @Bean
    public Dog dog(){
        return new Dog();
    }
}

新建一个myConfig2

package com.zhangjiangbo.cn.config;

import com.zhangjiangbo.cn.bean.Dog;
import com.zhangjiangbo.cn.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// Configuration表示这是一个配置类
@Configuration
public class MyConfig2 {

    @Bean
    public User user(){
        return new User();
    }
}

测试类

    @org.junit.Test
    public void test02(){
        //如果完全使用了配置类,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        Dog dog = context.getBean("dog", Dog.class);
        User user = context.getBean("user", User.class);
        System.out.println(dog);
        System.out.println(user);
    }

打印
在这里插入图片描述

可以看到两个配置中的bean都被打印了

关于java的配置方式,在以后的springboot和springcloud中我们还会看到大量的这种@Import注解,我们需要知道这些注解的作用既可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值