4-2 Spring Bean装配之Autowired注解说明-1

Required 不常用

  • Required注解适用于bean属性的setter方法
  • 这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在bean定义或通过自动装配一个明确的属性值
  • Required 这整个应用中并不常用
public class SimpleMovieLister {
    private MovieFinder movieFinder;
    @Required
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}

Autowired 常用

  • 可以将@Autowired注解为“传统”的setter方法
public class SimpleMovieLister {
    private MovieFinder movieFinder;
    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}
  • 可用于构造器或成员变量
public class SimpleMovieLister {
    @Autowired
    private MovieFinder movieFinder;
    private CustomerPreferenceDao customerPreferenceDao;
    @Autowired
    public SimpleMovieLister(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }
}
  • 默认情况下,如果因找不到合适的bean将会导致autowiring失败抛出异常,可以通过required = false方式避免
public class SimpleMovieLister {
    private MovieFinder movieFinder;
    private CustomerPreferenceDao customerPreferenceDao;
    @Autowired(required = false)
    public SimpleMovieLister(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }
}
  • 每个类只能有一个构造器被标记为required=true
  • @Autowired的必要属性,建议使用@Required注解

例子:

在这里插入图片描述

package com.torey.beanannotation.injection.dao;

/**
 * @ClassName:InjectionDao
 * @Description:
 * @author: Torey
 */
public interface InjectionDao {
    public void save(String arg);
}
package com.torey.beanannotation.injection.dao;

import org.springframework.stereotype.Repository;

/**
 * @ClassName:InjectionDaoImpl
 * @Description:
 * @author: Torey
 */
@Repository
public class InjectionDaoImpl implements InjectionDao {
    public void save(String arg){
        System.out.println("模拟数据添加:" + arg);
    }
}

package com.torey.beanannotation.injection.service;

import org.springframework.util.StringUtils;

/**
 * @ClassName:InjectionService
 * @Description:
 * @author: Torey
 */
public interface InjectionService {
    void save(String arg);
}

package com.torey.beanannotation.injection.service;

import com.torey.beanannotation.injection.dao.InjectionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName:InjectionServiceImpl
 * @Description:
 * @author: Torey
 */
@Service
public class InjectionServiceImpl implements InjectionService {
   // @Autowired
    private InjectionDao injectionDao;
    @Autowired
    public InjectionServiceImpl(InjectionDao injectionDao) {
        this.injectionDao = injectionDao;
    }

    //@Autowired
    public void setInjectionDao(InjectionDao injectionDao) {
        this.injectionDao = injectionDao;
    }

    @Override
    public void save(String arg) {
        System.out.println("Service接收的参数为:" + arg);
        arg=arg+":"+this.hashCode();
        injectionDao.save(arg);
    }
}

<?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 http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.torey.beanannotation"/>
</beans>
package com.torey.springtest.base;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

/**
 * @ClassName:UnitTestBase
 * @Description:
 * @author: Torey
 */
public class UnitTestBase {
    public UnitTestBase(){}
    private String springXmlPath;
    private ClassPathXmlApplicationContext applicationContext;
    public UnitTestBase(String springXmlpath){
        this.springXmlPath=springXmlpath;
    }
    @Before
    public void before(){
        if (StringUtils.isEmpty(springXmlPath)) {
            springXmlPath="classpath*:spring-*.xml";
        }
        applicationContext= new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]"));
        applicationContext.start();
    }
    @After
   public void after(){
        applicationContext.destroy();
   }
   public Object getBean(String beanId){
       return applicationContext.getBean(beanId);
   }
}

package com.torey.springtest.bean;

import com.torey.beanannotation.BeanAnnotation;
import com.torey.beanannotation.injection.service.InjectionService;
import com.torey.springtest.base.UnitTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import java.io.IOException;

/**
 * @ClassName:TestResource
 * @Description:
 * @author: Torey
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
    public TestBeanAnnotation(){
        super("classpath:spring-beanannotation.xml");
    }
    @Test
    public void testAutowired(){
        InjectionService inje= (InjectionService)super.getBean("injectionServiceImpl");
        inje.save("this is testAutowired");
    }

}

根据慕课网 moocer老师的spring课程 整理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值