要使用Annotation,需确认Spring的版本是2.0以上,JDK版本是5.0以上
使用注解的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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 指明Spring容器使用注解的方式初始化bean -->
<context:annotation-config/>
</beans>
@Autowired
按类型去查找并注入
使用范围:
1.setter方法
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
2.以成员变量(bean)为参数的方法
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
3.构造方法或成员变量
public class MovieRecommender {
@Autowired
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
@Autowired(required=false) 当查找失败时不会报错,默认不写时为required=true
@Qualifier
按名字去查找并注入
1.使用在成员变量上
public class MovieRecommender {
@Autowired
@Qualifier("mainCatalog")
private MovieCatalog movieCatalog;
// ...
}
2.使用在方法参数bean上
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("mainCatalog") MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
@Resource
按name值进行查找 @Resource有一个‘name’属性,缺省时,Spring 将这个值解释为要注射的 bean 的名字
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
// 在容器中查找名字为myMovieFinder的bean
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
// 在容器中查找名字为movieFinder的bean
@Component
是所有受Spring管理组件的通用形式。
@Repository(存储层)、@Service(服务层)和 @Controller(控制层)则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说, 你能用@Component来注解你的组件类, 但如果用@Repository、@Service 或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。
加上该注解的bean,就不用在xml中进行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
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="org.example"/>
</beans>
使用@Component注解需要在xml配置自动扫描范围。同时Spring也提供了在扫描范围内进行过滤的配置。
过滤器类型 | 表达式范例 |
---|---|
annotation | org.example.SomeAnnotation |
assignable | org.example.SomeClass |
regex | org/.example/.Default.* |
aspectj | org.example..*Service+ |
<context:component-scan base-package="org.example">
<!-- 配置包含过滤 -->
<context:include-filter type="regex" expression=".*Stub.*Repository"/>
<!-- 配置排除过滤 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
// 注册名字为myMovieLister,类型为SimpleMovieLister,生命周期为prototype的bean
@Component("myMovieLister")
@Scope("prototype")
public class SimpleMovieLister {
// ...
}
// 注册名字为movieFinderImpl ,类型为SimpleMovieLister的bean (默认bean名字为类名且首字母小写,生命周期为singleton)
@Component
public class MovieFinderImpl implements MovieFinder {
// ...
}
@PostConstruct
表明该方法在bean初始化时首先执行
@PostConstruct
public void init() {
System.out.println("init");
}
@PreDestroy
表明该方法在bean生命周期结束时执行
@PreDestroy
public void destroy() {
System.out.println("destroy");
}