Spring IoC — 基於Java類的配置
普通的POJO只要標註@Configuration註解,就可以爲Spring容器提供Bean定義的信息了,每個標註了@Bean的類方法都相當於提供一個Bean的定義信息。
package com.ioc.cha4_11; public class UserDao { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "UserDao{" + "name='" + name + '\t' + '}'; } public UserDao() { System.out.println("userDao"); } }
package com.ioc.cha4_11; public class LogDao { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "LogDao{" + "name='" + name + '\t' + '}'; } public LogDao() { System.out.println("LogDao"); } }
logonService類:
package com.ioc.cha4_11; public class LogonService { private LogDao logDao; private UserDao userDao; public LogDao getLogDao() { return logDao; } public void setLogDao(LogDao logDao) { this.logDao = logDao; } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void printHelllo(){ System.out.println("hello!"); } }
AppConf類:
package com.ioc.cha4_11; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //將一個POJO標註爲定義Bean的配置類 @Configuration public class AppConf { //以下兩個方法定義了兩個Bean,並提供了Bean的實例化邏輯 @Bean public UserDao userDao() { return new UserDao(); } @Bean public LogDao logDao() { return new LogDao(); } //定義了logonService的Bean @Bean public LogonService logonService() { LogonService logonService = new LogonService(); //將前面定義的Bean輸入到LogonService Bean中 logonService.setLogDao(logDao()); logonService.setUserDao(userDao()); return logonService; } }
beans1.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="userDao" class="com.ioc.cha4_11.UserDao"/> <bean id="logDao" class="com.ioc.cha4_11.LogDao"/> <bean id="logonService" class="com.ioc.cha4_11.LogonService" p:logDao-ref="logDao" p:userDao-ref="userDao"/> </beans>
測試類:
package com.ioc.cha4_11; /** * Created by gao on 16-3-25. */ public class Test { public static void main(String[] args) { AppConf ac = new AppConf(); LogonService ls = ac.logonService(); ls.printHelllo(); } }
package com.ioc.cha4_11; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @Configuration public class DaoConfig { @Bean(name="userDao") public UserDao userDao(){ return new UserDao(); } //每次調用該方法都會返回一個新的LogDao Bean @Scope("prototype") @Bean public LogDao logDao(){ return new LogDao(); } }
ServiceConfig類:
package com.ioc.cha4_11; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(DaoConfig.class) public class ServiceConfig { //像普通Bean一樣注入DaoConfig @Autowired private DaoConfig daoConfig; @Bean public LogonService logonService(){ LogonService logonService = new LogonService(); System.out.println(daoConfig.logDao() == daoConfig.logDao()); //像普通Bean一樣,調用Bean相關的方法 logonService.setLogDao(daoConfig.logDao()); logonService.setUserDao(daoConfig.userDao()); return logonService; } }
LogonAppConfig類:
package com.ioc.cha4_11; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource("classpath:com\\ioc\\cha4_11\\beans3.xml") public class LogonAppConfig { @Bean @Autowired public LogonService logonService(UserDao userDao,LogDao logDao){ LogonService logonService = new LogonService(); logonService.setUserDao(userDao); logonService.setLogDao(logDao); return logonService; } }
beans2.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.ioc.cha4_11" resource-pattern="AppConf.class" /> </beans>
beans3.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="userDao" class="com.ioc.cha4_11.UserDao"/> <bean id="logDao" class="com.ioc.cha4_11.LogDao"/> </beans>
測試類:
package com.ioc.conf; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class JavaConfigTest { public static void main(String[] args) { //1.通過構造函數加載配置類 // ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConf.class); //2.通過編碼方式註冊配置類 // AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); // ctx.register(DaoConfig.class); // ctx.register(ServiceConfig.class); // ctx.refresh(); //3.通過XML組裝@Configuration配置類所提供的配置信息 // ApplicationContext ctx = new ClassPathXmlApplicationContext("com/baobaotao/conf/beans2.xml"); //4.通過@Configuration組裝XML配置所提供的配置信息 // ApplicationContext ctx = new AnnotationConfigApplicationContext(LogonAppConfig.class); //5.@Configuration的配置類相互引用 ApplicationContext ctx = new AnnotationConfigApplicationContext(DaoConfig.class,ServiceConfig.class); LogonService logonService = ctx.getBean(LogonService.class); System.out.println((logonService.getLogDao() !=null)); logonService.printHelllo(); } }