根据之前的注解知识,我们可以明白,通过注解可以简化bean配置的功能,动力节点的老杜将Spring6(事实上本文档就是跟着老杜的适配记录的)里面有一节是获得注解的扫描器课程,本质上就是sprin容器通过反射获得注解和类,然后可以进行注入。总之通过注解开发可以进一步简化开发流程,但本质上是bean套了一层皮。
首先是 四个声明式注解
@Component @Controller @Service @Repository 只能用在类上面
这四个没有区别,只是为了增加代码的可阅读性,可以用在不同的包中
点开后就会发现
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
这四个注解都是声明类的作用,意思把类注入spring容器,提到了<bean id ="" class="">
首先,我们要在配置文件当中写一下我们要加载的类的包名,这样才能告诉Spring在哪里找到,告诉spring要扫描那些包
首先我们要将配置文件修改一下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns: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://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
我们来测试一下
给出一个类
@Component("role")
public class Role {
private String name = "绘梨衣";
public void print(){
System.out.println(this.name);
}
}
@Componeny("xxx")中的xxx是后来要去bean的时候用的,不写xxx的话默认是首字符名字小写,也就是Role--->role
完善一下bean.xml,主要是配置要扫描的包
<context:component-scan base-package="com.chenchen.dao"></context:component-scan>
然后还是之前的测试方法不用改变
public class TEST {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Role role = applicationContext.getBean("role", Role.class);
role.print();
}
}
就可以输出了 ,当然这里有很多可以省略,我就不记录了,我是觉得每一个@Commpoent都写上名字比较合适
spring扫描完文档,找到要扫描的包,扫描完全部的类,找到注解并且可以读取注解,然后完成类的实例化将其注入Map集合当中,我们就可以拿到需要的实例化好的类了,不需要自己手动创建。
--------------------------------------------------------------------------------------------------------------------
多个包的扫描问题
相同同时扫描两个包中的类,我们有以下两种方式
第一种 <context:component-scan base-package="com.chenchen.dao.service ,com.chenchen.dao.user"> 加逗号
第二种 只写他们的父包名即可
------------------------------------------------------------------------------------------------------
多个类的选择配置问题
当一个包中有多个类的时候,我们想要只实例化其中的一个或者多个怎么办??
首先多给几个类
一个包中三个类,我只想实例化一和三
需要在bean.xml中进行配置,而且有两种方法
第一种 use-default-filters 值为false 包中所有的类的声明失效 配合
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
expression =" "<---------里面是可以生效的注解,我们可以用四个不同的注解来决定那个类可以被实例化
三个类如下
@Component("role")
public class Role {
private String name = "绘梨衣";
public void print(){
System.out.println(this.name);
}
}
@Service("role2")
public class Role2 {
private String name = "绘梨衣2";
public void print(){
System.out.println(this.name);
}
}
@Controller("role3")
public class Role3 {
private String name = "绘梨衣3";
public void print(){
System.out.println(this.name);
}
}
配置文件如下:
<context:component-scan base-package="com.chenchen.dao.service" use-default-filters="false" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
测试如下
public class TEST {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Role role = applicationContext.getBean("role", Role.class);
Role2 role2 = applicationContext.getBean("role2", Role2.class);
Role role3 = applicationContext.getBean("role3", Role3.class);
role.print();
role2.print();
role3.print();
}
}
此时结果是
D:\idea\project\yaoshanchude\spring\src\test\java\TEST.java:13:66
java: 找不到符号
符号: 类 Role3
位置: 类 TEST
但是我们删除role3
就可以顺利运行了