8 使用注解开发
在Spring4之后,要使用注解开发,必须保证aop的包导入
在使用注解需要导入context约束,增加注解的支持
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解支持-->
<context:annotation-config/>
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.serene.pojo"/>
</beans>
8.1 bean注入使用@Component
//User.java
//等价于在applicationContext.xml中配置<bean id="user" class="com.serene.pojo.User"/>
//Component 组件
@Component
public class User {
public String name="韵宝宝";
}
//Test.java
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.name);
}
8.2 属性注入使用@Value注解
注意8.1中,对象的属性直接创建属性的,如果该例子中不进行赋值,运行起来会打印null。
//等价于配置<bean id="user" class="com.serene.pojo.User"/>
//Component 组件
@Component
public class User {
//等价于在bean中配置<property name="name" value="韵宝宝anno"/>
@Value("韵宝宝anno")
public String name;
//public String name="韵宝宝";
}
当然,如果实体类提供了set方法,也可以使用@Value去注入属性;如下面的例子,且setName的优先级高。
@Component
public class User {
//等价于在bean中配置<property name="name" value="韵宝宝anno"/>
@Value("韵宝宝anno")
public String name;
//public String name="韵宝宝";
@Value("韵宝宝set")
public void setName(String name) {
this.name = name;
}
}
8.3 衍生的注解
@Component有几个衍生注解,我们在web开发中,会按照MVC三层架构分层!
-
dao层【@Repository】
-
service层【@Service】
-
controller层【@Controller】
这四个注解的功能是一样的,都是代表将某个类注册到Spring中,装配Bean,只是使用的地方不同
8.4 自动装配
@Autowired 自动装配通过类型、名字
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")
@Nullable 字段标记了这个注解,说明这个字段可以为null
@Resource 自动装配通过名字,类型
8.5 作用域
@Scope(“singleton”)单例
8.6 小结
XML与注解:
- xml更加万能,适用于任何场合!维护更加简单
- 注解不是自己类使用不了,维护相对复杂
XML与注解配合使用:
-
xml用来管理bean
-
注解只负责完成属性的注入
-
我们在使用过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<!--开启注解支持--> <context:annotation-config/> <!--指定要扫描的包,这个包下的注解就会生效--> <context:component-scan base-package="com.serene.pojo"/>
对应学习代码:spring-06-anno
9 使用Java的方式配置Spring
现在要完全不使用Spring的xml配置类与属性来开发,完全交给java来做
JavaConfig是Spring的一个子项目,在Spring4之后,他成为一个核心功能!SpringBoot中常用的方法。
实体类
//这里这个注解的意思,就是说明这个类被Spring接管了。注解到了容器中
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("韵宝宝")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
java配置类
//这个也是被Spring容器 托管,注册到了容器中国,因为它本身就是一个@Component,
//@Component代表这是一个配置类,和我们之前看的bean.xml是一样的
@Configuration
//完成包扫描
@ComponentScan("com.serene.pojo")
//导入,这里可以用于配置多个对象
@Import(YunConfig2.class)
public class YunConfig {
//注册一个bean,就相当于之前写的一个bean标签
//这个方法的名字,就相当于bean标签的id属性
//这个方法的返回值,就相当于bean标签中的class属性
@Bean
public User getUser(){
return new User();//返回要注入到bean的对象
}
}
测试代码
@Test
public void test(){
//如果完全使用配置类的方法去做,我们只能通过AnnotationConfigApplicationContext
// 上下文来获得容器,通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(YunConfig.class);
User user = context.getBean("getUser", User.class);
System.out.println(user.toString());
}
这种通过java配置类来完成bean.xml的功能,纯java的配置方式,在SpringBoot随处可见!
对应练习代码:spring-07-config