基于注解的Spring注入
使用注解时,需要添加额外的依赖spring-aop-5.2.6.RELEASE.jar
,下面是项目结构:
我们已经将jar导入到lib库中,就可以直接使用了。
首先需要在bean.xml文件中进行配置,因为使用注解进行属性的注入,需要在xml中引入context
和aop
。
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解扫描
指定扫描的包,如果需要扫描多个包,可以使用逗号进行隔开
use-default-filters 标签说明是否使用默认的filters,默认为true;如果选择false,则需要自己配置filters
-->
<context:component-scan base-package="spring5, test" use-default-filters="false">
<!-- context:include-filter标签 用于指定包含的filters
context:exclude-filter标签 用于指定不包含的filters
type标签指定扫描的类型 annotation为注解
expression指定扫描的种类
针对类的注解有多种
@Repository 一般用于注解dao
@Component 一般用于注解存放于pojo中的普通类
@Controller 一般用于注解Servlet类
@Service 一般用于注解Service类
上面集中注解的功能相似
-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
然后创建一个普通的类Author,对其String
类型的name属性进行注入。
package spring5;
import org.springframework.stereotype.Repository;
// Repository用于指定用于注解
@Repository
public class Author {
@Value(value="Bruce")
private String name;
public Author() {
}
public Author(String name) {
this.name = name;
}
@Override
public String toString() {
return "Author{" +
"name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
创建一个对象属性进行注入的类Book,使用@Autowired
对其 Author类型的 author属性进行注入。
package spring5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
@Repository(value = "book")
public class Book {
/**
* 使用set方法进行属性的注入
*/
@Value("Bruce")
private String name;
@Autowired
private Author author;
public Book() {
}
public Book(String name, Author author) {
this.name = name;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
}
其中
- Autowired是根据数据类型进行注入
- Qualifier是根据名称,即
id
值进行注入。在@Repository
,@Component
,@Controller
,@Service
后面可以添加value属性用于指定id值,与xml中<bean id="author" class="spring5.Author"/>
配置类似。
简单的spirng进行测试,可能只需要使用其中一个就可以实现了,但是在开发当中可能在对属性进行注入时,会遇到同类型
或同名称
的属性值,这时候可以将二者搭配起来进行使用。
@Autowired
@Qualifier(value = "author")
此外@Autowired
还可以对List、Map等集合对象进行注入,可参考连接Autowired使用
下面是编写的测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring5.Book;
public class TestUser {
@Test
public void test(){
// 1、加载xml文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
// 2、获取配置创建对象
Book book = context.getBean("book", Book.class);
// 3、因为在xml进行了有参构造的配置,使用在类型重写的toString方法进行输出
System.out.println(book);
}
}
执行输出为
完全注解开发
上面使用spring进行对象属性的注解,需要来bean.xml文件开启组件扫描并进行相应的配置,而开发中,也可以不使用xml文件进行配置,通过定义一个配置类来实现。
首先介绍一个工程结构,删除了bean.xml文件,新建了一个config包,在包里面定义了一个SpringConfig类。
其中SpringConfig类为:
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration 说明是配置类,用来替代XML文件
* @ComponentScan 指定注解扫描的包,需要注意的是虽然Value的定义仍然采用的是键值对的形式,但是多个包的包含还是有点区别的
*
*/
@Configuration
@ComponentScan(basePackages = {"spring5", "test"})
public class SpringConfig {
}
因为是配置类,所以不需要对类进行具体属性的定义,主要用于通过获取配置创建对象时使用。
该项目中Java Bean对象与上面的相同,下面编写测试类。
package test;
import config.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import spring5.Book;
public class TestUser {
@Test
public void test(){
// 1、加载xml文件
// 因为是加载完全注解配置类,不适用XML文件进行配置,所以用AnnotationConfigApplicationContext替代ClassPathXmlApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
// 2、获取配置创建对象
Book book = context.getBean("book", Book.class);
// 3、因为在xml进行了有参构造的配置,使用在类型重写的toString方法进行输出
System.out.println(book);
}
}
执行程序打印输出
关于Author对象中name值为null的原因是因为:在Author只是提供注解,但是没有对name
属性进行提供初值,所以默认为null,只需要在Author.java中在name属性定义时通过注解提供初值:
@Value(value = "Bruce")
private String name;
这时测试程序的输出为