1. 程式人生 > >註解的使用 和 繼承的配置

註解的使用 和 繼承的配置

1.註解

   (1)註解就是為了說明java中的某一個部分的作用(Type); 
   (2)註解都可以用於那個部分是@Target註解起的作用;
   (3)註解可以標註在ElementType列舉類所指定的位置上;
   (4)用來解析註解的類成為註解解析器;

2.依賴注入的註解

    (1) bean.xml 匯入註解解析器 - context名稱空間,只有引用型別可以加上註解,基本型別不可以;
    (2)@Resource使用步驟:
           1) 在spring的配置檔案中匯入名稱空間

 xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd"



   2) 引入註解解析器
 <context:annotation-config/>


   3) 在spring的配置檔案中把bean引入進來
<bean id="person_di" class="cn.labelnet.di.Person">
	</bean>

	<bean id="student_di" class="cn.labelnet.di.Student"></bean>


   4) 在一個類屬性上把bean引入進來
	@Resource(name="student_di")
	private Student student;


   5) 在一個類的屬性上加上@Resource(name="")
    該註解可以用於屬性上或者方法上,但一般用於屬性上,屬性name預設值為"";
 

      (3)示例

             1)Person類

public class Person {
	
	private String name;
	private Integer age;
	
	@Resource(name="student_di")
	private Student student;
	
	
	public void showStudent(){
		this.student.say();
	}

	
}
           2)Student類
public class Student {
	
	public void say(){
		System.out.println("我是學生");
	}
}

          3)配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	       http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

	<bean id="person_di" class="cn.labelnet.di.Person">
	</bean>

	<bean id="student_di" class="cn.labelnet.di.Student"></bean>

</beans>

        4)測試
	@Test
	public void testDI() {
 
		 Person person=(Person) context.getBean("person_di");
	     person.showStudent();
	}


      (4)總結:

                當啟動spring容器的時候,spring容器載入了配置檔案,只要遇到bean的配置,就會
  為該bean建立物件,在容器範圍內查詢所有的bean,如果有@Resource註解,判斷註解name屬
  性值是否與speing容器中ID的值做匹配,匹配成功則賦值;

3.類掃描註解

    (1)類掃描的註解解析器 
  component : 指的就是一個類;base-package 在該包 
     (2)基本步驟:
1. 匯入名稱空間

  xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">


2. 引用註解解析器
    包含兩個功能,類掃描和依賴注入,在base-package包下的查詢所有類;
   <context:component-scan base-package="包名"></context:component-scan>


3. 配置@Component("")
@Component("a")
public class Person {

	private String name;
	private Integer age;

	@Resource(name = "b")
	private Student student;

	public void showStudent() {
		this.student.say();
	}

}


4. 按照@Resource步驟來使用;
 

    (3)示例

           1)Person類

@Component("a")
public class Person {

	private String name;
	private Integer age;

	@Resource(name = "b")
	private Student student;

	public void showStudent() {
		this.student.say();
	}

}
        2)Studnet類
@Component("b")
public class Student {
	
	public void say(){
		System.out.println("我是學生 SCAN");
	}
}

      3)bean配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	       http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

   <context:component-scan base-package="cn.lablenet.scan"></context:component-scan>

</beans>

         4)測試
@Test
	public void testScan() {
 
		 Person person=(Person) context.getBean("a");
	     person.showStudent();
	}


    (4)總結xml與註解
  1. xml書寫麻煩,但是效率高;
  2. 註解書寫簡單,但效率低;(可以忽略)

4.示例 - 簡單的mvc程式

    使用的註解有 : @Repository , @Service , @Controller ,分別對應 dao層,service層,action層;

  (1)dao 介面

public interface DocumentDao {
	void saveDocument();
}
  

   (2)dao實現

@Repository("DocDao")
public class DocumentDaoImpl implements DocumentDao {
	@Override
	public void saveDocument() {
         System.out.println("儲存成功");
	}
}
    (3)service介面
public interface DocumentService {
	void saveDocument();
}
    (4)service實現
@Service("DocService")
public class DocumentServiceImpl implements DocumentService {
	@Resource(name="DocDao")
	private DocumentDao docdao;
	
	@Override
	public void saveDocument() {
		docdao.saveDocument();
	}
}


    (5)action實現
@Controller("DocController")
public class DocumentView {
	
	@Resource(name="DocService")
	private DocumentService doucService;

	public void saveDoc(){
		doucService.saveDocument();
	}

}


    (6)bean配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	       http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
           
   <context:component-scan base-package="cn.labelnet.mvc"></context:component-scan>
   
</beans>

    (7)測試
@Test
	public void testDocument() {
		
		DocumentView view= (DocumentView)context.getBean("DocController");
		view.saveDoc();
	}

5.繼承配置 

    繼承 :
  在配置檔案中 給 bean 新增 abstruct="true" 屬性,告訴spring容器,該bean不能建立物件;
  子類想用父類的屬性,需要在bean 新增 parent="xxx abstruct" 屬性,新增父類bean的id;
    示例:

     父類 :

public class Person {
	private String name;
	public void say(){
		System.out.println("我是父類");
	}
}
    子類 :
public class Student extends Person {

}

   bean配置 :
<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="person_ex" class="cn.labelnet.extend.Person" abstract="true"></bean>
    
    <bean id="student_ex" class="cn.labelnet.extend.Student" parent="person_ex"></bean>
    
</beans>

6.Demo免積分下載