Spring学习(6)——使用注解开发

说明

  • 使用注解形式,必须得要引入aop的依赖包
  • 在配置文件当中,还得要引入一个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">

    <context:annotation-config/>
</beans>

bean的实现

  • 之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解
  1. 配置扫描哪些包下的注解
<!--指定注解扫描包-->
<?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="site.duing.pojo"/>
	<context:annotation-config/>
</beans>
  1. 在指定包下编写类,增加注解
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component("user")
public class User {
    private String name = "孙悟空";

	public String getName() {
        return name;
    }
}
  1. 测试
@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    User user = context.getBean("user", User.class);
    System.out.println(user.getName());
}

在这里插入图片描述

属性注入

  • 同样是使用注解进行注入
  1. 可以不用提供set方法,直接在直接名上添加@value(“值”)
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component("user")
public class User {
	//相当于配置文件中 <property name="name" value="孙悟空"/>
    @Value("孙悟空")
    private String name;

	public String getName() {
        return name;
    }
}
  1. 如果提供了set方法,在set方法上添加@value(“值”)
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component("user")
public class User {
    private String name;
    
    //相当于配置文件中 <property name="name" value="孙悟空"/>
    @Value("猪八戒")
    public void setName(String name) {
        this.name = name;
    }

	public String getName() {
        return name;
    }
}
  1. 测试结果不变

衍生注解

  • @Component三个衍生注解:为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样
    1. @Controller:controller层
    2. @Service:service层
    3. @Repository:dao层
  • 写上这些注解,就相当于将这个类交给Spring管理装配了

作用域

  • @Scope
    • singleton:单例模式,默认的。关闭工厂 ,所有的对象都会销毁
    • prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Component("user")
@Scope("singleton")
public class User {
    @Value("孙悟空")
    private String name;

    public String getName() {
        return name;
    }
}

小结

  • XML与注解比较
    • XML可以适用任何场景 ,结构清晰,维护方便
    • 注解不是自己提供的类使用不了,开发简单方便
  • xml与注解整合开发 :推荐最佳实践
    • xml管理Bean
    • 注解完成属性注入
    • 使用过程中,必须让注解生效,就需要开启注解的支持
    <!--指定注解扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="site.duing.pojo"/>
    <context:annotation-config/>
    

下一篇:《Spring学习(7)——代理模式》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值