使用Spring的注解方式注入属性

使用Spring的注解方式注入属性

@Component 描述Spring框架中的Bean,除了 @Component 外Spring提供了3个功能基本等效的注解:

  • @Repository 用于对DAO实现类进行标注
  • @Service 用于对Service实现类进行标注
  • @Controller 用于对Controller实现类进行标注

@Value 进行属性注入
@Autowired 注入其他Bean默认按属性进行,有两个相同类型的Bean,按名称注入
使用注解注入属性时,可以在属性上使用也可以在Setter上使用
@AutoWired 使用required属性,设置一定要找到匹配的Bean。
@Qualifier 指定注入Bean的名称与 @Autowired 一起使用
@Resource相当于同时使用 @Autowired 和 @Qualifier,使用时需导入JSR-250包
@PostConstruct 当Bean被初始化时调用
@PreDestroy 当Bean被销毁时调用
@Scope 注解用于指定Bean的作用范围,默认singleton

以下通过一个简单示例测试注解注入

目录结构

annotation
- src
- - main
- - - java
- - - - com.zhangxin9727
- - - - - Persion.java
- - - - - Cat.java
- - - - - Dog.java
- - - resources
- - - - applicationContext.xml
- - test
- - - java
- - - - MyTest.java
- pom.xml

详细代码

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <groupId>com.zhangxin9727</groupId>
    <artifactId>annotation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
</project>
Person.java
package com.zhangxin9727;

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;

@Component("person")
@Scope("singleton")
public class Person {
    @Value("Alice")
    private String name;
    @Value("16")
    private int age;
    @Resource(name = "cat")
    private Cat cat;
    @Autowired
    @Qualifier("dog")
    private Dog dog;

    @PostConstruct
    public void init() {
        System.out.println("初始化···");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("销毁中···");
    }

    @Override
    public String toString() {
        return "Person{" + "name='" + name + '\'' + ", age=" + age + ", cat=" + cat + ", dog=" + dog + '}';
    }
}
Cat.java
package com.zhangxin9727;

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

@Component("cat")
@Scope("prototype")
public class Cat {
    private String name;

    @Value("Kitty")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat{" + "name='" + name + '\'' + '}';
    }
}
Dog.java
package com.zhangxin9727;

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

@Component("dog")
@Scope("prototype")
public class Dog {
    private String name;

    @Value("Bob")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dog{" + "name='" + name + '\'' + '}';
    }
}
applicationContext.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.zhangxin9727"/>
</beans>
MyTest.java
import com.zhangxin9727.Person;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
        ((ClassPathXmlApplicationContext) context).close();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值