关于小白对SSM框架的学习和理解Day02-Spring框架的注解

上期博客讲到spring的注入方式,这期关于通过注解来注入spring,不清楚的小伙伴可以回去查看上一期内容https://blog.csdn.net/qq_41356363/article/details/80561815

1.关于注解配置

环境搭建所需jar包
这里写图片描述

接着在applicationContext.xml配置文档中,开启注解扫描
注意:base-package="com.shaw.annotation"是扫描的包的位置,我的目录结构参考下图,小伙伴根据自己创建的包进行更改
这里写图片描述

<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" 
    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
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

        <!-- 开启spring注解扫描 -->
        <context:component-scan base-package="com.shaw.annotation"></context:component-scan>
</beans>

接着创建实体类Car ,标注@Component("car")
其他类别的标注

package com.shaw.annotation.entity;

import org.springframework.stereotype.Component;

@Component("car")
// 相当于<bean name="car" class="com.shaw.annotation.entity.Car"></bean>
public class Car {
    private String name;
    private Integer price;

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

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + ", price=" + price + "]";
    }

}

再创建测试类

package com.shaw.annotation.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.shaw.annotation.entity.Car;

//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
// 相当于ApplicationContext ac = new
// ClassPathXmlApplicationContext("applicationContext.xml");
public class SecondTestSpring {

    @Resource(name = "car")
    //指定name为car的实体类注入当前对象
    private Car car;

    @Test
    public void test() {
        System.out.println(car);
    }
}

测试结果:
Car [name=null, price=null]

2.在类中使用注解

@Component("car") // 适用于所有层
@Service("car") // 适用于service层
@Repository("car") // 适用于持久层
@Controller("car") // 适用于controller层
@scope(scopeName="singleton")//标注单例
@scope(scopeName="prototype")//标注多例

3.使用注解注入属性

使用@value标注在私有的成员变量注入

package com.shaw.annotation.entity;

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

@Component("car")
// 相当于<bean name="car" class="com.shaw.annotation.entity.Car"></bean>
public class Car {
    @Value("宝马")
    private String name;
    @Value("100")
    private Integer price;

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

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + ", price=" + price + "]";
    }

}

打印结果:
Car [name=宝马, price=100]

使用@value在setting方法上注入

package com.shaw.annotation.entity;

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

@Component("car")
// 相当于<bean name="car" class="com.shaw.annotation.entity.Car"></bean>
public class Car {

    private String name;

    private Integer price;

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

    @Value("100")
    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + ", price=" + price + "]";
    }

}

输出结果:
Car [name=宝马, price=100]

扫描二维码关注公众号,回复: 1604758 查看本文章

4.使用注解进行装配

按照名称进行装配

通过@Resource(name = "car")装配 @Component("car") 指定好的name值

@Resource(name = "car")
    private Car car;

按照类型进行自动装配

通过@Autowired按照标注了@Component的实体类进行装配,可以省略后面的括号内容,因为是按照成员变量中指定的类型来装配。但是如果需要装配的实体类有多个相同类型的,可能会造成错误,这时通过@Qualifier("car")来指定名称进行装配

    @Autowired
    private Car car;
    @Autowired
    @Qualifier("car")
    private Car car;

5.使用注解配置初始方法和销毁方法

在Car实体类方法下指定方法上配置@PostConstruct,实体类会在创建时执行,称为初始化方法

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

执行结果:
Car被初始化
Car [name=宝马, price=100]

在Car实体类方法下指定方法上配置@PreDestroy,实体类会在销毁时执行,称为销毁方法

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

执行结果:
Car [name=宝马, price=100]
Car被销毁

猜你喜欢

转载自blog.csdn.net/qq_41356363/article/details/80568364