要使用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");
}