SSM-Spring-07:Spring基于注解的di注入

简介: ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------     注解:   说起注解,哇哦,每个人都或多或少的用到过   像什么@Overried,@Test,@Param等等之前就早已熟悉的注解,现在要用注解实现di的注入   注解的本质是什么?...

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

注解:

  说起注解,哇哦,每个人都或多或少的用到过

  像什么@Overried,@Test,@Param等等之前就早已熟悉的注解,现在要用注解实现di的注入

  注解的本质是什么?就是一个接口,他里面的参数是什么呢?就是这个接口里面的方法,so,我们怎么做?

案例如下:

  基于注解的jar包就不用说了,按照之前的博客走下来的无需再添加新的jar包

  还是俩个类,一个car,一个student,学生有一辆小汽车,基于注解的di注入

    Car类

package cn.dawn.day07annotationdi;

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

/**
 * Created by Dawn on 2018/3/5.
 */
@Component("car")
public class Car {
    @Value("红色")
    private String color;
    @Value("奔驰")
    private String type;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

    @Componed("car")  //表示这个生成的对象的名字

    @Value("奔驰")    //用于给属性赋值

  Student类

package cn.dawn.day07annotationdi;


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

import javax.annotation.Resource;

/**
 * Created by Dawn on 2018/3/5.
 */
//student类
    @Component("student")
public class Student {
        @Value("老胡小子,呵呵哒")
    private String name;
        @Value("20")
    private Integer age;

        //@Resource(name = "car")
    @Autowired
    @Qualifier(value = "car")
    private Car car;

    //带参构造
    public Student(String name, Integer age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

    //无参构造
    public Student() {
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
}

    一样的就不做解释了,说一下下面这几个

    @Resource(name="car")    //这个是javax包下的注解,可以实现域属性的注入,下面还有一种方式,

 

    @AutoWried

    @Qualifier(value="car")    //这两行联用,他是spring的注解,也是给对象的域属性赋值

  

  在Spring的配置文件中,需要配置一点内容,首先导入命名空间context,和注解的包扫描器(我是idea,写完下面的节点,上面的命名空间自动生成)

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" 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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="cn.dawn.day07annotationdi">
    </context:component-scan>

</beans>

  单测方法

package cn.dawn.day07annotationdi;


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

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180306 {


    @Test
    /*di注解注入*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day07annotationdi.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("学生"+student.getName()+""+student.getCar().getType());
    }
}

 

目录
打赏
0
0
0
0
1030
分享
相关文章
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
121 26
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
189 89
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
185 73
|
19天前
|
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
55 16
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
67 21
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
一键注入 Spring 成员变量,顺序编程
【Spring配置】idea编码格式导致注解汉字无法保存
问题一:对于同一个项目,我们在使用idea的过程中,使用汉字注解完后,再打开该项目,汉字变成乱码问题二:本来a项目中,汉字注解调试好了,没有乱码了,但是创建出来的新的项目,写的注解又成乱码了。
Spring MVC中的请求映射:@RequestMapping注解深度解析
在Spring MVC框架中,`@RequestMapping`注解是实现请求映射的关键,它将HTTP请求映射到相应的处理器方法上。本文将深入探讨`@RequestMapping`注解的工作原理、使用方法以及最佳实践,为开发者提供一份详尽的技术干货。
271 2
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等