深入分析JavaWeb Item52 -- Spring注解Annotation_context annotation-config 标签红色出错(1)

最后

现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

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

}


Student类代码如下



package com.itheima10.spring.di.annotation;

public class Student {
public void say(){
System.out.println(“student”);
}
}


配置applicationContext.xml文件



<?xml version="1.0" encoding="UTF-8"?>







context:annotation-config</context:annotation-config>


编写测试类AnnotationTest 



package com.itheima10.spring.di.annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 原理:
* 1、启动spring容器
* 2、把person和student两个bean实例化
* 3、当spring容器解析到
* context:annotation-config</context:annotation-config>
* 就会启动依赖注入的注解解析器
* 4、spring容器会在纳入spring管理的bean的范围内查找,看这些类的哪些属性上加有@Resource注解
* 5、如果某一个属性上加有@Resource注解
* 会查看该注解的name属性的值是否为""
* 如果为"“,则会把该注解所在的属性的名称和spring容器中的id的值作匹配,如果匹配成功,则赋值
* 如果匹配不成功,则按照类型进行匹配,匹配成功则赋值
* 如果再匹配不成功,则报错
* 如果不为”",则把该注解的name属性的值和spring容器中id的值作匹配,如果匹配成功,则赋值
* 如果匹配不成功,则直接报错
*
说明:
注解只能作用于引用类型
xml与注解的对比
xml的效率比较高,书写比较麻烦
注解的书写比较简单,效率比较低
*
*/
public class AnnotationTest {
@Test
public void testAnnotation(){
ApplicationContext context =
new ClassPathXmlApplicationContext(“applicationContext.xml”);
Person person = (Person)context.getBean(“person”);
person.say();
}
}


如果使用注解,就不需要在配置文件中装载person和student了,这样就可以简化配置文件的编写。


##### **3、 扫描**


前面的例子我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。要使用自动扫描机制,我们需要打开以下配置信息:


**1、引入context命名空间** 


在xml配置文件中添加`context:component-scan`标签 


其中**base-package**为需要扫描的包(含子包)。


**实例:**   
 将上述实例用扫描的方式书写如下



@Component
public class Person {
@Resource(name=“student”)
private Student student;

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

}



@Component
public class Student {
public void say(){
System.out.println(“student”);
}
}


applicationContext.xml只需配置一句话



 <!-- 

component 组件
把一个类放入到spring容器中,该类就称为组件
在base-package指定的包及子包下扫描
–>
<context:component-scan base-package=“com.itheima10.spring.scan”></context:component-scan>


编写测试类AnnotationTest 



/**
* 原理
* 1、启动spring容器
* 2、spring容器解析
* <context:component-scan base-package=“com.itheima10.spring.scan”>
</context:component-scan>
3、在base-package指定的包及子包中扫描,看哪些类上面是否含有@Component注解
4、如果有该注解
@Component
public class Person {
}
==等价于

@Component(“aa”)
public class Person {
}
==等价于

5、按照@Resource的解析步骤执行
说明:
整个过程扫描两次,效率越来越低,书写越来越简单
*
*
*/
public class AnnotationTest {
@Test
public void testAnnotation(){
ApplicationContext context =
new ClassPathXmlApplicationContext(“applicationContext.xml”);
Person person = (Person)context.getBean(“person”);
person.say();
}
}


**实例再现**


我们将Item51中最后的文档管理系统用注解的方式改一下,Document接口不变,有read和write方法,实现类分别如下ExcelDocument ,PDFDocument ,WordDocument 。



@Component(“excelDocument”)
public class ExcelDocument implements Document{

public void read() {
    System.out.println("excel read");
}
public void write() {
    System.out.println("excel write");
}

}



@Component(“pdfDocument”)
public class PDFDocument implements Document{

public void read() {
    System.out.println("pdf read");
}
public void write() {
    System.out.println("pdf write");
}

}



@Component(“wordDocument”)
public class WordDocument implements Document{

public void read() {
    System.out.println("word read");
}
public void write() {
    System.out.println("word write");
}

}


DocumentManager 



@Component(“documentManager”)
public class DocumentManager {
@Resource(name=“excelDocument”)
private Document document;

public void read(){
    this.document.read();
}

public void write(){
    this.document.write();
}

}


配置文件



<context:component-scan base-package="com.itheima10.spring.iocdi.document">
</context:component-scan>

编写测试类DocumentTest 



public class DocumentTest {
@Test
public void testDocument(){
ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
DocumentManager documentManager = (DocumentManager)context.getBean(“documentManager”);
documentManager.read();
documentManager.write();
}
}


**2、其他注解功能介绍**   
 **@Service**用于标注业务层组件、服务层注解   
 **@Controller**用于标注控制层组件(如struts中的action)、控制层注解   
 **@Repository**用于标注数据访问组件,即DAO组件。持久层注解


而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 


**实例重现–MVC案例**


我们再次回顾Item51中的MVC案例,分别将PersonDaoImpl ,PersonAction ,PersonServiceImpl 的Dao,Service,Action层加上注解有



@Repository(“personDao”)
public class PersonDaoImpl implements PersonDao {
@Override
public void savePerson() {
System.out.println(" save person");
}
}



@Service(“personService”)
public class PersonServiceImpl implements PersonService{
@Resource(name=“personDao”)
private PersonDao personDao;

public void setPersonDao(PersonDao personDao) {
    this.personDao = personDao;
}
@Override
public void savePerson() {
    this.personDao.savePerson();

}

}



@Controller(“personAction”)
public class PersonAction {
@Resource(name=“personService”)
private PersonService personService;

public void setPersonService(PersonService personService) {
    this.personService = personService;
}

public void  savePerson(){
    this.personService.savePerson();
}

}


编写测试MVCTest 



public class MVCTest {
@Test
public void testMVC(){
ApplicationContext context =
new ClassPathXmlApplicationContext(“applicationContext.xml”);
PersonAction personAction = (PersonAction)context.getBean(“personAction”);
personAction.savePerson();
}
}


##### **4. spring中的继承**


Spring支持继承,可以分为类继承和属性继承


**1. 类继承**


Spring属性:   
 (1)abstract: 如果设置为true,表示定义的bean是抽象的,告诉spring不要实例化这个bean;   
 问题:必须是抽象类么?可以不是抽象类么?   
 (2)parent: 指明bean的id,对bean的作用,相当于extends对于java类的作用;


场景:有三个Bean:



<bean id = "bean1" class = "……TestBean">
    <property name="sex" value="male"/>
</bean>
<bean id = "bean2" class = "……TestBean">
    <property name="sex" value="male"/>
</bean>
<bean id = "bean3" class = "……TestBean">
    <property name="sex" value="female"/>
</bean>

修改:定义spring 父bean



<bean id ="BaseBean" class ="……TestBean">
    <property name="sex" value="male"/>
</bean>

定义子Bean



<bean id ="bean1" parent = "BaseBean"/>      继承父Bean的属性
<bean id ="bean2" parent = "BaseBean"/>         
<bean id ="bean3" parent = "BaseBean">     覆盖父Bean的属性
    <property name="sex" value="female"/>
</bean>

最后

2020年在匆匆忙忙慌慌乱乱中就这么度过了,我们迎来了新一年,互联网的发展如此之快,技术日新月异,更新迭代成为了这个时代的代名词,坚持下来的技术体系会越来越健壮,JVM作为如今是跳槽大厂必备的技能,如果你还没掌握,更别提之后更新的新技术了。

更多JVM面试整理:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

020年在匆匆忙忙慌慌乱乱中就这么度过了,我们迎来了新一年,互联网的发展如此之快,技术日新月异,更新迭代成为了这个时代的代名词,坚持下来的技术体系会越来越健壮,JVM作为如今是跳槽大厂必备的技能,如果你还没掌握,更别提之后更新的新技术了。

[外链图片转存中…(img-sN4hElit-1715691028006)]

更多JVM面试整理:

[外链图片转存中…(img-s8f9mJpN-1715691028007)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值