7、框架-Spring-使用注解开发

本文深入探讨了Spring框架下注解开发的详细过程,包括如何导入必要的包、配置约束、使用@Component、@Controller等注解定义bean,以及通过@Value进行属性注入。同时,文章还介绍了自动装配、作用域设置、AOP应用等内容,为读者提供了全面的Spring注解开发指南。

bean

  1. 使用注解开发需要导入spring的一系列包;

  2. 需要再配置文件中加一个约束:context;

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
  3. 配置扫描组件

    <!--自动扫描指定包下的注解-->
    <context:component-scan base-package="com.chao.demo"/>
    
  4. 编写代码:

    //等价于:<bean id="user" class="com.chao.demo.User"/>
    
    /*
    @Component : 组件 bean
    @Controller :web层
    @Service : service层
    @Repository :dao层
     */
    
    @Component("user")
    public class User {
        public String name = "晁文利1号";
    }
    

IOC注入

  1. 可以不用提供set方法,可以直接在属性名上添加一个@Values(值);
@Controller("user2")
public class User2 {
    //    <bean class="com.kuang.demo.User2" id="user2">
    //        <property name="name" value="秦疆2号"/>
    //    </bean>

    @Value("晁文利")
    private String name;

    public String getName() {
        return name;
    }
}
  1. 加入有set呢,直接在set方法上面加上@Values(值);
@Controller("user2")
public class User2 {
    //    <bean class="com.kuang.demo.User2" id="user2">
    //        <property name="name" value="秦疆2号"/>
    //    </bean>


    private String name;

    public String getName() {
        return name;
    }
    
    @Value("晁文利")
    public void setName(String name) {
        this.name = name;
    }
}

自动装配【了解】

@Component("user3")
public class User3 {

    @Value("晁文利3号")
    public String name;
/*
    @Autowired //自动注入按照类型匹配,如果存在两个自动匹配对象的值,则无法匹配;
    @Qualifier("dog1")
    public Dog dog;
*/


    //不是spring的包,是annotation的包;
    @Resource(name = "dog1")
    public Dog dog;
}

作用域【了解】

@Scope("prototype") //Spring默认是单例模式的,如果要改成多例模式,加上作用域prototype即可
@Scope("prototype")
@Component("user3")
public class User3 {

}

AOP

回看AOP注解实现

注解和XML对比

  • xml可以适用于任何场景,结构清晰,推荐使用。
  • 注解不是自己提供的类,存在局限性;好处:开发简单,方便

注解xml可以整合开发

xml和注解的最佳实践:

  • xml管理bean
  • 注解完成属性的注入
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值