这里我们回顾一下什么是注解 Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。 Java 语言中的类、方法、变量、参数和包等都可以被标注。和 Javadoc 不同,Java 标注可以通过反射获取标注内容。在编译器生成类文件时,标注可以被嵌入到字节码中。Java 虚拟机可以保留标注内容,在运行时可以获取到标注内容 。 当然它也支持自定义 Java 标注。
通过注解注入属性 一、引入context名称空间
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"二、设置组件扫描
<!-- 添加组件扫描 引入context名称空间--> <context:component-scan base-package="cn.kexing.Spring5"></context:component-scan>context:component-scan提供了两个子标签
context:include-filter
context:exclude-filter
在说明这两个子标签前,先说一下<context:component-scan>有一个use-default-filters属性,该属性默认为true,这就意味着会扫描指定包下的全部的标有Spring注解的类,并注册成bean。也就是@Component,@Controller,@Service,@Reposity等。所以如果仅仅是在配置文件中这么写
<context:component-scan base-package="cn.kexing.Spring5"></context:component-scan>use-default-filter此时为true那么会对base-package包或者子包下的所有的进行java类进行扫描,并把匹配的java类注册成bean。 此时组件扫描的范围 为设置的整个包下的所有类,范围过大,那如果我想指定需要扫描的内容,比如只扫描@Controller注解注册的bean时该怎么办呢?下面举个例子,只扫描Spring5包下设置了@Controller注解的类
<context:component-scan base-package="cn.kexing.Spring5" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>use-default-filters属性值设置为false,不启动默认的扫描,<context:include-filter>标签的意思就是扫描包含了指定的注解(这里类似于过滤器),expression属性值设置指定的注解位置
另一个标签context:exclude-filter
<!-- context:exclude-filter不扫描哪些内容 除了Service注解其他的都扫描 --> <context:component-scan base-package="cn.kexing.Spring5"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> </beans>该标签来指定不扫描哪些包
三、注解装配bean 原来需要使用XML配置的方式将id和类绑定,现在不需要这样做了只需要在类上添加注解即可。
@Component :组件通用注解,常用于Model类@Controller :常用于对Controller实现类进行标注@Service:常用于对Service实现类进行标注@Repository:常用于对DAO实现类进行标注四、Bean属性注入注解
@Autowired 根据类型自动注入 如果接口引用有多个实现类时需和下面注解配合使用@Qualifier 根据名称注入,当接口有多个实现类时,指定要注入类的名称@Resource 根据对象注入@Value 注入普通类型下面以注解方式装配bean举例,有两个包,Service包和Dao包 Dao包中接口及类:
UserDao 接口
package cn.kexing.Spring5.Dao; public interface UserDao { public void print(); }UserDaoImpl 实现类
package cn.kexing.Spring5.Dao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements UserDao{ @Override public void print() { System.out.println("UserDaoImpl"); } }UserDaoImpl2实现类
package cn.kexing.Spring5.Dao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl2 implements UserDao{ @Override public void print() { System.out.println("UserDaoImp2"); } }Service类
package cn.kexing.Spring5.Service; import cn.kexing.Spring5.Dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired //根据类型自动注入 @Qualifier(value = "userDaoImpl2") //根据名称注入,当接口有多个实现类时,指定要注入类的名称 private UserDao userDao; @Value("zhnagsan") private String name; public void print(){ System.out.println("UserService"+"----"+userDao+"------"+name); } }这里的属性注入,因为Dao接口有两个实现类,所以用@Qualifier注解指定对应的实现类
xml配置 bean2
<?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: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"> <!-- 添加组件扫描 引入context名称空间--> <context:component-scan base-package="cn.kexing.Spring5"></context:component-scan> </beans>测试
@Test public void testFiled(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml"); UserService service = context.getBean("userService",UserService.class); service.print(); }Output
UserService----cn.kexing.Spring5.Dao.UserDaoImpl2@66982506------zhnagsan以上是基于注解装配bean的基本内容和步骤
真正的热爱不会因为别人的三言两语左右