Spring注解开发

HeJin大约 2 分钟Spring全家桶Spring入门

  • @Component 放在类上,说明这个类被Spring管理了,就是bean
  • @Value 可以放在属性上,也可以放在set方法上
  • @Repository dao层
  • @Service service层
  • @Controller controller层
  • @Scope 作用域

在Spring4之后,要使用注解开发,必须要保证aop的包导入了!

使用注解需要导入contex约束,增加注解的支持!

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

    <context:annotation-config/>

</beans>

bean

@Component放在类上,说明这个类被Spring管理了,就是bean!

/**
 * @author Administrator
 * Component(组件)   等价于<bean id="user" class="com.kuang.pojo.User"/>
 */
@Component
public class User {
    public String name = "张三";
}

属性注入

@Value可以放在属性上,也可以放在set方法上

@Value("张三")
public void setName(String name) {
    this.name = name;
}
package com.kuang.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @author Administrator
 * Component(组件)等价于<bean id="user" class="com.kuang.pojo.User"/>
 */
@Component
public class User {

    /**
     * Value 相当于 <property name="name" value="张三"/>
     */
    @Value("张三")
    public String name;

    public void setName(String name) {
        this.name = name;
    }
}

衍生的注解

@Component有几个衍生注解。我们在web开发中,会按照mvc三层架构分层!

dao【@Repository】

package com.kuang.dao;

import org.springframework.stereotype.Repository;

/**
 * @author Administrator
 */
@Repository
public class UserDao {
    
}

service【@Service】

package com.kuang.service;

import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */
@Service
public class UserService {
    
}

controller【@Controller】

package com.kuang.controller;

import org.springframework.stereotype.Controller;

/**
 * @author Administrator
 */
@Controller
public class UserController {
    
}

这4个注解功能都是一样的,都是代表将某个类注册到Spring容器中,装配Bean。

自动装配

  • 使用@AutoWired我们可以不编写set方法,前提是你这个自动装配的属性在IOC(Spring)容器中存在且符合名字ByName!
  • 如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解完成的时候【@Autowired】。我们可以使用
  • @Qualifier(value = "xxx")去配合@Autowired的使用,指定一个唯一的bean对象注入!
  • @Nullable 说明这个字段可以为null。

作用域

原型模式

@Component
@Scope("prototype")
public class User {

    /**
     * Value 相当于 <property name="name" value="张三"/>
     */
    @Value("张三")
    public String name;

    public void setName(String name) {
        this.name = name;
    }
}

单例模式

@Component
@Scope("singleton")
public class User {

    /**
     * Value 相当于 <property name="name" value="张三"/>
     */
    @Value("张三")
    public String name;

    public void setName(String name) {
        this.name = name;
    }
}

小结

xml和注解

  • 1、xml更加万能,适用于任何场景!维护简单方便。
  • 2、注解不是自己类用不了,维护相对复杂。

xml与注解最佳实践

  • xml用来管理bean
  • 注解只负责完成属性的注入
  • 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解支持。
<context:annotation-config/>
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.kuang"/>