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();   
    }
}

 

 
 
 
 
 
 
 
posted @ 2016-03-26 12:03  我是一名老菜鸟  阅读(1730)  评论(0编辑  收藏  举报