spring IoC注解开发---【小白系列】0基础到熟练应用spring框架(二)
2017-07-11 11:16
826 查看
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.jar2.指定测试类
这里我们新建一个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(); } }
相关文章推荐
- spring环境搭建&spring快速入门---【小白系列】0基础到熟练应用spring框架(一)
- spring AOP---【小白系列】0基础到熟练应用spring框架(三)
- struts2环境搭建---【小白系列】0基础到熟练应用struts2框架(一)
- HibernateUtils工具---【小白系列】0基础到熟练应用hibernate框架(四)
- struts2值栈&struts ognl表达式---【小白系列】0基础到熟练应用struts2框架(四)
- struts2获取作用域对象---【小白系列】0基础到熟练应用struts2框架(三)
- hibernate主键生成策略---【小白系列】0基础到熟练应用hibernate框架(七)
- Spring 自学系列之IOC-10【注解开发@Autowired】
- hibernate事务---【小白系列】0基础到熟练应用hibernate框架(十)
- hibernate的Configuration,SessionFactory,Session详解---【小白系列】0基础到熟练应用hibernate框架(二)
- hibernatJPA---【小白系列】0基础到熟练应用hibernate框架(十五)
- hibernate实体类的三种状态---【小白系列】0基础到熟练应用hibernate框架(八)
- Struts2结果视图配置---【小白系列】0基础到熟练应用struts2框架(二)
- hibernate实体类的创建规则---【小白系列】0基础到熟练应用hibernate框架(六)
- struts2 interceptor拦截器---【小白系列】0基础到熟练应用struts2框架(六)
- hibernate多对多关系---【小白系列】0基础到熟练应用hibernate框架(十三)
- struts2参数封装---【小白系列】0基础到熟练应用struts2框架(五)
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.2 相关Java基础知识 类装载器 反射机制
- hibernate中对象视图导航查询|加载策略---【小白系列】0基础到熟练应用hibernate框架(十四)
- hibernate一级缓存---【小白系列】0基础到熟练应用hibernate框架(九)