Spring基于注解方式实现属性注入(超详细)

简介: Spring基于注解方式实现属性注入(超详细)

image.png

上面的三种是注入对象的,@Value是注入普通类型属性的(如String)。


首先需要开启开启组件扫描


bean1.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.Keafmd"></context:component-scan>
</beans>


注入对象类型属性


需求:我们通过注解的方式在UserService调用UserDaoImpl的方法。


第一步:把service和dao对象创建,在service和dao类添加创建对象的注解。

第二步:在service注入dao对象。


UserDao接口:


package com.Keafmd.spring5.dao;
/**
 * Keafmd
 *
 * @ClassName: UserDao
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-17 15:39
 */
public interface UserDao {
    public void add();
}

UserDaoImpl实现类:


package com.Keafmd.spring5.dao;
import org.springframework.stereotype.Repository;
/**
 * Keafmd
 *
 * @ClassName: UserDaoImpl
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-17 15:39
 */
@Repository("userDaoImpl01")
public class UserDaoImpl implements UserDao{
    @Override
    public void add() {
        System.out.println("dao add ...");
    }
}

UserService类:


package com.Keafmd.spring5.service;
import com.Keafmd.spring5.dao.UserDao;
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.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * Keafmd
 *
 * @ClassName: UserService
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-17 13:15
 */
//在注解里的value值可以不写,默认就是类的首字母小写 userService
// @Component(value = "userService")  //<bean id="userService" class=".."/>
@Service
public class UserService {
    //定义dao类型属性 ,不需要添加set方法了
    //添加注入属性的注解
   /* @Autowired
    @Qualifier(value = "userDaoImpl01")  // 要和@AutoWired一起使用 ,填写value值是为了解决有多个实现类,根据类型的话就没法区分
    private UserDao userDao;*/
//    @Resource  //根据类型注入
    @Resource(name = "userDaoImpl01")  // 根据名称注入
    private UserDao userDao;
    public void add(){
        System.out.println("service add......");
        userDao.add();
    }
}

测试类:


package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Keafmd
 *
 * @ClassName: TestSpring5Demo1
 * @Description: 测试类
 * @author: 牛哄哄的柯南
 * @date: 2021-01-17 13:03
 */
public class TestSpring5Demo1 {
    @Test
    public void testService(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }
}

测试结果:


com.Keafmd.spring5.service.UserService@7ea37dbf
service add......
dao add ...
Process finished with exit code 0

service和dao中的内容都输出了,证明基于注解方式实现对象属性的注入就成功了。


注入普通类型属性


修改UserService类,添加一个String类型的属性,并使用@Value注解对普通类型的属性进行注入,在添加一个输出语句方便测试。


UserService类:


package com.Keafmd.spring5.service;
import com.Keafmd.spring5.dao.UserDao;
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.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * Keafmd
 *
 * @ClassName: UserService
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-01-17 13:15
 */
//在注解里的value值可以不写,默认就是类的首字母小写 userService
// @Component(value = "userService")  //<bean id="userService" class=".."/>
@Service
public class UserService {
    @Value(value = "Keafmd")
    private String name;
    //定义dao类型属性 ,不需要添加set方法了
    //添加注入属性的注解
   /* @Autowired
    @Qualifier(value = "userDaoImpl01")  // 要和@AutoWired一起使用 ,填写value值是为了解决有多个实现类,根据类型的话就没法区分
    private UserDao userDao;*/
//    @Resource  //根据类型注入
    @Resource(name = "userDaoImpl01")  // 根据名称注入
    private UserDao userDao;
    public void add(){
        System.out.println("name:"+name);
        System.out.println("service add......");
        userDao.add();
    }
}

其他类和测试类不变。

测试结果:


com.Keafmd.spring5.service.UserService@7ea37dbf
name:Keafmd
service add......
dao add ...
Process finished with exit code 0

输出了Keafmd就证明成功的使用注解的方式注入了name属性。


以上就完成了基于注解方式实现属性注入。


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

你好,我是AI助理

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