JavaWeb学习笔记-spring-07-ioc-基于java类的配置

基于java类的配置

//将一个pojo标注为bean配置类型
@Configuration
public class AppConf{
    @Bean
    //将一个pojo标注为bean配置类型
    public UserDao userDao(){
        return new UserDao();
    }

    @Bean
    public LogDao logDao(){
        return new LogDao();
    }
    @Bean
    //定义logonService的bean
    public LogonService logonService(){
        LogService logonService = new LogonService();
        //将定义的bean注入logonService Bean
        logonService.setLogDao(logDao());
        logonService.setUserDao(userDao());
        return logonService;
    }

}
@Bean(name="userDao")
<bean id="userDao" class="com.smart.anno.UserDao"/>
<bean id="logDao" class="com.smart.anno.LogDao"/>
<bena id="logonService" class="com.smart.conf.LogonService"
    p:logDao-ref="userDao"
    p:userDao-ref="logDao"/>

引用不同配置类中定义的Bean

@Configuration
public class DaoConfig{
    @Bean
    public UserDao userDao(){
        return new UserDao();
    }
    @Bean
    public LogDao logDao(){
        return new LogDao();
    }
}
@Configuration
public class ServiceConfig{
    @Autowired
    //像普通Bean一样注入DaoConfig
    private DaoConfig daoConfig;
    @Bean
    public LogonService logonService(){
        LogonService logonService = new LogonService();
        logonService.setLogDao(daoConfig.logDao());
        logonService.setUserDao(daConfig.userDao());
        return logonService;
    }
}

@Bean标注@Scope(“propotype”)

@Configuration
public class DaoConfig{
    @Scope("prototype")
    @Bean
    publci LogDao logDao(){
        return new LogDao();
    }
}

@Configuration类启动spring

public class JavaConfigTest{
    public static void main(String[] args){
        //使用@Configuration类提供的Bean定义信息启动容器
        ApplicationContet ctx = new AnnotationContext(AppConf.class);
        LogonService logonService = ctx.getBean(LogonService.class);
        logonService.printHello();
    }
}
加载多个@Configuration配置类
public class JavaConfigTest{
    public static void main(String[] args){
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        //注册多个@Configuration
        ctx.register(DaoConfig.class);
        ctx.register(ServiceConfig.class);
        //刷新容器以应用这些注解配置类
        ctx.refresh();
        LogonService logonService = ctx.getBean(LogonService.class);
        logonService.printHello();
    }
}

使用@Import

@Configuration
@Import(DaoConfig.class)
//可以使用@Import将多个配置类组装到一个配置类中
public class ServiceConfig{
    @Bean
    public LogonService logonService(){
        LogonService logonService = new LogonService();
        return logonService;
    }
}

通过xml配置文件引用@Configuration

<?xml version="1.0" encoding="UTF-8"?>
<benas 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/benas/spring-benas-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!--通过上下文加载AppConf的配置类-->
    <context:component-scan base-package="com.smart.conf" resource-pattern="Application.class"/>    
</beans>

通过Configuration配置类引用xml配置信息

<?xml version="1.0" encoding="UTF-8"?>
<benas 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/benas/spring-benas-3.1.xsd">
    <bean id="userDao" class="com.smart.conf.UserDao"/>
    <bean id="logDao" class="com.smart.conf.LogDao"/>
</beans>
//通过@ImportResource引入xml配置文件
@ConFiguration
@ImportResource("classpath:com/smart/conf/beans.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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值