本文已参与「新人创作礼」活动,一起开启掘金创作之路。
1.在Spring4之后,要使用注解开发,必须要保证aop的包导入了
2.新建一个子项目模块spring-5-anno 使用注解需要导入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
http://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">
</beans>
3.在java下新建一个包com.kuang.pojo,假设里面有一个注解: 第一种方法就是指定要扫描的包,用这种方法这个包下的注解就会生效:
4.在com.kuang下建四个包:
5.我们在dao层建一个User类:
5.如果按照以前的写法,那么就是在applicationContext.xml文件里,还是bean注入的方式来写:
6.但是我们要换一种方法来写,用注解来写: applicationContext.xml:
<?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
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:component-scan base-package="com.kuang.dao"/>
<!--开启注解支持-->
<context:annotation-config/>
</beans>
User.java:
package com.kuang.dao;
import org.springframework.stereotype.Component;
//@Component等价于<bean id="user" class="com.kuang.dao.User"/>
@Component//组件的意思
public class User {
public String name="文字游戏";
}
写一个测试类MyTest.java:
import com.kuang.dao.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("user");
System.out.println(user.name);
}
}
最后发现注解方式注入成功!
7.属性如何注入 :可以放在属性上或者set方法上 直接在属性上利用注解Value("需要注入的值")即可
@Value("文字")相当于<property name="name" value="tiantian"/>
8.衍生的注解
@Component有几个衍生注解,我们在web开发中会按照mvc三层架构分层
dao [@Repository]
pojo [@Component]
service [@Service]
controller [@Controller]
这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配bean
9.自动装配
@Autowired:自动装配通过类型和名字
如果@Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value=”xxx”)
@Nullable:某个字段标记了这个注解,说明这个字段可以为空
@Resoure:自动装配通过名字、类型
10.作用域@Scope("")
@Scope="singleton"代表单例
@Scope="prototype"代表多例
11.总结:
xml与注解:
1)xml更加万能,适用于任何场合!维护简单方便
2)注解不是自己类使用不了,维护相对复杂!
3)xml与注解最佳实践:
xml用来管理bean
注解只负责完成属性的注入
我们在使用的过程中,只需要注意一个问题:必须让注解生效,开启注解支持
也就是下面两行代码:
<!--扫描包-->
<context:component-scan base-package="com.kuang.dao"/>
<!--开启注解支持-->
<context:annotation-config/>
其中 //@Component意思是这个类被Spring托管了,注册到了容器中
比如下面的xml文件只管理bean:
在java类中完成属性的注入:
本篇博客到此结束啦,有问题随时私信哦!