spring IoC注解开发---【小白系列】0基础到熟练应用spring框架(二)

spring加载非自定义bean

以加载c3p0连接池为例,我们有个c3p0配置文件 jdbc.properties


#mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_demo
jdbc.user=root
jdbc.password=root

配置文件applicationContext.xml

<!-- 非自定义的Bean 例如:连接池-->
	<!-- 0、加载jdbc.properties文件  将该文件的信息放到容器中 方便其他bean在创建时 使用相关参数 -->
	<context:property-placeholder location="classpath:jdbc.properties"/> 
	
	<!-- 1、c3p0连接池 -->
	 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

spring注解开发

导入spring-aop.jar
在applicationContext.xml配置组件扫描
    <context:component-scan base-package="cn.it"></context:component-scan>
你注解使用在哪,就配哪。
@Component("annoBean")  
//@Repository("annoBean")
//@Service("annoBean")
//@Controller("annoBean")
@Scope("singleton")
//@Scope("prototype")
public class AnnoBean {

	public void show(){
		System.out.println("AnnoBean show running... ...");
	}
	
	@PostConstruct //该注解是AnnoBean初始化时执行的方法
	public void init(){
		System.out.println("初始化方法...");
	}
	
	@PreDestroy//该注解是AnnoBean销毁时执行的方法
	public void destroy(){
		System.out.println("销毁方法...");
	}
	
}


此外在web开发中,根据@Component还有三个衍生的注解

@Repository:dao层实体上使用该注解

@Service:service层实体上使用该注解

@Controller:web层实体上使用该注解


我们在来看下service层的属性注解

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

	//@Autowired//自动注入属性:从spring容器中 根据该属性的类型去寻找对应的实体  找到后自动注入到该位置
	//@Qualifier("customerDao")使用此属性是从spring容器中找叫customerDao的对象,而不是去寻找类型,该注解必须和@Autowired同时使用
	@Resource(name="customerDao") 注意:Resource = Autowired+Qualifier

	private CustomerDao customerDao;
	
	
	//注意:使用注解进行属性的注入 那么set方法可以省略
	/*public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}*/

	@Override
	public void save() {
		customerDao.save();
	}

}

spring新注解


/*
 * SpringConfiguration替代applicationContext.xml
 */
@Configuration//标注该类是一个配置类
@ComponentScan(basePackages={"com.itheima"})
@Import(value = { DataSourceProvider.class })//将其他的配置对象引入到核心配置对象内部
public class SpringConfiguration {

}

@PropertySource(value = { "classpath:jdbc.properties" })//将配置文件加载到容器中
public class DataSourceProvider {
	
	@Value(value = "${jdbc.driver}")
	private String driverClass;
	@Value(value = "${jdbc.url}")
	private String jdbcUrl;
	@Value(value = "${jdbc.user}")
	private String user;
	@Value(value = "${jdbc.password}")
	private String password;

	@Bean(name="dataSource")
	public DataSource createDataSource() throws PropertyVetoException{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass(driverClass);
		dataSource.setJdbcUrl(jdbcUrl);
		dataSource.setUser(user);
		dataSource.setPassword(password);
		
		return dataSource;
	}
	
	//spring4.3之前 手动配置 property的解析器
	@Bean
	public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer(){
		return new PropertySourcesPlaceholderConfigurer();
	}
@Configuration			标注一个类是配置对象(代替配置文件)
@ComponentScan(basePackages={"com.itheima"})
包扫描 <context:component-scan>
@Import				导入其他配置对象
@Bean(name="dataSource")
将方法的返回值以指定的名称放到spring容器中
@PropertySource(value = { "classpath:jdbc.properties" })
使用注解方式加载指定property配置文件到spring容器中
@Value(value = "${jdbc.driver}")
从spring容器中获得指定key的值


注意:实际开发中 自定义的类通过注解配置  非自定义的类通过配置文件
例如:CustomerDao CustomerService 使用注解
DataSource 使用配置文件

开发模式:注解+配置文件

spring整合junit测试


在junit测试中我们写了大量的重复代码,其实我们只需要getBean就够了,那么我们如何简化抽取呢,那就是让junit来帮我们完成配置文件的加载以及getbean

步骤

1.导包 spring-test-4.2.4.RELEASE.jar,junit-4.9.jar

2.指定测试类
这里我们新建一个SpringJunitTest类,

@RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)//SpringJUnit4ClassRunner帮你加载配置文件
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件进行测试  --重点
//@ContextConfiguration(classes={SpringConfiguration.class})配置类方式

public class SpringJunitTest {
	//测试service
	@Autowired
	private CustomerService customerService;
	@Autowired
	private CustomerDao customerDao;
	
	@Test
	public void test1(){
		customerDao.save();
	}
	
	@Test
	public void test(){
		customerService.save();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LawsonJin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值