日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring学习(36):注入简单类型

發(fā)布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring学习(36):注入简单类型 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄結構

pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.geyao</groupId><artifactId>spring01geyao</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>3.2.0.RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency></dependencies> </project>

applictioncontext.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:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--bean元素:描述當前的對象需要由spring容器管理id屬性:標識對象 未來在應用程序中可以根據(jù)id獲取對象class對象:被管理的對象的全名--> <bean name="compactDisc1 compactDisc1" class="com.geyao.demo.soundSystem.CompactDisc"><constructor-arg name="title" value="I Do"/><constructor-arg name="artist" value="莫文蔚"/> </bean><!-- <bean name="compactDisc2" class="com.geyao.demo.soundSystem.CompactDisc"></bean><bean name="cdPlayer1" class="com.geyao.demo.soundSystem.CDPlayer"><constructor-arg ref="compactDisc1"/></bean><bean name="cdPlayer2" class="com.geyao.demo.soundSystem.CDPlayer" c:cd-ref="compactDisc2"></bean>--> </beans>

log4j.properties

### 設置### log4j.rootLogger = ERROR,stdout### 輸出信息到控制抬 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n log4j.category.org.springframework.beans.factory=ERROR

CompactDisc類

import com.geyao.demo.soundSystem.CompactDisc; import org.springframework.context.support.ClassPathXmlApplicationContext;public class ApplicationSpring {public static void main(String[] args){System.out.println("Application ");ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");CompactDisc cd=context.getBean(CompactDisc.class);cd.play();} }

cdplayer類

package com.geyao.demo.soundSystem;public class CDPlayer {private CompactDisc cd;public CDPlayer() {super();System.out.println("cdplayer的構造方法"+this.toString());}public CDPlayer(CompactDisc cd){this.cd=cd;System.out.println("cdplayer的有殘構造方法");}public void play(){cd.play();} }

ApplicationSpring類

import com.geyao.demo.soundSystem.CompactDisc; import org.springframework.context.support.ClassPathXmlApplicationContext;public class ApplicationSpring {public static void main(String[] args){System.out.println("Application ");ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");CompactDisc cd1=(CompactDisc) context.getBean("compactDisc1");CompactDisc cd2=(CompactDisc) context.getBean("compactDisc2");cd1.play();cd2.play();} }

appTest類

package com.geyao.demo.souundSystem;import com.geyao.demo.soundSystem.CompactDisc; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AppTest {@Autowiredprivate CompactDisc compactDisc1;//@Autowired//private CompactDisc compactDisc12;//@Autowired//@Qualifier("compacDisc2")// private CompactDisc compactDisc3;@Testpublic void testApp(){compactDisc1.play();// compactDisc12.play();// compactDisc3.play();}}

CDplayerTest

package com.geyao.demo.souundSystem;import com.geyao.demo.soundSystem.CDPlayer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class CDplayerTest {@Autowiredprivate CDPlayer cdPlayer1;@Autowiredprivate CDPlayer cdPlayer2;@Testpublic void test01(){cdPlayer1.play();}@Testpublic void test02(){cdPlayer1.play();}}

運行結果

compactDisc的有殘構造函數(shù)com.geyao.demo.soundSystem.CompactDisc@342c38f8 播放cd音樂com.geyao.demo.soundSystem.CompactDisc@342c38f8I Do莫文蔚

?

總結

以上是生活随笔為你收集整理的spring学习(36):注入简单类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。