一、如果使用註解的話,在配置檔案中應該做什麼?
在beans標籤後加上一個
<context:annotation-config/>
複製程式碼
標籤來宣告將要使用註解就可以了。
<?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">
<!--在此處加上此標籤-->
<context:annotation-config/>
<bean name="c" class="com.learn.springDemo.Category">
<property name="name" value="Eather"></property>
<property name="id" value="12345"></property>
</bean>
<bean name="p" class="com.learn.springDemo.Product">
<property name="name" value="a product"></property>
<property name="id" value="1008611"></property>
<!--將下面這行註釋掉,因為程式碼中將使用自動裝配來將categroy例項注入product-->
<!--<property name="category" ref="c"></property>-->
</bean>
</beans>
複製程式碼
二、註解應該怎麼用?
如果是使用@Autowired註解的話,可以加在兩個地方前面。
屬性前面 以及 setter方法前面。
如下程式碼:
package com.learn.springDemo;
import org.springframework.beans.factory.annotation.Autowired;
public class Product {
private int id;
private String name;
//加在這個地方
@Autowired
private Category category;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
//加在這個地方
@Autowired
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Category getCategory() {
return this.category;
}
}
複製程式碼
在這裡必須要import org.springframework.beans.factory.annotation.Autowired;
執行結果:
a product`s id is 1008611 and its category name is Eather
複製程式碼
三、注入同一個類的不同例項應該怎麼辦?
上一篇筆記 裡提到了 xml裡面可以寫多個相同的bean嗎 這個問題,也就是注入同一個類的不同例項應該怎麼辦?在使用xml配置時很簡單,改一下name就行。
此時就可以使用@Resource註解
先改一下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">
<!--在此處加上此標籤-->
<context:annotation-config/>
<bean name="c" class="com.learn.springDemo.Category">
<property name="name" value="Eather"></property>
<property name="id" value="12345"></property>
</bean>
<bean name="cc" class="com.learn.springDemo.Category">
<property name="name" value="David"></property>
<property name="id" value="10086"></property>
</bean>
<bean name="p" class="com.learn.springDemo.Product">
<property name="name" value="a product"></property>
<property name="id" value="1008611"></property>
<!--將下面這行註釋掉-->
<!--<property name="category" ref="c"></property>-->
</bean>
</beans>
複製程式碼
註解應該加在屬性的前面
package com.learn.springDemo;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
public class Product {
private int id;
private String name;
@Resource(name = "c")
private Category category;
@Resource(name = "cc")
private Category category1;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(Category category) {
this.category = category;
}
public void setCategory1(Category category1) {
this.category1 = category1;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Category getCategory() {
return this.category;
}
public Category getCategory1() {
return category1;
}
}
複製程式碼
測試程式碼:
package com.learn.springTest;
import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Product p = (Product) ac.getBean("p");
System.out.println(p.getName() + "`s id is " + p.getId() + " and its category name is " + p.getCategory().getName() + " and " + p.getCategory1().getName());
}
}
複製程式碼
執行結果:
a product`s id is 1008611 and its category name is Eather and David
複製程式碼
四、xml配置檔案裡可不可以 bean 也不寫?
上述例子都是對 注入物件行為 的註解,對於bean物件本身的使用可不可以也用註解的方式進行,而不寫到配置檔案中呢?
答案當然是可以,修改一下配置檔案,去掉所有beans和bean標籤,加上
<context:component-scan base-package="com.learn.springDemo"/>
複製程式碼
base-package 用來指定要掃描的包。
<?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">
<!--在此處加上此標籤-->
<context:component-scan base-package="com.learn.springDemo" />
</beans>
複製程式碼
類定義程式碼是:
package com.learn.springDemo;
import org.springframework.stereotype.Component;
@Component("c")
public class Category {
private int id;
private String name = "我是一個Category";
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
package com.learn.springDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("p")
public class Product {
private int id;
private String name = "我是一個product";
@Autowired
private Category category;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public Category getCategory() {
return this.category;
}
}
複製程式碼
測試程式碼是:
package com.learn.springTest;
import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
Product p = (Product) ac.getBean("p");
System.out.println("product 名字是 " + p.getName() + " category 名字是 " + p.getCategory().getName());
}
}
複製程式碼
執行結果是:
product 名字是 我是一個product category 名字是 我是一個Category
複製程式碼
至於前面的第三個問題應該怎麼解決,暫時我也不知道,可能這個問題沒有太大的意義吧!