Spring笔记之注解
一、使用注解
jdk1.5开始支持注解,spring2.5开始全面支持注解。
-
在spring配置文件中引入context文件头
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
-
开启属性注解支持
<context:annotation-config/>
1.1 @Autowired
- @Autowired是按类型自动转配的,不支持id匹配。
- 需要导入 spring-aop的包!
-
将User类中的set方法去掉,使用@Autowired注解
public class User { @Autowired private Cat cat; @Autowired private Dog dog; private String str; public Cat getCat() { return cat; } public Dog getDog() { return dog; } public String getStr() { return str; } }
-
此时配置文件内容
<context:annotation-config/> <bean id="dog" class="com.spring.pojo.Dog"/> <bean id="cat" class="com.spring.pojo.Cat"/> <bean id="user" class="com.spring.pojo.User"/>
-
测试,成功输出结果
@Autowired(required=false) 说明: false,对象可以为null;true,对象必须存在,不能为null
//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;
1.2 @Qualifier
- @Autowired是根据类型自动装配的,加@Qualifier则可以根据byName的方式自动装配
- @Qualifier不能单独使用。
-
配置文件修改内容,保证类型存在对象。且名字不为类的默认名字!
<bean id="dog1" class="com.spring.pojo.Dog"/> <bean id="dog2" class="com.spring.pojo.Dog"/> <bean id="cat1" class="com.spring.pojo.Cat"/> <bean id="cat2" class="com.spring.pojo.Cat"/>
-
没有加Qualifier测试,直接报错
-
在属性上添加Qualifier注解
@Autowired @Qualifier(value = "cat2") private Cat cat; @Autowired @Qualifier(value = "dog2") private Dog dog;
-
测试,成功输出
1.3 @Resource
- @Resource如有指定的name属性,先按该属性进行by Name方式查找装配;
- 其次再进行默认的byName方式进行装配;
- 如果以上都不成功,则按byType的方式自动装配。
- 都不成功,则报异常。
实体类:
public class User {
//如果允许对象为null,设置required = false,默认为true
@Resource(name = "cat2")
private Cat cat;
@Resource
private Dog dog;
private String str;
}
beans.xml:
<bean id="dog" class="com.spring.pojo.Dog"/>
<bean id="cat1" class="com.spring.pojo.Cat"/>
<bean id="cat2" class="com.spring.pojo.Cat"/>
<bean id="user" class="com.spring.pojo.User"/>
测试:结果OK
配置文件2:beans.xml , 删掉cat2
<bean id="dog" class="com.spring.pojo.Dog"/>
<bean id="cat1" class="com.spring.pojo.Cat"/>
实体类上只保留注解
@Resource
private Cat cat;
@Resource
private Dog dog;
测试:结果OK
结论:先进行byName查找,失败;再进行byType查找,成功。
1.4 小结
@Autowired与@Resource异同:
- @Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。
- @Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用
- @Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是
需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。 - 它们的作用相同,都是用注解方式注入对象,但执行顺序不同:@Autowired先byType,@Resource先byName。
二、使用注解开发
2.1 说明
在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"
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">
</beans>
2.2 Bean的实现
我们之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!
-
配置扫描哪些包下的注解
<!--指定注解扫描包--> <context:component-scan base-package="com.spring.pojo"/>
-
在指定包下编写类,增加注解
@Component("user") // 相当于配置文件中 <bean id="user" class="当前注解的类"/> public class User { public String name = "spring"; }
-
测试
@Test public void test(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); User user = (User) applicationContext.getBean("user"); System.out.println(user.name); }
2.3 属性注入
使用注解注入属性
-
可以不用提供set方法,直接在属性名上添加@value("值")
@Component("user") // 相当于配置文件中 <bean id="user" class="当前注解的类"/> public class User { @Value("spring") // 相当于配置文件中 <property name="name" value="spring"/> public String name; }
-
如果提供了set方法,在set方法上添加@value("值")
@Component("user") public class User { public String name; @Value("spring") public void setName(String name) { this.name = name; } }
2.4 衍生注解
我们这些注解,就是替代了在配置文件当中配置步骤而已!更加的方便快捷!
@Component三个衍生注解:为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
- @Controller:web层
- @Service:service层
- @Repository:dao层
写上这些注解,就相当于将这个类交给Spring管理装配了!
2.5 自动装配注解
在Bean的自动装配已经讲过了,自行回顾!
2.6 作用域
@scope
- singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂,所有的对象都会销毁。
- prototype:多例模式。关闭工厂,所有的对象不会销毁。内部的垃圾回收机制会回收。
@Controller("user")
@Scope("prototype")
public class User {
@Value("spring")
public String name;
}
2.7 小结
XML与注解比较:
- XML可以适用任何场景 ,结构清晰,维护方便
- 注解不是自己提供的类使用不了,开发简单方便
xml与注解整合开发 :推荐最佳实践
- xml管理Bean
- 注解完成属性注入
- 使用过程中, 可以不用扫描,扫描是为了类上的注解
<context:annotation-config/>
作用:
- 进行注解驱动注册,从而使注解生效
- 用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示地向Spring注册
- 如果不扫描包,就需要手动配置bean
- 如果不加注解驱动,则注入的值为null
2.8 基于Java类进行配置
JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。
-
编写一个实体类,Dog
@Component //将这个类标注为Spring的一个组件,放到容器中! public class Dog { public String name = "dog"; }
-
新建一个config配置包,编写一个MyConfig配置类
@Configuration //代表这是一个配置类 public class MyConfig { @Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id! public Dog dog(){ return new Dog(); } }
-
测试
@Test public void test2(){ ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Dog dog = (Dog) applicationContext.getBean("dog"); System.out.println(dog.name); }
-
成功输出结果!
导入其他配置如何做呢?
-
再编写一个配置类!
@Configuration //代表这是一个配置类 public class MyConfig2 { }
-
在之前的配置类中我们来选择导入这个配置类
@Configuration @Import(MyConfig2.class) //导入合并其他配置类,类似于配置文件中的 inculde 标签 public class MyConfig { @Bean public Dog dog(){ return new Dog(); } }
关于这种Java类的配置方式,我们在之后的SpringBoot 和 SpringCloud中还会大量看到,我们需要知道这些注解的作用即可!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)