Spring 學習 (四) 註解的使用
阿新 • • 發佈:2019-02-09
分四塊
1.如何將使用註解當前類放入到容器?
將當前類放入Spring容器(如果不新增名稱,則以當前類名為預設名稱)
@Component("user")//相當於<bean name="user" class="cn.itcast.bean.User"></bean>
下面三個註解從功能上來說與component一樣,都是將當前類放入Spring容器中,只不過使用下面三個註解在實際開發中,比較好區分當前類在哪一層
@Service //service層
@Repository //dao層
@Controller //action層
下面是具體案例:
指定掃描註解路徑
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" >
<!--
指定掃描cn.itcast.bean包下所有類中的註解
注意:掃描包時,會掃描指定報下的所有子孫包
-->
<context:component-scan base-package="cn.itcast.bean"></context:component-scan>
</beans>
寫一個測試類
/*@Service //service層
@Repository //dao層
@Controller //action層
相當於<bean name="user" class="cn.itcast.bean.User"></bean>
*/@Component("user")
public class User {
private String name ;
private Integer age ;
private Car car ;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void init ()
{
System.out.println("init");
}
public void destroy ()
{
System.out.println("destroy");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
測試
public class Demo {
@Test
public void testAnnotation ()
{
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml") ;
User user = (User) ac.getBean("user") ;
System.out.println(user);
}
}
2.作用範圍
以多例模式來建立(不寫即預設單例)
@Scope(scopeName="prototype")
3.屬性注入
值型別注入
@Value
兩種用法:
1,放到成員變數
//將tom賦給name屬性
@Value("TOM")
private String name ;
2.放到set方法上
@Value("TOM")
public void setName(String name) {
this.name = name;
}
區別:
第一種情況是通過反射的field直接賦值,直接對name賦值
第二種情況是通過set方法進行賦值
按道理講應該推薦使用set賦值,因為第一種破壞了封裝性,但是使用set賦值又會使bean中程式碼變得比較亂,所以你隨意嘍
物件型別注入
首先你要把要注入的那個物件也加入到容器中
@Component("car")
public class Car {
@Autowired
//侷限:如果匹配到多個型別一致的物件,無法選擇具體注入哪個物件
@Autowired //自動裝配 從容器中找當前要賦值型別的物件(Car),然後付給他
private Car car ;
@Qualifier要和@Autowired一起用才有效
@Autowired
@Qualifier("car")//告訴Spring容器自動裝配那個物件
private Car car ;
@Resource 推薦
//如果不加入name,則根據這個欄位去IOC容器中找相同型別(在容器中要確保改型別只有一個變數)
@Resource(name="car")//手動注入,指定注入哪個名稱的物件
private Car car ;
4.初始化與銷燬
@PostConstruct
@PreDestroy
@PostConstruct //初始化,在物件建立後呼叫
public void init ()
{
System.out.println("init");
}
@PreDestroy //在銷燬之前呼叫
public void destroy ()
{
System.out.println("destroy");
}
@Test
public void testAnnotation ()
{
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml") ;
User user = (User) ac.getBean("user") ;
ac.close();
}
總結:
1) 使用註解,可以簡化配置,且可以把物件加入IOC容器,及處理依賴關係(DI)
2) 註解可以和XML配置一起使用。