Autowired 메모 사용

3504 단어 J2EE
1. Autowired는 @Autowired를 전통적인 setter 방법으로 주석할 수도 있고, 구조기나 구성원 변수에 사용할 수도 있다. 기본적으로 적당한 bean을 찾지 못하면 aotowired가 실패하거나 이상을 던질 수도 있지만, 아래 코드를 통해 피할 수 있다.
public class SimpleMovieLister{
    private MovieFinder movieFinder;
    @Autowired(required=flase)
    public void setMovieFinder(MovieFinder movie){
        this.movieFinder = movie;
    }
}

클래스마다 하나의 구조기만 Required =true로 표시됩니다.spring의 공식 문서에서 필요한 속성이라면 @required 주석을 사용하십시오
2. @Autowired의 주석을 통해list 집합이나 맵 집합을 사용하여 이 클래스의 자원을 출력하거나 사용할 수 있습니다.
public interface BeanInterface {
//       
}

@Order(2)//         
@Component
public class One_interface implements BeanInterface{
    //        
}

@Order(1)
@Component
public class Two_interface implements BeanInterface{
    //        
}

@Component
public class BeanInvoker {
    @Autowired
    private List list;

    public void say(){
        if(list != null && list.size() != 0)
            for (BeanInterface beanInterface : list) {
                System.out.println(beanInterface.getClass().getName());
            }
        else
            System.out.println("not null");
    }
}

@RunWith(BlockJUnit4ClassRunner.class)
public class TestInterface {
    public TestInterface() {}
    @Test
    public void say(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-interface.xml");
        BeanInvoker beaninvoker = ctx.getBean("beanInvoker", BeanInvoker.class);
        beaninvoker.say();
    }
}

//    xml     

/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">
        
        ">
 

좋은 웹페이지 즐겨찾기