[4]Spring以註解的方式進行Bean的例項化和屬性注入
在進行Spring開發時所需要的基礎jar包有:
當需要在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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation ="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
</beans>
在配置檔案中進行配置:
在需要使用註解進行的時候,需要在配置檔案中進行 開啟註解掃描的配置
<!--開啟註解的掃描 掃描com.merpyzf下所有的包-->
<context:component-scan base-package="com.merpyzf" ></context:component-scan>
<!--只會掃描屬性上的註解-->
<context:annotation-config></context:annotation-config>
使用註解的方式來建立物件:
Computer.java(需要被建立的類)
/**
* Created by wangke on 17-6-23.
*/
@Component(value = "computer") //相當於在配置檔案中 <bean id="computer" class="……"></bean>
@Scope(value = "prototype") //配置建立物件是否是以單列模式進行建立
public class Computer {
public void printBrand(){
System.out.println("小米筆記本");
}
}
在需要例項化的類的類名上面加上@Component 註解來進行標識,value的值就相當於在配置檔案中進行配置時bean標籤的id屬性的值,用於物件的建立。
建立物件的四個註解:
@Component
@Controller //web層中使用
@Service //業務層
@Repository //持久層
這四個註解目前的功能都是一樣的,註解名的不同為了能夠讓標記類本身的用途更加清晰,Spring在後續的版本中會對其加強。
建立物件的測試程式碼:
@Test
public void Test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext02.xml");
Computer computer = (Computer) context.getBean("computer");
computer.printBrand();
}
執行結果:
使用註解注入物件屬性
在開發中我們經常會遇到在一個類中持有另外一個類的引用,在前面的部落格中記錄了使用配置檔案中注入物件屬性的方式,下面我們接著來看一下使用註解進行注入的方式:
下面的例子,在UserService中持有UserDao這個物件的引用
UserDao.java
/**
* Created by 春水碧於天 on 2017/6/23.
*/
@Component(value = "userDao")
public class UserDao {
public void printInfo(){
System.out.println("UserDao中的例項化物件裡的方法執行了");
}
}
兩種物件屬性注入的註解:
1.使用 @Autowired 註解進行自動裝配,不需要指定要注入的物件的value值,自動的根據類名去尋找對應的類來建立物件並進行物件屬性的注入。
2. 使用 @Resource(name=”userDao”),需要指定需要建立的物件的名字,這裡的name對應@Component註解中的value的值,使用這個註解能夠根據我們所指定的物件名準確創建出我們所需要的物件。
UserService.java(示例中使用@Resource註解進行物件屬性的注入)
/**
* Created by 春水碧於天 on 2017/6/23.
*/
@Service(value = "userService")
public class UserService {
private List list;
//@Autowired //自動裝配,根據類名稱去找相應的類來建立物件
@Resource(name="userDao") //name屬性值寫使用註解建立物件時的value中的值
private UserDao userDao;
public void print(){
userDao.printInfo();
}
}
MyTest.java 測試程式碼:
@Test
public void Test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) context.getBean("userService");
service.print();
}
執行結果:
配置檔案和註解混合使用
1.建立物件的操作使用配置檔案的方式實現
2.注入屬性的操作使用註解的方式實現
下面看一下混合使用的例子:
在StudentService中持有BookDao,ComputerDao物件的引用,呼叫StudentService例項化物件中的printDo()方法測試物件是否建立成功。
BookDao.java
/**
* Created by 春水碧於天 on 2017/6/23.
*/
public class BookDao {
public void doSomething(){
System.out.println("讀書");
}
}
ComputerDao.java
/**
* Created by 春水碧於天 on 2017/6/23.
*/
public class ComputerDao {
public void doSomething(){
System.out.println("敲程式碼……");
}
}
StudentService.java
/**
* Created by 春水碧於天 on 2017/6/23.
*/
public class StudentService {
@Resource(name = "bookDao")
private BookDao bookDao;
@Resource(name = "computerDao")
private ComputerDao computerDao;
public void printDo(){
bookDao.doSomething();
computerDao.doSomething();
}
}
applicationContext.xml(Spring 配置檔案)
<!--開啟註解掃描-->
<context:component-scan base-package="com.merpyzf"></context:component-scan>
<!--進行物件的建立-->
<bean id="studentService" class="com.merpyzf.study.StudentService"></bean>
<bean id="bookDao" class="com.merpyzf.study.BookDao"></bean>
<bean id="computerDao" class="com.merpyzf.study.ComputerDao"></bean>
MyTest.java(測試程式碼)
@Test
public void Test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService service = (StudentService) context.getBean("studentService");
service.printDo();
}
執行結果: