在寫完Spring第二和第三篇後,去讀了Spring In Action這本書…發現有知識點要補充,知識點跨越了第二和第三篇,所以專門再開一篇博文來寫…java
因爲Spring的自動裝配並不能將第三方庫組件裝配到應用中,因而須要顯式裝配配置。顯示裝配有兩種方式spring
Spring In Action做者首推使用自動裝配的功能,然後是經過java代碼配置bean,最後才用XML文件配置的方式..markdown
那麼怎麼經過java代碼來配置Bean呢??app
@org.springframework.context.annotation.Configuration public class Configuration { }
@org.springframework.context.annotation.Configuration public class Configuration { @Bean public UserDao userDao() { UserDao userDao = new UserDao(); System.out.println("我是在configuration中的"+userDao); return userDao; } }
package bb; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; /** * Created by ozc on 2017/5/11. */ //加載配置類的信息 @ContextConfiguration(classes = Configuration.class) public class Test2 { @Test public void test33() { ApplicationContext ac = new ClassPathXmlApplicationContext("bb/bean.xml"); UserDao userDao = (UserDao) ac.getBean("userDao"); System.out.println(userDao); } }
Spring in Action有兩種方法(但我測試不出來,若是會的請在評論去告訴我.….)函數
import org.springframework.context.annotation.Bean; @org.springframework.context.annotation.Configuration public class Configuration { @Bean() public UserDao userDao() { return new UserDao(); } @Bean public UserService userService() { //直接調用@bean的方法 return new UserService(userDao()); } }
import org.springframework.context.annotation.Bean; @org.springframework.context.annotation.Configuration public class Configuration { @Bean() public UserDao userDao() { return new UserDao(); } @Bean public UserService userService(UserDao userDao) { //經過構造函數依賴注入 return new UserService(userDao); } }
import org.springframework.beans.factory.annotation.Autowire; import org.springframework.context.annotation.Bean; @org.springframework.context.annotation.Configuration public class Configuration { @Bean() public UserDao userDao() { return new UserDao(); } @Bean(autowire = Autowire.BY_TYPE) public UserService userService(UserDao userDao) { return new UserService(userDao); } }
public class Configuration { @Bean() public UserDao userDao() { return new UserDao(); } @Bean(autowire = Autowire.BY_TYPE) public UserService userService() { return new UserService(userDao()); } }
固然了,最簡單直觀的方法還有一種:在UserService中加入setUser()方法,那麼只要set進去就好了..測試
public class UserService { private UserDao userDao ; public UserService() { } public UserService(UserDao userDao) { } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
import org.springframework.context.annotation.Bean; @org.springframework.context.annotation.Configuration public class Config1 { @Bean(name = "userDao") public UserDao userDao() { return new UserDao(); } @Bean(name="userService") public UserService userService() { UserService userService = new UserService(); userService.setUserDao(userDao()); return userService; } }
咱們在使用XML配置建立Bean的時候,若是該Bean有構造器,那麼咱們使用<constructor-arg>
這個節點來對構造器的參數進行賦值…this
<constructor-arg>
未免有點太長了,爲了簡化配置,Spring來提供了c名稱空間,其實和咱們以前講到的p名稱空間是同樣的…spa
要想c名稱空間是須要導入xmlns:c="http://www.springframework.org/schema/c"
的code
<bean id="userService" class="bb.UserService" c:userDao-ref=""> </bean>
c名稱空間有個缺點:不能裝配集合,當咱們要裝配集合的時候仍是須要<constructor-arg>
這個節點xml
若是對象上的屬性或者構造函數擁有集合的時候,而咱們又須要爲集合賦值,那麼怎麼辦?
<bean id="userService" class="bb.UserService" > <constructor-arg > <list> //普通類型 <value></value> </list> </constructor-arg> </bean>
<property name="userDao"> <list> <ref></ref> </list> </property>
咱們有的時候可能會兩種配置混合時候…
若是JavaConfig的配置類是分散的,咱們通常再建立一個更高級的配置類(root),而後使用@Import來將配置類進行組合
若是XML的配置文件是分散的,咱們也是建立一個更高級的配置文件(root),而後使用來將配置文件組合
<bean>
節點就好了