spring三: 装配bean( 在xml中进行显式配置, 在java中进行显式配置)

49 篇文章 0 订阅

 

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); SpringConfiguration配置类作为类

AnnotationConfigApplicationContext的参数,那么SpringConfiguration就可以省略@Configuration. 如下

//@Configuration
@ComponentScan(basePackages = {"com.atchina"})
public class SpringConfiguration {

}

 

在java中进行显式配置 

package soundsystem2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CDPlayerConfig {
	
	 // @Bean注解告诉spring这个方法将会返回一个对象,该对象
	// 要注册为spring应用上下文中的bean.
	@Bean
	public CompactDisc compactDisc(){
		return new SgtPeppers();
	}
	
	@Bean
	public CDPlayer cdPlayer(CompactDisc compactDisc){
		return new CDPlayer(compactDisc);
	} 
}

通过xml装配bean

   spring刚刚出现时,xml是描述配置的主要方式.但现在xml已经不再是Spring的唯一可选方案。spring已经有了强大的自动化配置和基于java的配置,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:c="http://www.springframework.org/schema/c"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <!--配置bean -->

</beans>

 

 

 

@Import注解

public class JdbcConfig {
    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner getRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }

    @Bean("dataSource")
    public DataSource getDataSource(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setUser("root");
            dataSource.setPassword("1");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
        }catch (Exception e){
            e.printStackTrace();
        }
        return dataSource;
    }
}

 

@PropertySource

 

@ComponentScan(basePackages = {"com.atchina"})
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {

}


public class JdbcConfig {

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.driver}")
    private String driver;

    @Bean(name="runner")
    @Scope("prototype")
    public QueryRunner getRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }

    @Bean("dataSource")
    public DataSource getDataSource(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setJdbcUrl(url);
            dataSource.setDriverClass(driver);
        }catch (Exception e){
            e.printStackTrace();
        }
        return dataSource;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值