Spring - 注解的使用 和 继承的配置

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免积分下载

http://download.csdn.net/detail/lablenet/9379973


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值