© 版權聲明:本文為博主原創文章,轉載請注明出處
Spring Bean常用注解
@Component:通常注解,可用於任何Bean
@Repository:通常用於注解DAO層,即持久層
@Service:通常用於注解Service層,即服務層
@Controller:通常用於注解Controller層,即控制層
類的自動檢測及Bean的注冊
<context:component-scan base-package=""/>:自動掃描base-package定義的包或其子包下的類,並將帶有@Component,@Controller,@Service,@Repository等注解的類自動注冊到IOC容器中。
<context:annotation-config/>:隱式的向Spring容器中注冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 和RequiredAnnotationBeanPostProcessor 這四個BeanPostProcessor。
<context:component-scan/>包含了<context:annotation-config/>,因此使用<context:component-scan/>就不用再使用<context:annotation-config/>
定義Bean
Bean名稱由BeanNameGenerator生成(@Component,@Controller,@Service,@Repository都有個name屬性用於顯示的指定Bean Name;默認是類名首字母小寫)
也可使用<context:component-scan/>中的name-generator自定義Bean的命名策略,但是要實現BeanNameGenerator接口並且包含一個無參構造器
作用域
@Scope注解標識Bean的作用域。默認是singleton。
實例
1.項目結構
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.spring</groupId> <artifactId>Spring-BeanAnnotation</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <name>Spring-BeanAnnotation Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.3.7.RELEASE</spring.version> </properties> <dependencies> <!-- junit依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring核心依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>Spring-BeanAnnotation</finalName> </build> </project>
3.spring-beanannotation.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 自動掃描包下的Bean並注冊到IOC容器中 --> <context:component-scan base-package="org.spring.annotation.bean"/> </beans>
4.BeanAnnotation.java
package org.spring.annotation.bean; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Scope("prototype") @Component public class BeanAnnotation { public void say() { System.out.println("注解方式獲取成功"); } public void hasCode() { System.out.println("BeanAnnotation:" + this.hashCode()); } }
5.TestBase.java
package org.spring.annotation.test; import org.junit.After; import org.junit.Before; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StringUtils; public class TestBase { private ClassPathXmlApplicationContext context; private String xmlPath; /** * 無參構造器 */ public TestBase() { } /** * 含參構造器,初始化配置文件路徑 * * @param xmlPath * 配置文件路徑 */ public TestBase(String xmlPath) { this.xmlPath = xmlPath; } /** * 初始化spring的IOC容器 */ @Before public void before() { if(StringUtils.isEmpty(xmlPath)) {//配置文件默認路徑 xmlPath = "classpath:spring-*.xml"; } //加載配置文件到spring容器中 context = new ClassPathXmlApplicationContext(xmlPath.split("[,\\s]+")); //啟動IOC容器s context.start(); } /** * 銷毀容器 */ @After public void after() { if(context != null){ context.destroy(); } } /** * 根據bean id獲取bean對象 */ public Object getBean(String id) { return context.getBean(id); } }
6.TestBeanAnnotation.java
package org.spring.annotation.test; import org.junit.Test; import org.spring.annotation.bean.BeanAnnotation; public class TestBeanAnnotation extends TestBase { /** * 構造器傳入spring配置文件路徑 */ public TestBeanAnnotation() { super("classpath:spring-beanannotation.xml"); } /** * 測試注解方式獲取bean對象 */ @Test public void testBeanAnnotation() { BeanAnnotation bean = (BeanAnnotation) super.getBean("beanAnnotation"); bean.say(); } /** * 測試注解方式的Bean的作用域 */ @Test public void testBeanScope() { BeanAnnotation bean = (BeanAnnotation) super.getBean("beanAnnotation"); bean.hasCode(); BeanAnnotation bean2 = (BeanAnnotation) super.getBean("beanAnnotation"); bean2.hasCode(); } }
7.效果預覽