一、注意事项
- 需要导入spring-aop的包。
二、让项目支持注解
- 在spring的配置文件中添加约束
- 在spring的配置文件中配置注解扫描
<?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.seven.bean"/>
<!-- 配置扫描属性的注解,上面的那个配置了过后,这个就没有配置的意义了 -->
<context:annotation-config/>
</beans>
三、Bean的实例化
在类上使用注解:
@Component 最基本的
@Controller 控制层
@Service 业务层
@Repository 持久层
这是个在实例化Bean中没有区别,只是习惯上是这样写,方便阅读。
例:
这四个都有一个参数value,相当于xml中bean的id。
四、Bean的作用域
在类上使用注解:
@Scope(value = “”)
例:
五、Bean的属性注入
Bean的属性注入有两个注解:
-
@Autowired
用于对 Bean 的属性变量、属性的 Set 方法及构造函数进行标注,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。
@Autowired有一个属性:required
required的默认值未true,表示必须注入该属性。可设置未false,表示不必须注入该属性。
拓展:
@Qualifier
与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。
-
@Resource
其作用与 Autowired 一样。其区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 实例名称进行装配。@Resource 中有两个重要属性:name 和 type。
Spring 将 name 属性解析为 Bean 实例名称,type 属性解析为 Bean 实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。
如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。
六、注解总结
-
@Component
可以使用此注解描述 Spring 中的 Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可。 -
@Repository
用于将数据访问层(DAO层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 -
@Service
通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 -
@Controller
通常作用在控制层(如 Struts2 的 Action),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 -
@Autowired
用于对 Bean 的属性变量、属性的 Set 方法及构造函数进行标注,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。@Autowired有一个属性:required
required的默认值未true,表示必须注入该属性。可设置未false,表示不必须注入该属性。 -
@Resource
其作用与 Autowired 一样。其区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 实例名称进行装配。@Resource 中有两个重要属性:name 和 type。
Spring 将 name 属性解析为 Bean 实例名称,type 属性解析为 Bean 实例类型。如果指定 name 属性,则按实例名称进行装配;如果指定 type 属性,则按 Bean 类型进行装配。
如果都不指定,则先按 Bean 实例名称装配,如果不能匹配,则再按照 Bean 类型进行装配;如果都无法匹配,则抛出 NoSuchBeanDefinitionException 异常。
-
@Qualifier
与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。
七、混合使用XML+注解
本例演示:
使用xml的方式创建Bean,使用注解的方式注入属性。
创建两个类:
Card类:
package com.seven.bean;
public class Card {
public void doSomething() {
System.out.println("Card...");
}
}
Person类:
package com.seven.bean;
import javax.annotation.Resource;
public class Person {
//使用注解的方式注入属性
@Autowried
private Card card;
public void doSomething() {
System.out.println("Person...");
card.doSomething();
}
}
配置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.seven.bean"/>
<!-- 使用xml的方式创建Bean -->
<bean id="person" class="com.seven.bean.Person"/>
<bean id="card" class="com.seven.bean.Card"/>
</beans>
测试类:
import com.seven.bean.Person;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XaTest {
private ApplicationContext context;
@Before
public void init() {
context = new ClassPathXmlApplicationContext("spring.config.xml");
}
@Test
public void test() {
Person person = (Person) context.getBean("person");
person.doSomething();
}
}
运行结果: