欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

spring常用的注入方式有哪些?-創(chuàng)新互聯(lián)

1、xml中配置
bean 的申明、注冊
<bean> 節(jié)點注冊 bean
<bean> 節(jié)點的 factory-bean 參數(shù)指工廠 bean,factory-method 參數(shù)指定工廠方法
bean 的注入
<property> 節(jié)點使用 set 方式注入
<constructor-arg> 節(jié)點使用 構(gòu)造方法注入
實測代碼
maven pom 文件?
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
A、<bean> + <property>,set方法注入
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 配置文件
<?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">
<bean id="bowl" class="constxiong.interview.inject.Bowl" />
<bean id="person" class="constxiong.interview.inject.Person">
<property name="bowl" ref="bowl"></property>
</bean>
</beans>
測試類
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,<bean> + <constructor-arg> 節(jié)點使用 構(gòu)造方法注入
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 配置文件
<?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">
<bean id="bowl" class="constxiong.interview.inject.Bowl" />
<bean id="person" class="constxiong.interview.inject.Person">
<constructor-arg name="bowl" ref="bowl"></constructor-arg>
</bean>
</beans>
C、<bean> 節(jié)點 factory-method 參數(shù)指定靜態(tài)工廠方法
工廠類,靜態(tài)工廠方法
package constxiong.interview.inject;
public class BowlFactory {
public static final Bowl getBowl() {
return new Bowl();
}
}
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: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">
<bean id="bowl" class="constxiong.interview.inject.BowlFactory" factory-method="getBowl"/>
<bean id="person" class="constxiong.interview.inject.Person">
<constructor-arg name="bowl" ref="bowl"></constructor-arg>
</bean>
</beans>
D、非靜態(tài)工廠方法,需要指定工廠 bean 和工廠方法
工廠類,非靜態(tài)工廠方法
package constxiong.interview.inject;
public class BowlFactory {
public Bowl getBowl() {
return new Bowl();
}
}
配置文件
<?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">
<bean id="bowlFactory" class="constxiong.interview.inject.BowlFactory"></bean>
<bean id="bowl" factory-bean="bowlFactory" factory-method="getBowl"/>
<bean id="person" class="constxiong.interview.inject.Person">
<constructor-arg name="bowl" ref="bowl"></constructor-arg>
</bean>
</beans>
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è)置注解掃描目錄
<?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="constxiong.interview" />
</beans>
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ù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價格,機房位于中國電信/網(wǎng)通/移動機房,四川綿陽服務(wù)器托管服務(wù)有保障!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享文章:spring常用的注入方式有哪些?-創(chuàng)新互聯(lián)
文章路徑:http://aaarwkj.com/article40/dpgdho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、云服務(wù)器響應(yīng)式網(wǎng)站、App開發(fā)、建站公司、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司
国产乱码精品一区二区蜜臀| 亚洲精品一区二区av| 99热这里只有精品欧美| 亚洲一区二区三区在线播| 日韩不卡在线免费播放| 欧美一区二区三区亚洲| 九九在线免费视频蜜臀| 亚洲日本日本午夜精品| 国产午夜精品自拍视频| 亚洲国产日本一区自拍| 欧美大片黄片在线观看| 亚洲熟女内射特写一区| 国内精品久久久久久2021| 一区二区欧美日韩91| 国产丝袜美腿一二三区| 日韩蜜桃av一二三四区| 国产传媒网约在线观看| 久久人体午夜激情视频| 国产精品中文字幕欧美日韩| 国产亚洲一区二区自拍视频| 色婷婷久久五月中文字幕| 日韩不卡高清免费在线视频| 午夜高清影院免费观看| 国产黄片一区二区不卡| 粉嫩av蜜臀一区二区三区| 香蕉久草官网视频观看| 日韩精品一区二区三区人妻视频| 夜夜草视频在线免费观看| 中文字幕久久一区二区三区| 日韩成人精品一区欧美成人| 免费看男人添女人无遮挡| 久久色综合色悠悠色综合色| 91久久亚洲综合精品日本| 日本中文字幕乱码一区| 中字幕人妻一区二区三区| 亚洲日本不卡在线一区二区| 久久婷婷综合激情亚洲| 日本韩国欧美一区二区在线| 国产久精品热看久品热久热| 国产极品美女视频福利| 亚洲精品乱码精品乱码不卡|