Spring Annotation

@Autowired 
Inject dependent object automatically
option) @Autowired(required=false) >> Even if property of the object doesn't exist, Spring doesn't cause Exception.
의존관계의 자동설정, 의존하는 객체를 삽입해준다.
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  or <context:annotation-config> class needs to be registered.



@Qualifier
@Autowired can cause error when more than two beans are identical. To prevent it, @Qualifier appoints one bean to be used.
If in Bean configuration,
<bean id="cafe" class="com.spring.cafe" autowired="byType"></bean>
<bean id="coffee" class="com.spring.cafe.Coffee"></bean>
<bean id="coffee2" class="com.spring.cafe.Coffee"><qualifier value="main"/></bean> 


public class UserService {
    @Autowired
    @Qualifier("main")
    private Coffee coffee;

    public String result() {
        return blabla;
    }
}
coffee and coffee2 are identical each other, so it's supposed to cause exception, but it won't due to '<qualifier value="main"/>' and '@Qualifier'. Qualifier will help to inject 'coffee2' automatically.






@Resource
is similar to @Autowired. While @Autowired is byType, @Resource is connected by name.
Configuration must have <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>, and Class must have  @Resource(name="NAME")

<beans> 
    <context:annotation-config/>
    <bean id="user2" class="com.test.UserImpl" p:data="65536"/>
</beans>

public class UserService {
    @Resource(name="user2")
    private User user;  
    //UserImpl user2 = new UserImpl();
    //User user = user2;
 
    public void setUser(User user) {                          
        this.user = user;
    }
    public String result() {
        return user.getData();
    }
}






@Component
@Repository, @Service, @Controller are specific cases of @Component. Rather than simply using @Component, those three above is more appropriate for certain specific purpose.
 @Component
public class DeptDAOImpl implements DeptDAO {
    ...
}
1) @Repository annotation is a specialization of the @Component annotation with similar use and functionality. In addition to importing the DAOs into the DI container, it also makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException.
2) @Service annotation is also a specialization of the component annotation. It doesn’t currently provide any additional behavior over the @Component annotation, but it’s a good idea to use @Service over @Component in service-layer classes because it specifies intent better.
3) @Controller annotation marks a class as a Spring Web MVC controller. It too is a @Component specialization, so beans marked with it are automatically imported into the DI container. When you add the @Controller annotation to a class, you can use another annotation i.e. @RequestMapping; to map URLs to instance methods of a class.







@Required

1) put the annotiation in the class
import org.springframework.beans.factory.annotation.Required
public class Test {
    private TestDao testDao;

    @Required
    public void setTestDao(TestDao testDao) {
        this.testDao = testDao;
    }
}
2) register the class as Bean in configuration file
<bean class="org.springframework.beans.factory.annotaion.RequiredAnnotationBeanpostProcessor"/>
<bean name="testBean"  class="lja.test.TestBean"><property name="testDao" ref="testDao"/>  </bean>
RequiredAnnotationBeanPostProcessor Class checks bean's property. If @Required doesn't have <property>. it will cause error.




Previous
Next Post »