美文网首页
Spring Annotation

Spring Annotation

作者: ibyr | 来源:发表于2016-09-13 11:33 被阅读20次
    1. Special Annotation to mark beans provided by Spring.

    @Component: To mark a class of ordinary Spring bean.
    @Controller: To mark a class of controller component.
    @Service: To mark a class of logic service. (usually under service package.)
    @Repository: To mark a class of DAO component. (Usually under xxxDAO package.)

    @Scope: To assign the scope for bean. @Scope("prototype") @Scope("singleton")


    So you do not need to configurate them in Spring config file of applicationContext.xml.
    But you should designate the path to search the beans in applicationContext.xml.

    <!-- auto to san all the beans of the package and son-packages. -->
    <context:conponent-scan base-package="com.bupt.huang"/>
    

    Or you can also add elements like<include-filter>, <exclude-filter>.

    <context:component-scan base-package="com.bupt.huang">
        <!-- All the classes end with Chinese will be included. -->
        <context:include-filter type="regex" expression=".*Chinese"/>
        <!-- All the classes end with Axe will be excluded. -->
        <context:exclude-filter type="regex" expression=".*Axe"/>
    </context:component-scan>
    
        <!-- 
    type: to point out the filter type. There 4 kind of types:
        annotation, regex, assignable(to specify a class name), aspectj.
         -->
    
    

    4 kinds of Annotation.

    //Or you can mark the name of the bean instance. @Component("chinese")
    @Componnet
    public class Chinese implements Person{
        private int year;   // class attributes.
        private Axe axe; // IoC, or dependency injection. Set way. Anther way is by constructor.
        public void setAxe(Axe axe){
            this.axe = axe;
        }
        /** skip other methods. */
    }
    
    @Repository
    public class UseDAO{
        /** skip. */
    }
    
    @Service
    public class UserService{
        /** skip. */
    }
    
    @Controller
    public class LoginController {
        /** skip. */
    }
    

    @Resource(name="stoneAxe")
    @PostConstruct
    @PreDestroy

    @Component
    public class Chinese implements Person {
        // If you use @Resource annotation, then you don't need to realize setXXX() method.
        @Resource(name="steelAxe")  
        private Axe axe;
        public void useAxe(){
            System.out.println(axe.chop());
        }
        @PostConstruct
        public void init(){
            System.out.println("method before init bean..");
       }
        @PreDestroy
        public void close(){
            System.out.println("method before destroy bean.");
        }
    }
    

    @DependsOn({"aBean", "bBean"}) //to modify a class or method.
    @Lazy(true) //true to not pre-init a bean class when Spring container inits.
    @AutoWired // auto search and load to setXXX().
    @Qualifer("student") // student is the name of bean instance.

    相关文章

      网友评论

          本文标题:Spring Annotation

          本文链接:https://www.haomeiwen.com/subject/mxemettx.html