基于注解的Spring

本文详细介绍了基于注解的Spring框架,包括IOC注入的注解、配置类、Spring与Junit的整合以及AOP的注解使用。通过注解,可以实现对象创建、依赖注入、作用域和生命周期管理。此外,还探讨了如何在测试中避免无用资源的创建,以及如何启用AOP支持。
摘要由CSDN通过智能技术生成

一、基于注解的IOC注入

1.1 对应的XML配置文件的Spring注入

public class Student {
   

    private Integer number ;
    private String name ;
    private Date date ;

    public void init(){
   
        System.out.println("Student对象被初始化。。。");
    }
    public void destroy(){
   
        System.out.println("Student对象被销毁。。。");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.json.pojo.Student" 
          scope="singleton" init-method="init" destroy-method="destroy">
        <property name="number" value="001"></property>
        <property name="name" value="json"></property>
        <property name="date" ref="now"></property>
    </bean>
    
    <bean id="now" class="java.util.Date"/>

</beans>

改变对应的注解分为四类:

  • 创建对象的注解
  • 注入数据的注解
  • 改变作用范围的注解
  • 生命周期的注解

1.2 创建对象的注解

  • 作用和在XML配置文件中编写一个**< bean >**标签实现的功能一样
  • Component
    • 作用:用于把当前类对象存入spring容器中
    • 属性:value:用于指定bean的id(不写默认为当前类名首字母小写)
  • Controller:一般用在表现层
  • Service:一般用在业务层
  • Repository:一般用在持久层

以上三个注解他们的作用和属性与Component是一模一样,他们三个是Spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰

@Service("accountService")
public class AccountService {
   
    
}

1.3 注入数据的注解

  • 作用就和在XML配置文件中编写一个**< property>**标签实现的功能一样
  • Autowired:可以标注变量,也可以标注方法
    • 作用:自动按照类型注入(容器中配置的其他bean)
      • 容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
      • 如果IOC容器中没有任何bean的类型和要注入的变量类型匹配,则报错
      • 如果IOC容器中有多个类型匹配时,则在匹配的bean的id中,根据要注入的变量名确定,若所有匹配的bean的id没有与要注入的变量名匹配,则注入失败
    • 使用注解注入时,不需指定set方法
  • Qualifier
    • 属性:value:用于指定注入bean的id
    • 在自动按照类型注入的基础之上,再指定Bean的id注入
    • 在给成员变量注入时不能独立使用,必须和 @Autowire 一起使用
    • 给方法参数注入时,可以独立使用
  • Resource
    • 作用:直接按照bean的id注入。它可以独立使用
    • 属性:name:用于指定bean的id
  • Value
    • 作用:用于注入基本类型和String类型的数据
    • 属性:value:用于指定数据的值
    • 可以使用spring中SpEL(也就是spring的el表达式)
    • SpEL的写法:${表达式}
public class Student {
   

    @Value("23")
    private Integer number ;
    
    @Value("json")
    private String name ;
    
    @Autowired
    @Qualifier("now")
    @Resource(name="now")
    private Date date ;
}

1.4 改变作用范围的注解

  • 作用就和在bean标签中使用scope属性实现的功能是一样的
  • Scope
    • 作用:用于指定bean的作用范围
    • 属性:value:指定范围的取值。常用取值:singleton prototype
@Component
@Scope("prototype")
public class Student {
   

}

1.5 生命周期的注解

  • 作用就和在bean标签中使用init-methoddestroy-method的作用是一样的
  • PreDestroy
    • 作用:用于指定销毁方法
  • PostConstruct
    • 作用:用于指定初始化方法
@Component
public class Student {
   

    @PostConstruct
    public void init(){
   
        System.out.println("Student对象被初始化。。。");
    }
    
    @PreDestroy
    public void destroy(){
   
        System.out.println("Student对象被销毁。。。");
    }
}

1.6 注解创建Bean与注入数据

基于注解,需要一个扫描器扫描注解才能生效

导入约束时需要多导入一个 context 名称空间下的约束,配置扫描器,扫描注解

1585052268773

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

    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中,该包下及子包所有的注解均被扫描到-->
    <context:component-scan base-package="com.json" />

    <bean id="now" class="java.util.Date"></bean>
</beans>
@Component
@Scope("singleton")
public class Student {
   

    @Value("23")
    private Integer number ;
    
    @Value("json")
    private String name ;
    
    @Autowired
    @Qualifier("now")
    private Date date ;

    @PostConstruct
    public void init(){
   
        System.out.println("Student对象被初始化。。。");
    }
    @PreDestroy
    public void destroy(){
   
        System.out.println("Student对象被销毁。。。");
    }
}

注意:集合类型无法使用注解注入

二、配置类

基于注解的IOC 配置已经完成,但是依然离不开 spring 的 xml 配 置文件

之所以离不开 xml 配置文件,是因为有一个很关键的配置,即需要在配置文件配置扫描注解的扫描器,而且需要配置第三方jar的Bean对象也需要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
       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:component-scan base-package="com.json" />

</beans>

那么能不能不写xml 配置文件,所有的配置都用注解来实现,扫描配置使用注解,第三方jar的Bean也使用注解配置

2.1 @Configuration

@Configuration:

  • 作用:用于指定当前类为一个Spring的配置类,该配置类取代XML配置文件

  • 该配置类的Bean的获取需要用AnnotationConfigApplicationContext对象

@Configuration
public class SpringConfiguration {
   

}

2.2 @ComponentScan

@ComponentScan:

  • 作用:用于指定Spring 要扫描的包或配置类,作用和在 Spring 的 xml 配置文件中的扫描器一样
  • 属性:
    • basePackages[]:用于指定要扫描的包
    • value[]:用于指定要扫描的包(常用)
    • basePackageClasses[]:扫描指定的类(数组)
@Component
public class Student {
   
    
}

@Configuration
//扫描com.json包及子包所有注解
@ComponentScan("com.json")
public class SpringConfiguration
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值