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

基于注解方式实现属性注入

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

  • 注入对象类型属性

  • 注入普通类型属性


注解说明
@AutoWired根据属性类型自动装配
@Qualifier根据属性的名称注入要和@AutoWired一起使用
@Resource可以根据类型输入也可以根据名称注入
@Value注入普通类型属性

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

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

bean1.xml:

<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>
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

注入对象类型属性

需求:我们通过注解的方式在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();
}
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

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 ...");
}
}
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

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")  //

@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();
}
}
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

测试类:

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();
}
}
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

测试结果:

com.Keafmd.spring5.service.UserService@7ea37dbf
service add......
dao add ...

Process finished with exit code 0
  • 1

  • 2

  • 3

  • 4

  • 5

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")  //

@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();
}
}
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

  • 35

  • 36

  • 37

  • 38

  • 39

  • 40

  • 41

  • 42

  • 43

  • 44

其他类和测试类不变。
测试结果:

com.Keafmd.spring5.service.UserService@7ea37dbf
name:Keafmd
service add......
dao add ...

Process finished with exit code 0
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

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

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

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]
Spring基于注解方式实现属性注入(超详细)_Spring_02

加油!

共同努力!

Keafmd