Spring-IoC-DI-基于注解方式的依赖注入-(案例二:基于注解方式实现属性注入)@Resource根据属性类型或名称

(1)

package com.orzjiangxiaoyu.spring.test1;

/**
 * @author orz
 * @create 2020-08-16 20:55
 */
public interface UserDao {
    public void  add();
}
package com.orzjiangxiaoyu.spring.test1;

import org.springframework.stereotype.Repository;

/**
 * @author orz
 * @create 2020-08-16 20:55
 */
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("userDaoImpl1 add");
    }
}
package com.orzjiangxiaoyu.spring.test1;

import org.springframework.stereotype.Repository;

/**
 * @author orz
 * @create 2020-08-16 20:55
 */
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("userDaoImpl1 add");
    }
}
package com.orzjiangxiaoyu.spring.test1;

import org.springframework.stereotype.Repository;

/**
 * @author orz
 * @create 2020-08-16 21:17
 */
@Repository(value = "userDaoImpl2")
public class UserDaoImpl2 implements UserDao{
    @Override
    public void add() {
        System.out.println("userDaoImpl2  add");
    }
}
package com.orzjiangxiaoyu.spring.test1;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/**
 * @author orz
 * @create 2020-08-16 22:29
 */
@Repository
public class Userdao2 {
    public void add()
    {
        System.out.println("userdao2 add");
    }
}
package com.orzjiangxiaoyu.spring.test1;

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

import javax.annotation.Resource;

/**
 * @author orz
 * @create 2020-08-16 17:27
 */
//注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
//UserService-userService

@Service(value = "userService") //
public class UserService {

    //根据名称进行注入
    @Resource(name = "userDaoImpl1")
    private UserDao dao;

    @Resource(name = "userDaoImpl2")
    private UserDao dao2;

    //根据类型进行注入
    @Resource
    private Userdao2 userdao2;

    public void add()
    {

        System.out.println("service add ...");
        dao.add();
        dao2.add();
        userdao2.add();
    }
}

(2)



       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    
    
    package="com.orzjiangxiaoyu.spring">
    

(3)

package com.orzjiangxiaoyu.spring.test1;

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


/**
 * @author orz
 * @create 2020-08-16 17:06
 */
public class Test1 {
    @Test
    public void test1()
    {
        //1.加载spring配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置文件的创建对象
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();

    }
}

(4)

service add ...
userDaoImpl1 add
userDaoImpl2  add
userdao2 add

 

你可能感兴趣的:(Spring-IoC-DI-基于注解方式的依赖注入-(案例二:基于注解方式实现属性注入)@Resource根据属性类型或名称)