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

xiaoxiao2021-02-28  101

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

转载请注明原文地址: https://www.6miu.com/read-52832.html

最新回复(0)