Spring使用注解开发
在Spring4之后,要使用注解开发,必须要保证AOP的包导入了

bean如何注解
约束配置
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"
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">
<!--指定要扫描的的包,这个路径的下的包的类中注解就会生效-->
<context:component-scan base-package="com.cike6"/>
<context:annotation-config/>
</beans>
要注解的类
java
// 等价于 <bean id="user" class="com.cike6.dao.User"/>
// @Component 组件
@Component
public class User {
public String name="cike_y";
}
测试方法
java
public class spring_6_Test {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.name);
}
}

属性如何注入
在对应属性上面加一个@Value 注解即可
java
@Component
public class User {
// 相当于 <property name="name" value="cike_y"/>
@Value("cike_y")
public String name;
}
衍生的注解
@Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!都是组件的意思,只是名字不一样为了划分,功能一样
dao【@Repository】
- 一般dao层都会用这个注解
service【@Service】
- service层用这个注解
controller【@Controller】
- 控制层用这个注解
controller的案例
- 如果找不到controller的容器id,我们还可以指定注解显示定义一个容器id名
使用bean注解的类,并且属性注入的注解方式
java
@Controller("usercontroller")
public class UserController {
@Value("aa")
public String name;
}
注解约束的xml配置
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"
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">
<!--指定要扫描的的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.cike6"/>
<context:annotation-config/>
</beans>
测试方法并且实例化容器id
java
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController usercontroller = context.getBean("usercontroller", UserController.class);
System.out.println(usercontroller.name);
}
可以看见成功输出 UserController类下的属性值name

这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean
自动装配
plain
@Autowired:自动装配通过类型。名字
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xx")
@Nullable 字段标记了这个注解,说明这个字段可以null
@Resource 自动装配通过名字、类型
作用域
java
@Component
@Scope("singleton") // 单例模式
// 原型模式 @Scope("prototype")
public class User {
// 相当于 <property name="name" value="cike_y"/>
@Value("cike_y")
public String name;
}
小结
xml与注解:
- xml更加万能,适用于任何场景!维护简单方便
- 注解不是自己的类使用不了,维护相对复杂
xml与注解的最佳实践:
- xml用来管理bean;
- 注解之复杂完成属性的注入
- 我们在使用的过程中,只需要注意一个问题:必须要注解生效就需要开启注解的支持、扫描包
xml
<context:component-scan base-package="com.cike6"/>
<context:annotation-config/>