Spring框架(五):spring常用注解

一、基于注解的spring ioc的使用步骤

1、创建maven工程并导入坐标

2、创建实体

package com.wedu.spring03.domain;

import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户实体
 */
@Component
public class User implements Serializable {

    private Integer id;
    private String username;
    private String address;
    private String sex;
    private Date birthday;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
package com.wedu.spring03.domain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.io.Serializable;

/**
 * 账户实体
 */
@Component("account") //创建对象
@Scope("singleton") //作用域
public class Account implements Serializable {

    @Value("1")
    private Integer id;

    @Value("46")
    private Integer uid;

    @Value("10000") //向基本类型注入数据
    private Double money;

    //@Autowired //自动按照类型注入。
    //@Qualifier("user") //在按照类中注入的基础之上再按照名称注入
    @Resource(name = "user") //直接按照bean的id注入
    private User user;

    public void printUser() {
        System.out.println(user);
    }

    @PostConstruct //用于指定初始化方法
    public void  init(){
        System.out.println("初始化方法执行了");
    }

    @PreDestroy //用于指定销毁方法
    public void  destroy(){
        System.out.println("销毁方法执行了");
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }
}

3、创建配置文件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">

    <!--告知spring在创建容器时要扫描的包-->
    <context:component-scan base-package="com.wedu.spring03.domain"/>
</beans>

4、创建测试类

package com.wedu.spring03.domain;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 基于注解的spring ioc的使用
 */
public class AccountTest {

    @Test
    public void annotationTest() {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        Account account = ac.getBean("account",Account.class);
        System.out.println(account);
        account.printUser();
        Account account2 = ac.getBean("account",Account.class);
        System.out.println(account2);
        ac.close();
    }
}

二、spring ioc常用注解说明

1、创建对象的注解

@Component

  • 把资源交给spring来管理。相当于在xml中配置一个bean。
  • value指定bean的id。如果不指定value属性,默认bean的id是当前类的类名。首字母小写。

@Controller

  • 一般用于表现层的注解,同@Component

@Service

  • 一般用于业务层的注解,同@Component

@Repository

  • 一般用于持久层的注解,同@Component

2、作用域注解

@Scope:指定作用域的范围。

3、生命周期注解

@PostConstruct

  • 指定spring容器初始化方法

@PreDestroy

  • 指定spring容器销毁方法

4、注入方式的注解

@Autowired

  • 按照类型自动注入,它只能注入其他bean类型;
  • 支持构造器注入和 set 方法注入;如果是 set 方法注入,可以将该注解添加到 set 方法前或者直接添加到属性前
  • @Autowired按照类型注入,有可能找到多个,建议和@Qualifier注解一起使用,明确指定被注入的 bean的 id。

@Qualifier

  • 使用value属性指定bean的id。
  • 在自动按照类型注入的基础之上,再按照Bean的id注入。它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。

@Resource

  • 只能注入其他bean类型;
  • 支持 set 方法的注入,使用 name 属性指定被注入的 bean 的 id。

@Value

  • 注入基本类型和String类型值的相关的注解;
  • 该属性可以添加到属性前,也可以添加到set方法前;
  • 注意,集合类型的注入只能通过XML来实现。
发布了134 篇原创文章 · 获赞 10 · 访问量 7357

猜你喜欢

转载自blog.csdn.net/yu1755128147/article/details/103555207
今日推荐