1、xml中配置
bean 的申明、注冊(cè)
bean 的注入
實(shí)測(cè)代碼
maven pom 文件?
A、
class Bowl
package constxiong.interview.inject;
public class Bowl {
public void putRice() {
System.out.println("盛飯...");
}
}
class Person
package constxiong.interview.inject;
public class Person {
private Bowl bowl;
public void eat() {
bowl.putRice();
System.out.println("開始吃飯...");
}
public void setBowl(Bowl bowl) {
this.bowl = bowl;
}
}
spring 配置文件
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">
測(cè)試類
package constxiong.interview.inject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InjectTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring_inject.xml");
Person person = (Person)context.getBean("person");
person.eat();
}
}
B、修改為 配置文件和class Person,
class Person
package constxiong.interview.inject;
public class Person {
private Bowl bowl;
public Person(Bowl bowl) {
this.bowl = bowl;
}
public void eat() {
bowl.putRice();
System.out.println("開始吃飯...");
}
}
spring 配置文件
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">
C、
工廠類,靜態(tài)工廠方法
package constxiong.interview.inject;
public class BowlFactory {
public static final Bowl getBowl() {
return new Bowl();
}
}
spring 配置文件
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">
D、非靜態(tài)工廠方法,需要指定工廠 bean 和工廠方法
工廠類,非靜態(tài)工廠方法
package constxiong.interview.inject;
public class BowlFactory {
public Bowl getBowl() {
return new Bowl();
}
}
配置文件
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">
2、注解
bean 的申明、注冊(cè)
@Component //注冊(cè)所有bean
@Controller //注冊(cè)控制層的bean
@Service //注冊(cè)服務(wù)層的bean
@Repository //注冊(cè)dao層的bean
bean 的注入
@Autowired 作用于 構(gòu)造方法、AxiTrader返傭www.fx61.com/brokerlist/axitrader.html字段、方法,常用于成員變量字段之上。
@Autowired + @Qualifier 注入,指定 bean 的名稱
@Resource JDK 自帶注解注入,可以指定 bean 的名稱和類型等
測(cè)試代碼
E、spring 配置文件,設(shè)置注解掃描目錄
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">
class Bowl
package constxiong.interview.inject;
import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Controller;
//import org.springframework.stereotype.Repository;
//import org.springframework.stereotype.Service;
@Component //注冊(cè)所有bean
//@Controller //注冊(cè)控制層的bean
//@Service //注冊(cè)服務(wù)層的bean
//@Repository //注冊(cè)dao層的bean
public class Bowl {
public void putRice() {
System.out.println("盛飯...");
}
}
class Person
package constxiong.interview.inject;
//import javax.annotation.Resource;
//
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component //注冊(cè)所有bean
//@Controller //注冊(cè)控制層的bean
//@Service //注冊(cè)服務(wù)層的bean
//@Repository //注冊(cè)dao層的bean
public class Person {@Autowired
br/>@Autowired
// @Resource(name="bowl")
private Bowl bowl;
public void eat() {
bowl.putRice();
System.out.println("開始吃飯...");
}
}
測(cè)試類同上
A、B、C、D、E 測(cè)試結(jié)果都o(jì)k
盛飯...
開始吃飯...
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。