Spring IoC — 基於Java類的配置

Spring IoC — 基於Java類的配置

普通的POJO只要標註@Configuration註解,就可以爲Spring容器提供Bean定義的信息了,每個標註了@Bean的類方法都相當於提供一個Bean的定義信息。

基於Java類的配置方法和基於XML或基於註解的配置方式相比,前者通過代碼的方式更加靈活地實現Bean的實例化及Bean之間的裝配,但後面兩者都是通過配置聲明的方式,在靈活性上要稍遜一些,但是配置上要更簡單一些。
 
UserDao類:
複製代碼
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");
    }
}
複製代碼

 

logDao類:
複製代碼
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();
    }
}
複製代碼
輸出結果:
LogDao
userDao
hello!
 
 
DaoConfig類:
複製代碼
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();   
    }
}

轉自:https://www.cnblogs.com/yangyquin/p/5322591.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章