7.spring注解注解开发
7.1 环境准备
-
在spring4之后想要使用注解实现 Spring 自动装配,还需要引入Spring 提供的 spring-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"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context/spring-aop.xsd">
<!--配置注解的支持-->
<context:annotation-config/>
<!--新版本配置扫描注解就可以运行,开启组件扫描功能-->
<!--指定需要扫描的包,这个包下的注解会生效-->
<context:component-scan base-package="com.zk"></context:component-scan>
</beans>
-
这里也可以用空格扫描多个包
-
有了context:component-scan,另一个context:annotation-config/标签可以移除掉,因为已经被包含进去了。
-
举例:
-
实体类加@Component注解
/**
* @Component相当于xml中的bean
* @Component组件
*/
@Component
@Scope("singleton")
public class User {
private String name;
public String getName() {
return name;
}
@Value("tony")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
- 编写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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context/spring-aop.xsd">
<!--指定需要扫描的包,这个包下的注解会生效-->
<context:component-scan base-package="com.zk"></context:component-scan>
</beans>
- 调用测试
@Test
public void test01(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
//User{name='tony'}
7.2 spring注解
注解定义 Bean
- 使用注解定义 Bean,Spring 提供了以下多个注解,这些注解可以直接标注在 Java 类上,将它们定义成 Spring Bean。
注解 | 说明 |
---|---|
@Component | 该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等。 使用时只需将该注解标注在相应类上即可。 |
@Repository | 该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
@Service | 该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
@Controller | 该注解通常作用在控制层(如 Struts2 的 Action、SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
- @Component放在类上,说明这个类被spring作为bean管理了
- 注解方式注册的bena,调用时beanid为类首字母小写字符串,由于这种注册方式时单例模式,在容器中只有一份,所以可以通过@Autowired拿到
属性注入类
-
@Value(“tony”)作用在基本数据属性或String上为其注入值,相当于bean的 元素中的 value 属性
-
@Value(“tony”)也可以作用在setter方法上
@Value("tony") private String name;
- 引用类型用@atuowired
作用域类注解
@Scope("singleton")作用在类上表示时单例类
7.3 小结
- xml和注解对比
xml万能,适用于任何场合,后期维护简单方便
注解:自己的类引用bean和被其他bean引用比较麻烦,维护比较麻烦,若实体类多的时候。
最开始是为了不让程序员频繁改代码,注解之后又要回去改代码。
- 配合使用
xml用来管理bean
注解负责完成属性注入
注意使用注解要导入约束并开启注解扫描包,包路径设置对