Spring之实现IOC的注解开发

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

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

注解开发配置格式

相比于xml开发,注解开发的配置文件做了以下几点改变:
1.导入约束:context约束
2. 配置注解的支持<context:annotation-config/>

<?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.dps"/>
    <context:annotation-config/>

</beans>

注解使用

@Autowired(重点掌握)

  1. 直接使用在属性上即可!也可以在set方式上使用。
public class People {
	@Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
}
  • 这样去写的话,@Autowired就默认取代了在xml中People类bean的property属性里注入Cat类和Dog类的bean的id的过程
  • 注意:直接写在属性上就不用去写set方法了。
  • 另外,这个自动装配的属性(用@Autowired修饰的属性)在IOC容器中要存在。且符合名字byname(自定义类型的变量名等于该类型bean的id)!
  1. 如果@Autowired自动装配的环境比较复杂(比如xml中有多个同类型的bean,且该类型各个bean的id均不等于该自定义类型的变量名此时@Autowired既不能通过类型去匹配,又不能通过id名去匹配,就会出问题),自动装配无法通过一个注解【@Autowired】完成的时候,可以使用 @Qualifier(value = “xxx”)配合@Autowired使用,指定一个唯一的bean(@Qualifier的valve属性值,指的就是对应bean的id)对象注入。
	@Autowired
    @Qualifier(value = "dog1")
    private Dog dog;

@Autowired补充

  1. @Autowired的required属性。如果显式定义了Autowired的required属性为false(required默认为true),说明这个对象可以为null,否则不允许为空。(几乎用不上的东西
@Autowired(required = false)
    private Cat cat;
  1. @Nullable。该注解放入参数列表中,参数可以为空(说白了就是可以选择性的不设置某些参数)。字段标记了这个注解,这个字段就可以为空
//实体类的构造器
public People(@Nullable String name,@Nullable int a){
   this.name = name;
}
//不加@Nullable时xml的内容必须这样写:
<bean id="people" class="pojo.People">
     <constructor-arg name="a" value="66"/>
     <constructor-arg name="name" value="窦哥"/>
</bean>
//加了@Nullable后xml可以这样写(class值会报红,但是可运行):
<bean id="people" class="pojo.People"/>

@Resources

@Resources(name= “xxx”)。功能上涵盖以上两种注解。先通过bean的id去xml注解中寻找注入的对象,如果找不到或者注解后没加括号,就通过类型去找。

@Autowired和@Resources的区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @ Autowired通过byType的方式实现,而且必须要求这个对象存在!
  • @ Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现! 如果两个都找不到的情况下,就报错!
  • 执行顺序不同: @ Autowired通过byType的方式实现。@ Resource默认通过byname的方式实现。

注解开发

  • 在Spring4之后,要使用注解开发,必须保证aop的包导入了。
  • 使用注解需要导入context约束,增加注解的支持。
  • 即整体的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.dps"/>
    <context:annotation-config/>

</beans>

注解替换bean配置

  • bean注入@Component注解,用在类上,等价于<bean id="" class=""/>

@Component的衍生注解(web开发中,会按照MVC三层架构分层):
dao层:@Repository
service层:@Service
controller层:@Controller

这三个注解同@Component注解作用一模一样,都是装配bean,名字不同仅仅为了规范的区分不同的层。

  • 属性注入@Value,用在属性或属性的set方法上。@Value("") 等价于<property name="" value=""/>
  • @scope,用在类上。@scope(“singleton”)单例模式@scope(“prototype”)原型模式。(简单理解为单例就是生产一个对象原型是生产多个对象

xml与注解的对比

  • xml更加万能,适用于任何场合维护方便
  • 注解的话,不是自己的类使用不了,维护相对复杂
  • 最佳实践是,xml用来管理bean注解只负责完成属性的注入。简单来说就是xml写个类的bean空壳,具体属性注入让注解来完成。

彻底不使用xml配置

使用java的方式配置Spring,取代xml配置。

  • 其实就是个配置类,这种配置类一般写在config包下。在这里插入图片描述
  • 配置类长什么样?里面的注解都是干嘛的。在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值