其他分享
首页 > 其他分享> > Spring依赖注入的注解方式

Spring依赖注入的注解方式

作者:互联网

使用注解注入

环境准备

<!--告知spring在创建容器时扫描的包-->
<context:component-scan base-package="com.jz"/>

1、用于创建对象

相当于xml配置中的bean标签

实验:

@Component(value = "accountService")
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao){

        this.accountDao = accountDao;
    }

    public void saveAccount() {

        accountDao.saveAccount();
    }
}

测试类

public class Client {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as = (AccountService) context.getBean("accountService");
        System.out.println(as);
    }
}

运行结果:

2、用于注入数据

相当于bean标签里面的property

实验:

bean1

@Repository("accountDao1")
public class AccountDaoImpl implements AccountDao {

    public void saveAccount() {

        System.out.println("保存账户。。。1。111");
    }
}

bean2

@Repository("accountDao2")
public class AccountDaoImpl2 implements AccountDao {

    public void saveAccount() {

        System.out.println("保存账户。。。。22222");
    }
}

service类

@Component("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao1 = null; // 此时有多个bean的类型相同,所以要改属性名

    public void saveAccount() {

        accountDao1.saveAccount();
    }
}

测试类

public class Client {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as = (AccountService) context.getBean("accountService");
        as.saveAccount();
    }
}

运行结果:

实验:

只需修改@Autowried实验中的service类

@Component("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    @Qualifier("accountDao2") // 加上它之后就不需要再改属性名了
    private AccountDao accountDao = null;

    public void saveAccount() {

        accountDao.saveAccount();
    }
}

运行结果:

实验:

只需修改@Autowired中service类

@Component("accountService")
public class AccountServiceImpl implements AccountService {

    /*@Autowired
    @Qualifier("accountDao2")*/

    @Resource(name = "accountDao1")
    private AccountDao accountDao = null;

    public void saveAccount() {

        accountDao.saveAccount();
    }
}

运行结果:

3、用于改变作用范围

相当于bean标签中的scope

实验:

Service类

@Component("accountService")
@Scope("prototype")
public class AccountServiceImpl implements AccountService {

    /*@Autowired
    @Qualifier("accountDao2")*/

    @Resource(name = "accountDao1")
    private AccountDao accountDao = null;

    public void saveAccount() {

        accountDao.saveAccount();
    }
}

实验类:

public class Client {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as1 = (AccountService) context.getBean("accountService");
        AccountService as2 = (AccountService) context.getBean("accountService");

        System.out.println(as1 == as2);
    }
}

运行结果:

4、和生命周期相关

相当于bean标签中使用的init-method 和 destroy-method的作用一样

实验:

service类

@Component("accountService")
@Scope("singleton")
public class AccountServiceImpl implements AccountService {


    @Resource(name = "accountDao1")
    private AccountDao accountDao = null;

    public void saveAccount() {

        accountDao.saveAccount();
    }

    @PostConstruct()
    public void init(){

        System.out.println("初始化方法执行了。。。");
    }

    @PreDestroy
    public void destroy(){

        System.out.println("销毁方法执行了。。。。");
    }
}

测试类:

public class Client {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as1 = (AccountService) context.getBean("accountService");
        as1.saveAccount();
        context.close();
    }
}

运行结果:

标签:依赖,accountDao,saveAccount,AccountService,Spring,void,bean,注解,public
来源: https://www.cnblogs.com/jianzha/p/12611071.html