一、說明 與@Component註解功能相同,但意義不同的註解還有三個: 1)@Repository:註解在Dao實現類上 2)@Service:註解在Service實現類上 3)@Controller:註解在SpringMVC的處理器上 Bean作用域: @Scope("prototype"):用 ...
一、說明
與@Component註解功能相同,但意義不同的註解還有三個:
1)@Repository:註解在Dao實現類上
2)@Service:註解在Service實現類上
3)@Controller:註解在SpringMVC的處理器上
Bean作用域:
@Scope("prototype"):用於指定對象創建模式,可以是單例模式或者原型模式,預設是singleton
基本類型屬性註入:
@Value
@Autowired:byType方式的註解式註入,即根據類型註解
@Qualifier("mySchool"):byName方式的註解式註入,在使用@Qualifier時必須與@Autowired聯合使用
域屬性註解:
@Resource:不加name屬性則為byType方式的註解式註入,但前提是註入的對象只能有一個
@Resource(name="mySchool"):byName方式的註解式註入
Bean的生命始末:
@PostConstruct:當前Bean初始化剛完畢
@PreDestroy:當前Bean即將被銷毀
@Configuration:表示當前類充當Spring容器,即所有的Bean將由這個類來創建
註意:
在舉例之前聲明幾個問題:
1、註解需要依賴spring-aop-4.3.9.RELEASE.jar包,所以需要導入依賴包。
2、使用註解方式註入,配置文件需要添加約束頭文件:
<?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">
也可以自己從Spring的說明文檔中找到此頭文件:
3、如果使用到了SpringJUnit4測試,則還需要導入spring-test-4.3.9.RELEASE.jar包
二、舉例
1、首先創建一個School類:
package com.ietree.spring.basic.annotation.demo1; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("mySchool") public class School { @Value(value = "清華大學") private String name; public School() { super(); } public School(String name) { super(); this.name = name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "School [name=" + name + "]"; } }
創建Student類:
package com.ietree.spring.basic.annotation.demo1; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * 說明: * 與@Component註解功能相同,但意義不同的註解還有三個: * 1)@Repository:註解在Dao實現類上 * 2)@Service:註解在Service實現類上 * 3)@Controller:註解在SpringMVC的處理器上 * * Bean作用域: * @Scope("prototype"):用於指定對象創建模式,可以是單例模式或者原型模式,預設是singleton * * 基本類型屬性註入: * @Value * * @Autowired:byType方式的註解式註入,即根據類型註解 * @Qualifier("mySchool"):byName方式的註解式註入,在使用@Qualifier時必須與@Autowired聯合使用 * * 域屬性註解: * @Resource:不加name屬性則為byType方式的註解式註入,但前提是註入的對象只能有一個 * @Resource(name="mySchool"):byName方式的註解式註入 * * Bean的生命始末: * @PostConstruct:當前Bean初始化剛完畢 * @PreDestroy:當前Bean即將被銷毀 */ //@Scope("prototype") @Component("myStudent") public class Student { @Value(value = "小明") private String name; @Value(value = "25") private int age; // @Autowired // @Qualifier("mySchool") // @Resource(name="mySchool") @Resource private School school;// 對象屬性,也叫做域屬性 public Student() { super(); } public Student(String name, int age) { super(); this.name = name; this.age = age; } public void setName(String name) { System.out.println("執行setName()"); this.name = name; } public void setAge(int age) { System.out.println("執行setAge()"); this.age = age; } public void setSchool(School school) { this.school = school; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", school=" + school + "]"; } @PostConstruct public void initAfter(){ System.out.println("當前Bean初始化剛完畢"); } @PreDestroy public void preDestroy(){ System.out.println("當前Bean即將被銷毀"); } }
創建MyJavaConfig類:
package com.ietree.spring.basic.annotation.demo1; import org.springframework.beans.factory.annotation.Autowire; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Configuration:表示當前類充當Spring容器,即所有的Bean將由這個類來創建 */ @Configuration public class MyJavaConfig { @Bean(name="mySchool") public School mySchoolCreator(){ return new School("清華大學"); } // autowire=Autowire.BY_TYPE:指從當前類這個容器中查找與域屬性的類型具有is-a關係的Bean // autowire=Autowire.BY_NAME:指從當前類這個容器中查找與域屬性同名的Bean @Bean(name="myStudent", autowire=Autowire.BY_TYPE) public Student myStudentCreator(){ return new Student("小明", 25); } }
創建配置文件:
<?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"> <!-- 掃描 com.ietree.spring.basic.annotation.demo1這個包及其子包 --> <context:component-scan base-package="com.ietree.spring.basic.annotation.demo1"/> <!-- 掃描 com.ietree.spring.basic這個包的子包 --> <context:component-scan base-package="com.ietree.spring.basic.*"/> </beans>
創建測試類:
package com.ietree.spring.basic.annotation.demo1; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:com/ietree/spring/basic/annotation/demo1/applicationContext.xml") public class MyTest { @Autowired private Student student; @Test public void test01() { String resource = "com/ietree/spring/basic/annotation/demo1/applicationContext.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(resource); School school = (School) ctx.getBean("mySchool"); System.out.println(school); Student student = (Student) ctx.getBean("myStudent"); System.out.println(student); ((ClassPathXmlApplicationContext)ctx).close(); } public void test02(){ System.out.println(student); } }