1、xml中配置
bean 的申明、注冊
bean 的注入
實測代碼
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">
測試類
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 的申明、注冊
@Component //注冊所有bean
@Controller //注冊控制層的bean
@Service //注冊服務(wù)層的bean
@Repository //注冊dao層的bean
bean 的注入
@Autowired 作用于 構(gòu)造方法、AxiTrader返傭www.fx61.com/brokerlist/axitrader.html字段、方法,常用于成員變量字段之上。
@Autowired + @Qualifier 注入,指定 bean 的名稱
@Resource JDK 自帶注解注入,可以指定 bean 的名稱和類型等
測試代碼
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 //注冊所有bean
//@Controller //注冊控制層的bean
//@Service //注冊服務(wù)層的bean
//@Repository //注冊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 //注冊所有bean
//@Controller //注冊控制層的bean
//@Service //注冊服務(wù)層的bean
//@Repository //注冊dao層的bean
public class Person {@Autowired
br/>@Autowired
// @Resource(name="bowl")
private Bowl bowl;
public void eat() {
bowl.putRice();
System.out.println("開始吃飯...");
}
}
測試類同上
A、B、C、D、E 測試結(jié)果都o(jì)k
盛飯...
開始吃飯...
成都創(chuàng)新互聯(lián)是專業(yè)的松江網(wǎng)站建設(shè)公司,松江接單;提供做網(wǎng)站、網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行松江網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!