-
5 使用注解开发
5 使用注解开发
module:spring-06-anno
说明
在Spring4之后,想要使用注解,必须要引入aop的jar包
在配置文件中还必须要引入一个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"
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:annotation-config/>
</beans>
bean的实现
之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!
1、配置扫描哪些包下的注解
<context:component-scan base-package="com.zzb"/>
2、在指定包下编写类,并且增加注解
package com.zzb.pojo;
import org.springframework.stereotype.Component;
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component("user")
public class User {
public String name = "ZZB";
}
3、测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.name);
}
测试结果:
ZZB
成功输出!
属性注入
使用注解注入属性
1、可以不用提供set方法,直接在属性字段名上添加@Value("值")注解
package com.zzb.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component("user")
public class User {
@Value("ZZB")
// 相当于配置文件中 <property name="name" value="ZZB"/>
public String name;
}
2、如果提供了 set方法,可以在set方法上添加@Value("值")注解
package com.zzb.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("user")
public class User {
public String name;
@Value("ZZB")
public void setName(String name) {
this.name = name;
}
}
衍生注解
这些注解其实就是替代了配置文件中的配置步骤,更加的方便快捷!
@Component三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
- @Controller:web层
- @Service:service层
- @Repository:dao层
写上这些注解,就相当于将这个类交给Spring管理装配了!
自动装配注解
前文已经介绍过!
作用域
@scope
- singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
- prototype:多例模式。关闭工厂 ,所有的对象不会销毁,内部的垃圾回收机制会回收。
package com.zzb.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("user")
@Scope("singleton")
public class User {
public String name;
@Value("ZB")
public void setName(String name) {
this.name = name;
}
}
总结:
XML与注解比较
XML与注解比较
- XML可以适用任何场景 ,结构清晰,维护方便
- 注解不是自己提供的类使用不了,开发简单方便
XML与注解整合开发 :推荐最佳实践
- xml管理Bean
- 注解完成属性注入
- 使用过程中, 可以不用扫描,扫描是为了类上的注解
注意开启注解:
<context:annotation-config/>
作用:
- 进行注解驱动注册,从而使注解生效
- 用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册
- 如果不扫描包,就需要手动配置bean
- 如果不加注解驱动,则注入的值为null!
基于Java类进行配置
module:spring-07-appconfig
JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。
1、编写一个实体类,Dog
package com.zzb.pojo;
import org.springframework.stereotype.Component;
@Component // 可以被配置文件扫描自动装配
public class Dog {
public String name = "dog";
}
2、新建一个config配置包,编写一个MyConfig配置类
package com.zzb.config;
import com.zzb.pojo.Dog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //代表这是一个配置类
public class ZzbConfig {
@Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!
public Dog getDog(){
return new Dog();
}
}
3、测试
@Test
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(ZzbConfig.class);
// 通过@Bean配置
Dog dog = context.getBean("getDog", Dog.class);
System.out.println(dog.name);
}
测试结果:
dog
结果成功输出!
导入其他配置如何做呢?
1、我们再编写一个配置类!
@Configuration //代表这是一个配置类
public class ZzbConfig2 {
}
2、在之前的配置类中我们来选择导入这个配置类
package com.zzb.config;
import com.zzb.pojo.Dog;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.zzb.pojo")
@Import(ZzbConfig2.class)
public class ZzbConfig {
@Bean
public Dog getDog(){
return new Dog();
}
}
3、测试
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ZzbConfig.class);
// 通过@Bean配置
Dog dog = context.getBean("getDog", Dog.class);
// 通过@Component配置,@ComponentScan("com.zzb.pojo")
Dog dog2 = context.getBean("dog", Dog.class);
System.out.println(dog.name);
System.out.println(dog2.name);
}
关于这种Java类的配置方式,我们在之后的SpringBoot 和 SpringCloud中还会大量看到,我们需要知道这些注解的作用即可!
来源:https://www.cnblogs.com/zzbstudy/p/14115989.html