Spring学习笔记(二)——使用注解开发

1、开启注解

jdk1.5开始支持注解,spring2.5开始全面支持注解。

准备工作:利用注解的方式注入属性。

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

xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

2、开启属性注解支持!

<context:annotation-config/>

@Autowired

  • @Autowired是按类型自动转配的,不支持id匹配。
  • 需要导入 spring-aop的包!

测试:
1、将User类中的set方法去掉,使用@Autowired注解

@AllArgsConstructor
@ToString
@NoArgsConstructor
@Setter
@Getter
public class User {
    
    
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String str;

}

2、此时的配置文件

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

    <context:annotation-config/>

    <bean id="dog" class="chen.pojo.Dog"/>
    <bean id="cat" class="chen.pojo.Cat"/>

    <bean id="user" class="chen.pojo.User">
    </bean>
</beans>

3、测试,成功输出结果!

@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。

//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;

2、使用注解开发——详细

2.1、环境搭建

在spring4之后,想要使用注解形式,必须要引入aop的包

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

在配置文件中,引入context约束

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

</beans>

2.2、Bean的实现

我们之前都是使用bean标签进行bean注入,但是在实际开发中,我们一般会使用注解!

  1. 配置扫描哪些包下的注解
    <!--指定注解扫描包-->
    <context:component-scan base-package="chen.pojo"/>
  1. 在指定包下编写类,增加注解
@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
    
    
    public String name = "志辉";
}
  1. 测试
    @Test
    public void test02() {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user");
        System.out.println(user.name);
    }

2.3、属性注入

  1. @Component
    (1)可以不提供set方法,直接在名上添加@value(“值”)
@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
    
    

    @Value("志辉")
    // 相当于配置文件中 <bean id="user" class="当前注解的类"/>
    public String name;
}

(2)如果提供了set方法,在set方法上添加@value(“值”)

@Component("user")
public class User {
    
    

    public String name;

    @Value("志辉")
    public void setName(String name) {
    
    
        this.name = name;
    }
}

我们使用这些注解,就是替代在配置文件当中配置步骤而已!更加的方便快捷!

@Component三个衍生注解

为了更好的进行分层,Spring可以使用其他三个注解,功能一样

  • @Controller(web层)
  • @Service(service层)
  • @Repository(mapper层)

写上这些注解,就相当于把这个类交给Spring管理装配了

@scope

  • singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
  • prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Component("user")
@Scope("prototype")
public class User {
    
    

    public String name;

    @Value("志辉")
    public void setName(String name) {
    
    
        this.name = name;
    }
}

2.4、配置类注解

  1. @Configuration

测试:

编写一个实体类:Dog

@Component
public class Dog {
    
    

    @Value("dog")
    public String name;
}

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

@Configuration //代表这是一个配置类
public class MyConfig {
    
    
    
    @Bean //通过方法注册一个bean,这里的返回值就是bean的类型,方法名就是bean的id!
    public Dog dog(){
    
    
        return new Dog();
    }
}

测试:

    @Test
    public void test03() {
    
    
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        Dog dog = (Dog) context.getBean("dog");
        System.out.println(dog.name);
    }

成功输出结果!!!!

导入其他配置

1、我们在编写一个配置类

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

2、在之前的配置类中我们来选择导入这个配置类

@Configuration
@Import(MyConfig2.class)  //导入合并其他配置类,类似于配置文件中的 inculde 标签
public class MyConfig {
    
    

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

}

关于这种Java类的配置方式,我们在之后的SpringBoot 和 SpringCloud中还会大量看到,我们需要知道这些注解的作用即可!

3、小结

XML与注解比较

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

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

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

作用:

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

猜你喜欢

转载自blog.csdn.net/qq_45151059/article/details/114002578