spring 基于注解管理bean @Autowired

2023-12-16 23:39:26

目录结构:?

代码示例:

UserContronller.java

package com.atguigu.spring6.autowired.controller;

import com.atguigu.spring6.autowired.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserContronller {
    //注入Service  属性注入
    @Autowired//根据类型找到对应对象,完成注入
    private UserService userService;

    public void add(){
        System.out.println("controller");
        userService.add();
    }
}

?

?UserDao.interface

package com.atguigu.spring6.autowired.dao;

public interface UserDao {
    public void add();
}

UserdaoImpl.java

package com.atguigu.spring6.autowired.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {


    @Override
    public void add() {
        System.out.println("dao");
    }
}

UserService.interface

package com.atguigu.spring6.autowired.service;

public interface UserService {
    public void add();
}

?UserServiceImpl.java

package com.atguigu.spring6.autowired.service;

import com.atguigu.spring6.autowired.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
//    注入dao
    @Autowired
    private UserDao userDao;

    @Override
    public void add() {
        System.out.println("service");
        userDao.add();
    }
}

bean.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-3.0.xsd
    http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启组件扫描功能-->
    <context:component-scan base-package="com.atguigu.spring6"></context:component-scan>

</beans>

UserTest.java

package com.atguigu.spring6.autowired;

import com.atguigu.spring6.autowired.controller.UserContronller;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {
    @Test
    public void test(){
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        UserContronller userContronller = ac.getBean(UserContronller.class);
        userContronller.add();
    }
}

控制台输出

?此时Dao包下新增UserDao接口的实现类 UserRedisImpl.java

package com.atguigu.spring6.autowired.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

@Repository
public class UserRedisImpl implements UserDao{

    @Autowired
    private UserRedisImpl userRedis;
    @Override
    public void add() {
        System.out.println("Redis dao");
    }
}

运行报错:?

解决方法

?

文章来源:https://blog.csdn.net/qq_64005599/article/details/135039244
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。