spring(3)

 

 

package com.cn.pojo;

import org.springframework.stereotype.Component;

@Component
public class Book {

}
package com.cn.dao;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository
@Scope("prototype")
public class BookDao {

}
package com.cn.dao;

import org.springframework.stereotype.Repository;

@Repository
public class BookExt extends BookDao{

}
package com.cn.controller;

import org.springframework.stereotype.Controller;

@Controller
public class BookController {

}
Package com.cn.service; 

Import org.springframework.beans.factory.annotation.Autowired;
 Import org.springframework.beans.factory.annotation.Qualifier;
 Import org.springframework.stereotype.Service; 

Import com.cn.dao.BookDao ; 

@Service 
public  class the BookService { 
    
/ ** 
     * @Autowired automatically injecting a 
     *. 1, and to find the type of injection press a 
     * 2, a plurality if found, as the id attribute name followed by continued find and injecting a 
     *. 3, a plurality if found. However, as the id attribute name is not found, may be used @Qualifier ( "bookDao") annotation specifies the id to find and inject a 
     *. 4, can modify @Autowired (required = false) allows the field value is null 
     * / 
    @ Autowired (required = false  )
    @ Qualifier ("bookDaoExt" )
     Private BookDao bookDao1; 
    
    / ** 
     * @Autowired marked on the method, this method will be called after the object is created. 
     * 1, press lookup parameter type and a method for transferring calls 
     * 2, a plurality if found, followed by the parameter name as id continue to find and inject a 
     *. 3, a plurality if found. But as the id parameter name can not be found, you can use @Qualifier ( "bookDao") annotation specifies the id to find and call the 
     * 4, you can modify the @Autowired (required = false) are not allowed to call this method is not being given 
     * / 
    @Autowired (required = to false )
     public  void setBookDao (@Qualifier ( "bookExt" ) BookDao ABC) { 
        System.out.println ( ">>> BookDao come la ---" + ABC);
    

    return "BookService [bookDao=" + bookDao1 + "]";
    }
}
package com.cn.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    public void test1() throws Exception {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        /*
         * System.out.println(applicationContext.getBean("bookDao"));
         * System.out.println(applicationContext.getBean("bookDao"));
         * System.out.println(applicationContext.getBean("bookDao"));
         * 
         * 
         * System.out.println(applicationContext.getBean("book"));
         */
        System.out.println(applicationContext.getBean("bookService"));

    }

}
<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
    xmlns: the 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: // the WWW. springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd " > 


    < ! - package name scan base-package configuration bundle to be scanned is provided (its sub-packets will be scanned) use-default-filters = " false"
        When removing the package contains the default rule scan -> 
    <context: Scan-Component Base-Package = "com.cn"    > 

    <-!       exclude accordance annotation 
        <context: the exclude filter-type = "Annotation" 
            expression The = "org.springframework.stereotype.Service" /> 
        negative negative by type 

        <context: the exclude filter-type = "ASSIGNABLE" 
            expression The = "com.cn.controller.BookController" /> 
 -> 


! <- comprises according to the annotation -> 
        <-! <context: the include filter-type = " Annotation " 
            expression The =" org.springframework.stereotype.Service "/> 
 -> 

<! - comprise exclude classes
  -> 
        <! - <context:include-filter type="assignable"--
            expression="com.cn.controller.BookController" />
 -->
    </context:component-scan>
</beans>

 

Guess you like

Origin www.cnblogs.com/ywqtro/p/12263398.html