配置spring容器

1.XML文件
2.Java annotation(註解)
3.Java Source Code

C:eclipse-workspace\springdemo\src>tree /f
/src
│  applicationContext.xml
│
└─springdemo
        BaseballCoach.java
        Coach.java
        helloSpringApp.java
        MyApp.java
        TrackCoach.java

\springdemo\src\applicationContext.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">

    <!-- Define your beans here -->
    <bean id = "myCoach"
    	class = "springdemo.TrackCoach">
    	</bean>
</beans>

springdemo\src\springdemo\Coach.java:

package springdemo;

public interface Coach {
	public String getDailyWorkout(); 
}

\springdemo\src\springdemo\helloSpringApp.java:

package springdemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class helloSpringApp {

	public static void main(String[] args) {
		//load the spring configuration file
		ClassPathXmlApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		//retrieve bean from spring container
			Coach theCoach = context.getBean("myCoach",Coach.class);
			
		//call methods on the bean
			System.out.println(theCoach.getDailyWorkout());
		//close the context
		context.close();
	}

}	

\springdemo\src\springdemo\TrackCoach.java

package springdemo;

public class TrackCoach implements Coach {

	@Override
	public String getDailyWorkout() {
		// TODO Auto-generated method stub
		return "Run a hard 5k";
	}
}

在xml中添加依賴關係

比如,在BasketballCoach類中調用了HappyFortuneService類中的getFortune方法,那麼就需要在xml文件中將這種依賴關係聲明,不然它也找不到相應的方法。

FortuneService 接口

public interface FortuneService {
	public String getFortune();
}

HappyFortuneService 類

public class HappyFortuneService implements FortuneService {
	@Override
	public String getFortune() {
		return "Today is your lucky day!";
	}
}

BaseballCoach 類

public class BaseballCoach implements Coach {
	private FortuneService fortuneService;
	//define a private field for the dependency
	@Override
	public String getDailyWorkout() {
		return "get 30 mins on batting practice ";
	}

	//define a constructor for dependency injection
	public BaseballCoach(FortuneService theFortuneService) {
		fortuneService = theFortuneService;
	}
	
	@Override
	public String getDailyFortune() {
		//use my fortune to get a fortune
		return fortuneService.getFortune();
	}
}

Coach 接口

public interface Coach {
	public String getDailyWorkout();
	public String getDailyFortune();
}

<?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">

    <!-- Define your beans here -->
    <!-- Define the dependency -->
    <bean id = "myFortune"
    class = "springdemo.HappyFortuneService">
    </bean>
    
    <bean id = "myCoach"
    	class = "springdemo.TrackCoach">
    	    <!-- set up constructer injection -->
    	    <constructor-arg ref = "myFortune" />
    	</bean>
    	</beans>

用setter方法

CricketCoach .java

public class CricketCoach implements Coach {
	private FortuneService fortuneService;
	//create a no-arg constructor
	public CricketCoach() {
		System.out.println("CricketCoach:inside no-arg constructor ");
	}
	public void setFortuneService(FortuneService fortuneService) {
		System.out.println("CricketCoach:inside setter method - setFortuneServie ");
		this.fortuneService = fortuneService;
	}

SetterDemo.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetterDemo {
	public static void main(String[] args) {
		//load the spring configuration file		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");	
		//retrieve bean from spring container		
		CricketCoach theCoach = context.getBean("myCricketCoach",CricketCoach.class);	 
		//call  methods on the bean
		System.out.println(theCoach.getDailyWorkout());
		System.out.println(theCoach.getDailyFortune());
		//close the context
		context.close();
	}
}
CricketCoach:inside no-arg constructor 
CricketCoach:inside setter method - setFortuneServie 
prcatice fast bowling for 15 mins
Today is your lucky day!

applicationContext.xml

    <bean id = "myFortune"
    class = "springdemo.HappyFortuneService">
    </bean>
    <bean id = "myCricketCoach"
    	class="springdemo.CricketCoach">
    	<!-- set up setter injection -->
    	<property name="fortuneService" ref = "myFortune" />
	</bean>

首先setterdemo使用bean id = "myCricketCoach"的CricketCoach類創建一個theCoach對象,創建對象時CricketCoach構造器和set方法執行得到“CricketCoach:inside no-arg constructor
CricketCoach:inside setter method - setFortuneServie ”,getDailyWorkout是cricketCoach類本身的方法,getDailyFortune的實現使用FortuneService接口實例化的對象,再調用HappyFortuneService類裏面的getFortune方法得到。

從xml注入固定值

    <bean id = "myCricketCoach"
    	class="springdemo.CricketCoach">
    	
    	<!-- inject literal values -->
    	<property name = "emailAddress" value = "8200@qq" />
    	<property name = "team" value = "Amazon" />
	</bean>
public class CricketCoach implements Coach {
	private String emailAddress;
	private String team;
	
	public String getEmailAddress() {
		return emailAddress;
	}
	public void setEmailAddress(String emailAddress) {
		System.out.println("CricketCoach:inside setter method - emailAddress ");
		this.emailAddress = emailAddress;
	}
	public String getTeam() {
		return team;
	}
	public void setTeam(String team) {
		System.out.println("CricketCoach:inside setter method - setTeam ");
		this.team = team;
	}
	}
public class SetterDemo {
	public static void main(String[] args) {
		//load the spring configuration file
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		//retrieve bean from spring container
		CricketCoach theCoach = context.getBean("myCricketCoach",CricketCoach.class);	
		// call our new methods to get the literal values
		System.out.println(theCoach.getEmailAddress());
		System.out.println(theCoach.getTeam());
		//close the context
		context.close();
	}
}
CricketCoach:inside setter method - emailAddress 
CricketCoach:inside setter method - setTeam 
8200@qq
Amazon

從properties文件中注入變量值

applicationContext.xml

<!-- load the properties file:sport.properties(聲明路徑) -->
	<context:property-placeholder location = "classpath:sport.properties" />
	
	<!-- 把上面的values內容改爲在properties文件中的變量名 -->
    	<property name = "emailAddress" value = "${foo.email}" />
    	<property name = "team" value = "${foo.team}" />

新建sport.properties文件並編輯:

foo.email = zjiang24@stevens
foo.team = amzon

重新運行setterdemo文件獲取變量值

zjiang24@stevens
amzon
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章