java註解配置spring

新建applicationContext.xml
在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">

	<!-- add entry to enable component scanning -->
	
	<context:component-scan base-package="com.luv2code.springdemo" />
</beans>

添加註解

package com.luv2code.springdemo;

import org.springframework.stereotype.Component;

@Component("thisSillyCoach")
public class TennisCoach implements Coach {

	@Override
	public String getDailyWorkout() {
		// TODO Auto-generated method stub
		return "practice your backhand volley";
	}

}

package com.luv2code.springdemo;

public interface Coach {
	public String getDailyWorkout();
}

從spring容器中獲取bean

package com.luv2code.springdemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationDemo {

	public static void main(String[] args) {
		//read spring config file
		ClassPathXmlApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//get the bean from spring container
		Coach theCoach = context.getBean("thisSillyCoach",Coach.class);
		//call a method on the bean
		System.out.println(theCoach.getDailyWorkout());
		
		//close the context
		context.close();
	}

}

輸出結果:

practice your backhand volley

2.可以用默認的註解名獲取bean

@Component   //改爲@Component
public class TennisCoach implements Coach {

	@Override
	public String getDailyWorkout() {
		// TODO Auto-generated method stub
		return "practice your backhand volley";
	}

}

在實現類裏面的beanid改爲獲取類的首字母小寫的類名tennisCoach

		//get the bean from spring container
		Coach theCoach = context.getBean("tennisCoach",Coach.class);

一、用註解注入依賴

public interface FortuneService {
	
	public String getFortune();
	
}

public interface Coach {
	public String getDailyFortune();
}

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

一個構造函數 @Autowired 說明當創建 bean 時,即使在 XML 文件中沒有使用 元素配置 bean ,構造函數也會被自動連接

@Component
public class TennisCoach implements Coach {

	private FortuneService fortuneService;
	@Autowired
	public TennisCoach(FortuneService theFortuneService) {
		fortuneService = theFortuneService;
	}
	@Override
	public String getDailyFortune() {
		return fortuneService.getFortune();
	}
}

public class AnnotationDemo {

	public static void main(String[] args) {
		//read spring config file
		ClassPathXmlApplicationContext context = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		//get the bean from spring container
		Coach theCoach = context.getBean("tennisCoach",Coach.class);
		//call method to get the daily fortune
		System.out.println(theCoach.getDailyFortune());
		//close the context
		context.close();
	}

}
Today is your lucky day!

二、使用setter建立依賴

在tennisCoach類中更改構造方法:

	//define a default constructor
	public TennisCoach() {
		System.out.println(">> TennisCoach: inside default constructor");
	}
	@Autowired     //set方法換成其他名稱同樣適用
	public void setFortuneService(FortuneService theFortuneService) {
		System.out.println(">> TennisCoach: inside setFortuneService() method");
		fortuneService = theFortuneService;
	}

重新運行AnnotationDemo:

>> TennisCoach: inside default constructor
>> TennisCoach: inside setFortuneService() method
Today is your lucky day!

三、使用作用域配置

public class TennisCoach implements Coach {
	@Autowired
	private FortuneService fortuneService;
	...
	}

如果FortuneService接口被多個類繼承,那麼TennisCoach類在調用的時候使用的是哪一個呢?

例如創建RandomService ,RESTFortuneService 類:

@Component
public class RandomService implements FortuneService {

	@Override
	public String getFortune() {
		// TODO Auto-generated method stub
		return "RandomService";
	}

}

@Component
public class DatabaseFortune implements FortuneService {

	@Override
	public String getFortune() {
		// TODO Auto-generated method stub
		return "DatabaseService";
	}

}
@Component
public class RESTFortuneService implements FortuneService {

	@Override
	public String getFortune() {
		// TODO Auto-generated method stub
		return null;
	}

}

在spring裏面,如果沒有在作用域聲明的話,就會報錯找不到指定的bean:


Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.luv2code.springdemo.FortuneService' available: expected single matching bean but found 4: databaseFortune,happyFortuneService,RESTFortuneService,randomService

因此,需要在使用的類作用域內指定調用的具體類:

public class TennisCoach implements Coach {
	@Autowired
	@Qualifier("databaseFortune")
	...
	}

DatabaseService

Reference:

Core Technologies about Spring:Spring Core Technologies

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