注解方式进行依赖注入

首先需要对配置bean的包进行扫描导入spring配置文件

<?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 https://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.mingyu"></context:component-scan>
</beans>

进行bean的配置:

1,@Component
可以使用此注解描述Spring中的Bean,它是一个泛化的概念,表示一个组件(Bean),可以作用在任何层次。使用时只需要将该注解标注在相应的类上即可。

2,@Repository
用于将数据访问层(DAO层)的类标识为Spring中的Bean,其功能与@Component相同。

3,@Service
用于将业务层(Service层)的类标识为Spring中的Bean,其功能与@Component相同。

4,@Controller
用于将控制层的类标识为Spring中的Bean,其功能与@Component相同

package com.mingyu.dao.impl;

import com.mingyu.dao.studentDao;
import org.springframework.stereotype.Repository;

@Repository("dao")
public class studentDaoImpl implements studentDao {
    @Override
    public void study() {
        System.out.println("dao学习");
    }
}

相关依赖进行注入:

5,@Autowired
用于对Bean的属性变量,属性的Set方法以及构造方法进行标注,配合对应的注解处理器完成Bean的自动配置工作。默认按照Bean的类型(type)进行装配。

6,@Qualifier
与@Autowired注解配合使用,会将默认的按照Bean类型注入改为按照Bean的实例名称装配,Bean的实例名称由@Qualifier注解的参数指定。

7,@Resource
作用与@Autowired一样。区别在于@Autowired默认按照Bean类型(type)注入,而@Resource默认按照Bean实例名称进行装配

@Resource 中有两个重要属性:name 和 type。

Spring 将 name 属性解析为 Bean 实例名称,type 属性解析为 Bean 实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。

如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。

以上这些是对引用值的注入,如果我们需要对常量值进行注入则采用@Value()进行注入

package com.mingyu.service.impl;

import com.mingyu.dao.impl.studentDaoImpl;
import com.mingyu.service.studentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("service")
public class studentServiceImpl implements studentService {
    @Resource(name = "dao")
    private studentDaoImpl sdi;
    @Value("徐")
    private String name;
    @Override
    public void study() {
        System.out.println("service学习");
        sdi.study();
        System.out.println("姓名:"+name);
    }
}

最后对我们的代码进行测试

package com.mingyu;

import com.mingyu.service.impl.studentServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
//初始化spring容器,加载xml配置文件,实例化bean
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        studentServiceImpl ss = (studentServiceImpl) applicationContext.getBean("service");
        //调用PersonController中的add()方法
        ss.study();
    }
}

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值